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