Skip to content

Commit cdaad2f

Browse files
committed
Support custom commands via logical theme system
Added initial work to support registering commands through the logical theme system. Includes docs changes and example. Not yet covered via testing.
1 parent 4ddbc95 commit cdaad2f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

app/Console/Kernel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace BookStack\Console;
44

5+
use BookStack\Facades\Theme;
6+
use BookStack\Theming\ThemeService;
57
use Illuminate\Console\Scheduling\Schedule;
68
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
9+
use Symfony\Component\Console\Command\Command;
710

811
class Kernel extends ConsoleKernel
912
{
@@ -35,6 +38,13 @@ protected function schedule(Schedule $schedule)
3538
*/
3639
protected function commands()
3740
{
41+
// Default framework command loading from 'Commands' directory
3842
$this->load(__DIR__ . '/Commands');
43+
44+
// Load any user commands that have been registered via the theme system.
45+
$themeService = $this->app->make(ThemeService::class);
46+
foreach ($themeService->getRegisteredCommands() as $command) {
47+
$this->registerCommand($command);
48+
}
3949
}
4050
}

app/Theming/ThemeService.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
namespace BookStack\Theming;
44

55
use BookStack\Auth\Access\SocialAuthService;
6+
use Symfony\Component\Console\Command\Command;
67

78
class ThemeService
89
{
910
protected $listeners = [];
1011

12+
/**
13+
* @var Command[]
14+
*/
15+
protected $commands = [];
16+
1117
/**
1218
* Listen to a given custom theme event,
1319
* setting up the action to be ran when the event occurs.
@@ -43,6 +49,22 @@ public function dispatch(string $event, ...$args)
4349
return null;
4450
}
4551

52+
/**
53+
* Register a new custom artisan command to be available.
54+
*/
55+
public function registerCommand(Command $command)
56+
{
57+
$this->commands[] = $command;
58+
}
59+
60+
/**
61+
* Get the custom commands that have been registered.
62+
*/
63+
public function getRegisteredCommands(): array
64+
{
65+
return $this->commands;
66+
}
67+
4668
/**
4769
* Read any actions from the set theme path if the 'functions.php' file exists.
4870
*/

dev/docs/logical-theme-system.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,32 @@ Theme::listen(ThemeEvents::APP_BOOT, function($app) {
7777
});
7878
```
7979

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+
80106
## Custom Socialite Service Example
81107

82108
The below shows an example of adding a custom reddit socialite service to BookStack.

0 commit comments

Comments
 (0)