namespace BookStack\Theming;
use BookStack\Access\SocialAuthService;
+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<string, callable[]>
+ */
+ protected array $listeners = [];
/**
* 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] = [];
*
* 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);
/**
* Register a new custom artisan command to be available.
*/
- public function registerCommand(Command $command)
+ public function registerCommand(Command $command): void
{
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
*/
- 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);
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Tools\PageContent;
+use BookStack\Exceptions\ThemeException;
use BookStack\Facades\Theme;
use BookStack\Theming\ThemeEvents;
use BookStack\Users\Models\User;
});
}
+ public function test_theme_functions_loads_errors_are_caught_and_logged()
+ {
+ $this->usingThemeFolder(function ($themeFolder) {
+ $functionsFile = theme_path('functions.php');
+ file_put_contents($functionsFile, "<?php\n\\BookStack\\Biscuits::eat();");
+
+ $this->expectException(ThemeException::class);
+ $this->expectExceptionMessageMatches('/Failed loading theme functions file at ".*?" with error: Class "BookStack\\\\Biscuits" not found/');
+
+ $this->runWithEnv('APP_THEME', $themeFolder, fn() => null);
+ });
+ }
+
public function test_event_commonmark_environment_configure()
{
$callbackCalled = false;