2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Theming;
|
2021-03-16 13:14:03 -04:00
|
|
|
|
2021-03-19 12:16:26 -04:00
|
|
|
use BookStack\Auth\Access\SocialAuthService;
|
2021-11-22 17:22:31 -05:00
|
|
|
use Illuminate\Console\Application;
|
|
|
|
use Illuminate\Console\Application as Artisan;
|
2021-11-22 13:30:58 -05:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
2021-03-19 12:16:26 -04:00
|
|
|
|
2021-03-16 13:14:03 -04:00
|
|
|
class ThemeService
|
|
|
|
{
|
|
|
|
protected $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)
|
|
|
|
{
|
|
|
|
if (!isset($this->listeners[$event])) {
|
|
|
|
$this->listeners[$event] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->listeners[$event][] = $action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch the given event name.
|
|
|
|
* Runs any registered listeners for that event name,
|
|
|
|
* passing all additional variables to the listener action.
|
|
|
|
*
|
|
|
|
* If a callback returns a non-null value, this method will
|
|
|
|
* stop and return that value itself.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-16 13:14:03 -04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function dispatch(string $event, ...$args)
|
|
|
|
{
|
|
|
|
foreach ($this->listeners[$event] ?? [] as $action) {
|
|
|
|
$result = call_user_func_array($action, $args);
|
|
|
|
if (!is_null($result)) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2021-03-16 13:14:03 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-22 13:30:58 -05:00
|
|
|
/**
|
|
|
|
* Register a new custom artisan command to be available.
|
|
|
|
*/
|
|
|
|
public function registerCommand(Command $command)
|
|
|
|
{
|
2021-11-22 18:33:55 -05:00
|
|
|
Artisan::starting(function (Application $application) use ($command) {
|
2021-11-22 17:22:31 -05:00
|
|
|
$application->addCommands([$command]);
|
|
|
|
});
|
2021-11-22 13:30:58 -05:00
|
|
|
}
|
|
|
|
|
2021-03-16 13:14:03 -04:00
|
|
|
/**
|
|
|
|
* Read any actions from the set theme path if the 'functions.php' file exists.
|
|
|
|
*/
|
|
|
|
public function readThemeActions()
|
|
|
|
{
|
|
|
|
$themeActionsFile = theme_path('functions.php');
|
2021-07-03 06:53:46 -04:00
|
|
|
if ($themeActionsFile && file_exists($themeActionsFile)) {
|
2021-03-16 13:14:03 -04:00
|
|
|
require $themeActionsFile;
|
|
|
|
}
|
|
|
|
}
|
2021-03-19 12:16:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see SocialAuthService::addSocialDriver
|
|
|
|
*/
|
2021-05-24 07:55:45 -04:00
|
|
|
public function addSocialDriver(string $driverName, array $config, string $socialiteHandler, callable $configureForRedirect = null)
|
2021-03-19 12:16:26 -04:00
|
|
|
{
|
|
|
|
$socialAuthService = app()->make(SocialAuthService::class);
|
2021-05-24 07:55:45 -04:00
|
|
|
$socialAuthService->addSocialDriver($driverName, $config, $socialiteHandler, $configureForRedirect);
|
2021-03-19 12:16:26 -04:00
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
}
|