X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/cdaad2f40ec96fd98e6320638854c69b90bfe847..refs/pull/5721/head:/app/Theming/ThemeService.php diff --git a/app/Theming/ThemeService.php b/app/Theming/ThemeService.php index f095c7a8e..4bdb6836b 100644 --- a/app/Theming/ThemeService.php +++ b/app/Theming/ThemeService.php @@ -2,23 +2,33 @@ namespace BookStack\Theming; -use BookStack\Auth\Access\SocialAuthService; +use BookStack\Access\SocialDriverManager; +use BookStack\Exceptions\ThemeException; +use Illuminate\Console\Application; +use Illuminate\Console\Application as Artisan; use Symfony\Component\Console\Command\Command; class ThemeService { - protected $listeners = []; + /** + * @var array + */ + protected array $listeners = []; /** - * @var Command[] + * Get the currently configured theme. + * Returns an empty string if not configured. */ - protected $commands = []; + public function getTheme(): string + { + return config('view.theme') ?? ''; + } /** * Listen to a given custom theme event, * setting up the action to be ran when the event occurs. */ - public function listen(string $event, callable $action) + public function listen(string $event, callable $action): void { if (!isset($this->listeners[$event])) { $this->listeners[$event] = []; @@ -34,10 +44,8 @@ class ThemeService * * If a callback returns a non-null value, this method will * stop and return that value itself. - * - * @return mixed */ - public function dispatch(string $event, ...$args) + public function dispatch(string $event, ...$args): mixed { foreach ($this->listeners[$event] ?? [] as $action) { $result = call_user_func_array($action, $args); @@ -50,38 +58,44 @@ class ThemeService } /** - * Register a new custom artisan command to be available. + * Check if there are listeners registered for the given event name. */ - public function registerCommand(Command $command) + public function hasListeners(string $event): bool { - $this->commands[] = $command; + return count($this->listeners[$event] ?? []) > 0; } /** - * Get the custom commands that have been registered. + * Register a new custom artisan command to be available. */ - public function getRegisteredCommands(): array + public function registerCommand(Command $command): void { - return $this->commands; + Artisan::starting(function (Application $application) use ($command) { + $application->addCommands([$command]); + }); } /** * Read any actions from the set theme path if the 'functions.php' file exists. */ - public function readThemeActions() + public function readThemeActions(): void { $themeActionsFile = theme_path('functions.php'); if ($themeActionsFile && file_exists($themeActionsFile)) { - require $themeActionsFile; + try { + require $themeActionsFile; + } catch (\Error $exception) { + throw new ThemeException("Failed loading theme functions file at \"{$themeActionsFile}\" with error: {$exception->getMessage()}"); + } } } /** - * @see SocialAuthService::addSocialDriver + * @see SocialDriverManager::addSocialDriver */ - public function addSocialDriver(string $driverName, array $config, string $socialiteHandler, callable $configureForRedirect = null) + public function addSocialDriver(string $driverName, array $config, string $socialiteHandler, ?callable $configureForRedirect = null): void { - $socialAuthService = app()->make(SocialAuthService::class); - $socialAuthService->addSocialDriver($driverName, $config, $socialiteHandler, $configureForRedirect); + $driverManager = app()->make(SocialDriverManager::class); + $driverManager->addSocialDriver($driverName, $config, $socialiteHandler, $configureForRedirect); } }