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