]> BookStack Code Mirror - bookstack/blob - app/Theming/ThemeService.php
7bc12c5d1561f5e1463b8ff52dea935b7b78e0e4
[bookstack] / app / Theming / ThemeService.php
1 <?php
2
3 namespace BookStack\Theming;
4
5 use BookStack\Auth\Access\SocialAuthService;
6 use Illuminate\Console\Application;
7 use Illuminate\Console\Application as Artisan;
8 use Illuminate\Contracts\Console\Kernel;
9 use Symfony\Component\Console\Command\Command;
10
11 class ThemeService
12 {
13     protected $listeners = [];
14
15     /**
16      * Listen to a given custom theme event,
17      * setting up the action to be ran when the event occurs.
18      */
19     public function listen(string $event, callable $action)
20     {
21         if (!isset($this->listeners[$event])) {
22             $this->listeners[$event] = [];
23         }
24
25         $this->listeners[$event][] = $action;
26     }
27
28     /**
29      * Dispatch the given event name.
30      * Runs any registered listeners for that event name,
31      * passing all additional variables to the listener action.
32      *
33      * If a callback returns a non-null value, this method will
34      * stop and return that value itself.
35      *
36      * @return mixed
37      */
38     public function dispatch(string $event, ...$args)
39     {
40         foreach ($this->listeners[$event] ?? [] as $action) {
41             $result = call_user_func_array($action, $args);
42             if (!is_null($result)) {
43                 return $result;
44             }
45         }
46
47         return null;
48     }
49
50     /**
51      * Register a new custom artisan command to be available.
52      */
53     public function registerCommand(Command $command)
54     {
55         Artisan::starting(function(Application $application) use ($command) {
56             $application->addCommands([$command]);
57         });
58     }
59
60     /**
61      * Read any actions from the set theme path if the 'functions.php' file exists.
62      */
63     public function readThemeActions()
64     {
65         $themeActionsFile = theme_path('functions.php');
66         if ($themeActionsFile && file_exists($themeActionsFile)) {
67             require $themeActionsFile;
68         }
69     }
70
71     /**
72      * @see SocialAuthService::addSocialDriver
73      */
74     public function addSocialDriver(string $driverName, array $config, string $socialiteHandler, callable $configureForRedirect = null)
75     {
76         $socialAuthService = app()->make(SocialAuthService::class);
77         $socialAuthService->addSocialDriver($driverName, $config, $socialiteHandler, $configureForRedirect);
78     }
79 }