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