]> BookStack Code Mirror - bookstack/blob - dev/docs/logical-theme-system.md
Support custom commands via logical theme system
[bookstack] / dev / docs / logical-theme-system.md
1 # Logical Theme System
2
3 BookStack allows logical customization via the theme system which enables you to add, or extend, functionality within the PHP side of the system without needing to alter the core application files.
4
5 WARNING: This system is currently in alpha so may incur changes. Once we've gathered some feedback on usage we'll look to removing this warning. This system will be considered semi-stable in the future. The `Theme::` system will be kept maintained but specific customizations or deeper app/framework usage using this system will not be supported nor considered in any way stable. Customizations using this system should be checked after updates.
6
7 ## Getting Started
8
9 This makes use of the theme system. Create a folder for your theme within your BookStack `themes` directory. As an example we'll use `my_theme`, so we'd create a `themes/my_theme` folder.
10 You'll need to tell BookStack to use your theme via the `APP_THEME` option in your `.env` file. For example: `APP_THEME=my_theme`.
11
12 Within your theme folder create a `functions.php` file. BookStack will look for this and run it during app boot-up. Within this file you can use the `Theme` facade API, described below, to hook into certain app events.
13
14 ## `Theme` Facade API
15
16 Below details the public methods of the `Theme` facade that are considered stable:
17
18 ### `Theme::listen`
19
20 This method listens to a system event and runs the given action when that event occurs. The arguments passed to the action depend on the event. Event names are exposed as static properties on the `\BookStack\Theming\ThemeEvents` class. 
21
22 It is possible to listen to a single event using multiple actions. When dispatched, BookStack will loop over and run each action for that event.
23 If an action returns a non-null value then BookStack will stop cycling through actions at that point and make use of the non-null return value if possible (Depending on the event).
24
25 **Arguments**
26 - string $event
27 - callable $action
28
29 **Example**
30
31 ```php
32 Theme::listen(
33     \BookStack\Theming\ThemeEvents::AUTH_LOGIN,
34     function($service, $user) {
35         \Log::info("Login by {$user->name} via {$service}");
36     }
37 );
38 ```
39
40 ### `Theme::addSocialDriver`
41
42 This method allows you to register a custom social authentication driver within the system. This is primarily intended to use with [Socialite Providers](https://socialiteproviders.com/).
43
44 **Arguments**
45 - string $driverName
46 - array $config
47 - string $socialiteHandler
48
49 **Example**
50
51 *See "Custom Socialite Service Example" below.*
52
53 ## Available Events
54
55 All available events dispatched by BookStack are exposed as static properties on the `\BookStack\Theming\ThemeEvents` class, which can be found within the file `app/Theming/ThemeEvents.php` relative to your root BookStack folder. Alternatively, the events for the latest release can be [seen on GitHub here](https://github.com/BookStackApp/BookStack/blob/release/app/Theming/ThemeEvents.php).
56
57 The comments above each constant with the `ThemeEvents.php` file describe the dispatch conditions of the event, in addition to the arguments the action will receive. The comments may also describe any ways the return value of the action may be used. 
58
59 ## Example `functions.php` file
60
61 ```php
62 <?php
63
64 use BookStack\Facades\Theme;
65 use BookStack\Theming\ThemeEvents;
66
67 // Logs custom message on user login
68 Theme::listen(ThemeEvents::AUTH_LOGIN, function($method, $user) {
69     Log::info("Login via {$method} for {$user->name}");
70 });
71
72 // Adds a `/info` public URL endpoint that emits php debug details
73 Theme::listen(ThemeEvents::APP_BOOT, function($app) {
74     \Route::get('info', function() {
75         phpinfo(); // Don't do this on a production instance!
76     });
77 });
78 ```
79
80 ## Custom Commands
81
82 The logical theme system supports adding custom [artisan commands](https://laravel.com/docs/8.x/artisan) to BookStack. These can be registered in your `functions.php` file by calling `Theme::registerCommand($command)`, where `$command` is an instance of `\Symfony\Component\Console\Command\Command`. 
83
84 Below is an example of registering a command that could then be ran using `php artisan bookstack:meow` on the command line.
85
86 ```php
87 <?php
88
89 use BookStack\Facades\Theme;
90 use Illuminate\Console\Command;
91
92 class MeowCommand extends Command
93 {
94     protected $signature = 'bookstack:meow';
95     protected $description = 'Say meow on the command line';
96
97     public function handle()
98     {
99         $this->line('Meow there!');
100     }
101 }
102
103 Theme::registerCommand(new MeowCommand);
104 ```
105
106 ## Custom Socialite Service Example
107
108 The below shows an example of adding a custom reddit socialite service to BookStack. 
109 BookStack exposes a helper function for this via `Theme::addSocialDriver` which sets the required config and event listeners in the platform.
110
111 The require statements reference composer installed dependencies within the theme folder. They are required manually since they are not auto-loaded like other app files due to being outside the main BookStack dependency list. 
112
113 ```php
114 require "vendor/socialiteproviders/reddit/Provider.php";
115 require "vendor/socialiteproviders/reddit/RedditExtendSocialite.php";
116
117 Theme::listen(ThemeEvents::APP_BOOT, function($app) {
118     Theme::addSocialDriver('reddit', [
119         'client_id' => 'abc123',
120         'client_secret' => 'def456789',
121         'name' => 'Reddit',
122     ], '\SocialiteProviders\Reddit\RedditExtendSocialite@handle');
123 });
124 ```
125
126 In some cases you may need to customize the driver before it performs a redirect. 
127 This can be done by providing a callback as a fourth parameter like so:
128
129 ```php
130 Theme::addSocialDriver('reddit', [
131     'client_id' => 'abc123',
132     'client_secret' => 'def456789',
133     'name' => 'Reddit',
134 ], '\SocialiteProviders\Reddit\RedditExtendSocialite@handle', function($driver) {
135     $driver->with(['prompt' => 'select_account']);
136     $driver->scopes(['open_id']);
137 });
138 ```