3 namespace BookStack\Theming;
5 use BookStack\Access\SocialAuthService;
6 use BookStack\Exceptions\ThemeException;
7 use Illuminate\Console\Application;
8 use Illuminate\Console\Application as Artisan;
9 use Symfony\Component\Console\Command\Command;
14 * @var array<string, callable[]>
16 protected array $listeners = [];
19 * Listen to a given custom theme event,
20 * setting up the action to be ran when the event occurs.
22 public function listen(string $event, callable $action): void
24 if (!isset($this->listeners[$event])) {
25 $this->listeners[$event] = [];
28 $this->listeners[$event][] = $action;
32 * Dispatch the given event name.
33 * Runs any registered listeners for that event name,
34 * passing all additional variables to the listener action.
36 * If a callback returns a non-null value, this method will
37 * stop and return that value itself.
39 public function dispatch(string $event, ...$args): mixed
41 foreach ($this->listeners[$event] ?? [] as $action) {
42 $result = call_user_func_array($action, $args);
43 if (!is_null($result)) {
52 * Check if there are listeners registered for the given event name.
54 public function hasListeners(string $event): bool
56 return count($this->listeners[$event] ?? []) > 0;
60 * Register a new custom artisan command to be available.
62 public function registerCommand(Command $command): void
64 Artisan::starting(function (Application $application) use ($command) {
65 $application->addCommands([$command]);
70 * Read any actions from the set theme path if the 'functions.php' file exists.
72 public function readThemeActions(): void
74 $themeActionsFile = theme_path('functions.php');
75 if ($themeActionsFile && file_exists($themeActionsFile)) {
77 require $themeActionsFile;
78 } catch (\Error $exception) {
79 throw new ThemeException("Failed loading theme functions file at \"{$themeActionsFile}\" with error: {$exception->getMessage()}");
85 * @see SocialAuthService::addSocialDriver
87 public function addSocialDriver(string $driverName, array $config, string $socialiteHandler, callable $configureForRedirect = null): void
89 $socialAuthService = app()->make(SocialAuthService::class);
90 $socialAuthService->addSocialDriver($driverName, $config, $socialiteHandler, $configureForRedirect);