3 namespace BookStack\Theming;
5 use BookStack\Access\SocialDriverManager;
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 * Get the currently configured theme.
20 * Returns an empty string if not configured.
22 public function getTheme(): string
24 return config('view.theme') ?? '';
28 * Listen to a given custom theme event,
29 * setting up the action to be ran when the event occurs.
31 public function listen(string $event, callable $action): void
33 if (!isset($this->listeners[$event])) {
34 $this->listeners[$event] = [];
37 $this->listeners[$event][] = $action;
41 * Dispatch the given event name.
42 * Runs any registered listeners for that event name,
43 * passing all additional variables to the listener action.
45 * If a callback returns a non-null value, this method will
46 * stop and return that value itself.
48 public function dispatch(string $event, ...$args): mixed
50 foreach ($this->listeners[$event] ?? [] as $action) {
51 $result = call_user_func_array($action, $args);
52 if (!is_null($result)) {
61 * Check if there are listeners registered for the given event name.
63 public function hasListeners(string $event): bool
65 return count($this->listeners[$event] ?? []) > 0;
69 * Register a new custom artisan command to be available.
71 public function registerCommand(Command $command): void
73 Artisan::starting(function (Application $application) use ($command) {
74 $application->addCommands([$command]);
79 * Read any actions from the set theme path if the 'functions.php' file exists.
81 public function readThemeActions(): void
83 $themeActionsFile = theme_path('functions.php');
84 if ($themeActionsFile && file_exists($themeActionsFile)) {
86 require $themeActionsFile;
87 } catch (\Error $exception) {
88 throw new ThemeException("Failed loading theme functions file at \"{$themeActionsFile}\" with error: {$exception->getMessage()}");
94 * @see SocialDriverManager::addSocialDriver
96 public function addSocialDriver(string $driverName, array $config, string $socialiteHandler, callable $configureForRedirect = null): void
98 $driverManager = app()->make(SocialDriverManager::class);
99 $driverManager->addSocialDriver($driverName, $config, $socialiteHandler, $configureForRedirect);