Completed
Push — master ( 38f186...df7470 )
by Song
25s queued 10s
created

GenerateMenuCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $news of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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