|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Auth\Database\Menu; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Routing\Route; |
|
8
|
|
|
use Illuminate\Routing\Router; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
|
|
11
|
|
|
class GenerateMenuCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Router |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $router; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The name and signature of the console command. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $signature = 'admin:generate-menu {--dry-run : Dry run}'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The console command description. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $description = 'Generate menu items based on registered routes.'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Create a new command instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @param Router $router |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Router $router) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::__construct(); |
|
40
|
|
|
|
|
41
|
|
|
$this->router = $router; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Execute the console command. |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
public function handle() |
|
50
|
|
|
{ |
|
51
|
|
|
$routes = collect($this->router->getRoutes())->filter(function (Route $route) { |
|
52
|
|
|
$uri = $route->uri(); |
|
53
|
|
|
// built-in, parameterized and no-GET are ignored |
|
54
|
|
|
return Str::startsWith($uri, 'admin/') |
|
55
|
|
|
&& !Str::startsWith($uri, 'admin/auth/') |
|
56
|
|
|
&& !Str::endsWith($uri, '/create') |
|
57
|
|
|
&& !Str::contains($uri, '{') |
|
58
|
|
|
&& in_array('GET', $route->methods()); |
|
59
|
|
|
}) |
|
60
|
|
|
->map(function (Route $route) { |
|
61
|
|
|
$uri = substr($route->uri(), strlen('admin/')); |
|
62
|
|
|
|
|
63
|
|
|
return [ |
|
64
|
|
|
'title' => Str::ucfirst( |
|
65
|
|
|
Str::snake(str_replace('/', ' ', $uri), ' ') |
|
66
|
|
|
), |
|
67
|
|
|
'uri' => $uri, |
|
68
|
|
|
]; |
|
69
|
|
|
}) |
|
70
|
|
|
->pluck('title', 'uri'); |
|
71
|
|
|
|
|
72
|
|
|
$menus = Menu::all()->pluck('title', 'uri'); |
|
73
|
|
|
// exclude exist ones |
|
74
|
|
|
$news = $routes->diffKeys($menus)->map(function ($item, $key) { |
|
75
|
|
|
return [ |
|
76
|
|
|
'title' => $item, |
|
77
|
|
|
'uri' => $key, |
|
78
|
|
|
'order' => 10, |
|
79
|
|
|
'icon' => 'fa-list', |
|
80
|
|
|
]; |
|
81
|
|
|
})->values()->toArray(); |
|
82
|
|
|
|
|
83
|
|
|
if (!$news) { |
|
|
|
|
|
|
84
|
|
|
$this->error('No newly registered routes found.'); |
|
85
|
|
|
} else { |
|
86
|
|
|
if ($this->hasOption('dry-run') && $this->option('dry-run')) { |
|
87
|
|
|
$this->line('<info>The following menu items will be created</info>: '); |
|
88
|
|
|
$this->table(['Title', 'Uri'], array_map(function ($item) { |
|
89
|
|
|
return [ |
|
90
|
|
|
$item['title'], |
|
91
|
|
|
$item['uri'], |
|
92
|
|
|
]; |
|
93
|
|
|
}, $news)); |
|
94
|
|
|
} else { |
|
95
|
|
|
Menu::insert($news); |
|
96
|
|
|
$this->line('<info>Done!</info>'); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.