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