| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Theming; | 
					
						
							| 
									
										
										
										
											2021-03-17 01:14:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 21:49:53 +08:00
										 |  |  | use BookStack\Access\SocialDriverManager; | 
					
						
							| 
									
										
										
										
											2023-09-12 19:34:02 +08:00
										 |  |  | use BookStack\Exceptions\ThemeException; | 
					
						
							| 
									
										
										
										
											2021-11-23 06:22:31 +08:00
										 |  |  | use Illuminate\Console\Application; | 
					
						
							|  |  |  | use Illuminate\Console\Application as Artisan; | 
					
						
							| 
									
										
										
										
											2021-11-23 02:30:58 +08:00
										 |  |  | use Symfony\Component\Console\Command\Command; | 
					
						
							| 
									
										
										
										
											2021-03-20 00:16:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 01:14:03 +08:00
										 |  |  | class ThemeService | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-12 19:34:02 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var array<string, callable[]> | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected array $listeners = []; | 
					
						
							| 
									
										
										
										
											2021-03-17 01:14:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Listen to a given custom theme event, | 
					
						
							|  |  |  |      * setting up the action to be ran when the event occurs. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-12 19:34:02 +08:00
										 |  |  |     public function listen(string $event, callable $action): void | 
					
						
							| 
									
										
										
										
											2021-03-17 01:14:03 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         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. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-12 19:34:02 +08:00
										 |  |  |     public function dispatch(string $event, ...$args): mixed | 
					
						
							| 
									
										
										
										
											2021-03-17 01:14:03 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         foreach ($this->listeners[$event] ?? [] as $action) { | 
					
						
							|  |  |  |             $result = call_user_func_array($action, $args); | 
					
						
							|  |  |  |             if (!is_null($result)) { | 
					
						
							|  |  |  |                 return $result; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 01:14:03 +08:00
										 |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 05:38:43 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check if there are listeners registered for the given event name. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function hasListeners(string $event): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return count($this->listeners[$event] ?? []) > 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 02:30:58 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Register a new custom artisan command to be available. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-12 19:34:02 +08:00
										 |  |  |     public function registerCommand(Command $command): void | 
					
						
							| 
									
										
										
										
											2021-11-23 02:30:58 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-23 07:33:55 +08:00
										 |  |  |         Artisan::starting(function (Application $application) use ($command) { | 
					
						
							| 
									
										
										
										
											2021-11-23 06:22:31 +08:00
										 |  |  |             $application->addCommands([$command]); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-11-23 02:30:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 01:14:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Read any actions from the set theme path if the 'functions.php' file exists. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-12 19:34:02 +08:00
										 |  |  |     public function readThemeActions(): void | 
					
						
							| 
									
										
										
										
											2021-03-17 01:14:03 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $themeActionsFile = theme_path('functions.php'); | 
					
						
							| 
									
										
										
										
											2021-07-03 18:53:46 +08:00
										 |  |  |         if ($themeActionsFile && file_exists($themeActionsFile)) { | 
					
						
							| 
									
										
										
										
											2023-09-12 19:34:02 +08:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 require $themeActionsFile; | 
					
						
							|  |  |  |             } catch (\Error $exception) { | 
					
						
							|  |  |  |                 throw new ThemeException("Failed loading theme functions file at \"{$themeActionsFile}\" with error: {$exception->getMessage()}"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-17 01:14:03 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-20 00:16:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-12-06 21:49:53 +08:00
										 |  |  |      * @see SocialDriverManager::addSocialDriver | 
					
						
							| 
									
										
										
										
											2021-03-20 00:16:26 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-12 19:34:02 +08:00
										 |  |  |     public function addSocialDriver(string $driverName, array $config, string $socialiteHandler, callable $configureForRedirect = null): void | 
					
						
							| 
									
										
										
										
											2021-03-20 00:16:26 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-06 21:49:53 +08:00
										 |  |  |         $driverManager = app()->make(SocialDriverManager::class); | 
					
						
							|  |  |  |         $driverManager->addSocialDriver($driverName, $config, $socialiteHandler, $configureForRedirect); | 
					
						
							| 
									
										
										
										
											2021-03-20 00:16:26 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |