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