Skip to content

Commit 0f76c80

Browse files
qschmickroshangautam
authored andcommitted
feature ( command-filters) : add command filter config to avoid loading all available commands
1 parent 6275779 commit 0f76c80

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ Totem::auth(function($request) {
8383

8484
By default Totem's dashboard only works in local environment. To view the dashboard point your browser to /totem of your app. For e.g. laravel.dev/totem.
8585

86+
##### Filter Commands Dropdown
87+
88+
By default `Totem` outputs all Artisan commands on the Create/Edit tasks. To make this dropdown more concise there is a filter config feature that can be set in the `totem.php` config file.
89+
90+
Example filters
91+
```php
92+
'artisan' => [
93+
'command_filter' => [
94+
'stats:*',
95+
'email:daily-reports'
96+
],
97+
],
98+
```
99+
100+
This feature uses [fnmatch](http://php.net/manual/en/function.fnmatch.php) syntax to filter displayed commands. `stats:*` will match all Artisan commands that start with `stats:` while `email:daily-reports` will only match the command named `email:daily-reports`.
101+
86102
#### Middleware
87103

88104
`Laravel Totem` uses the default web and api middleware but if customization is required the middleware can be changed by setting the appropriate `.env` value. These values can be found in `config/totem.php`.

config/totem.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,7 @@
222222
'api' => [
223223
'middleware' => env('TOTEM_API_MIDDLEWARE', 'api'),
224224
],
225+
'artisan' => [
226+
'command_filter' => [],
227+
],
225228
];

src/Http/Controllers/TasksController.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Studio\Totem\Task;
66
use Studio\Totem\Totem;
7-
use Illuminate\Support\Facades\Artisan;
87
use Studio\Totem\Contracts\TaskInterface;
98
use Studio\Totem\Http\Requests\TaskRequest;
109

@@ -48,9 +47,7 @@ public function create()
4847
{
4948
return view('totem::tasks.form', [
5049
'task' => new Task,
51-
'commands' => collect(Artisan::all())->sortBy(function ($command) {
52-
return $command->getDescription();
53-
}),
50+
'commands' => Totem::getCommands(),
5451
'timezones' => timezone_identifiers_list(),
5552
'frequencies' => Totem::frequencies(),
5653
]);
@@ -94,9 +91,7 @@ public function edit($task)
9491
{
9592
return view('totem::tasks.form', [
9693
'task' => $task,
97-
'commands' => collect(Artisan::all())->sortBy(function ($command) {
98-
return $command->getDescription();
99-
}),
94+
'commands' => Totem::getCommands(),
10095
'timezones' => timezone_identifiers_list(),
10196
'frequencies' => Totem::frequencies(),
10297
]);

src/Totem.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Studio\Totem;
44

55
use Closure;
6+
use Illuminate\Support\Facades\Artisan;
67

78
class Totem
89
{
@@ -48,4 +49,31 @@ public static function frequencies()
4849
{
4950
return config('totem.frequencies');
5051
}
52+
53+
/**
54+
* Return collection of Artisan commands filtered if needed.
55+
*
56+
* @return \Illuminate\Support\Collection
57+
*/
58+
public static function getCommands()
59+
{
60+
$command_filter = config('totem.artisan.command_filter');
61+
$all_commands = collect(Artisan::all());
62+
63+
if (! empty($command_filter)) {
64+
$all_commands = $all_commands->filter(function ($command) use ($command_filter) {
65+
foreach ($command_filter as $filter) {
66+
if (fnmatch($filter, $command->getName())) {
67+
return true;
68+
}
69+
}
70+
71+
return false;
72+
});
73+
}
74+
75+
return $all_commands->sortBy(function ($command) {
76+
return $command->getDescription();
77+
});
78+
}
5179
}

0 commit comments

Comments
 (0)