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