Completed
Push — master ( c8a423...a23ea0 )
by Song
02:21
created

src/Console/PermissionCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Console;
4
5
use Encore\Admin\Auth\Database\Permission;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Str;
9
10
class PermissionCommand extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'admin:permissions {--tables=}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'generate admin permission base on table name';
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        $all_tables = $this->getAllTables();
44
45
        $tables = $this->option('tables') ? explode(',', $this->option('tables')) : [];
46
        if (empty($tables)) {
47
            $ignore_tables = $this->getIgnoreTables();
48
            $tables = array_diff($all_tables, $ignore_tables);
49
        } else {
50
            $tables = array_intersect($all_tables, $tables);
51
        }
52
53
        if (empty($tables)) {
54
            $this->info('table is not existed');
55
56
            return;
57
        }
58
59
        $permissions = $this->getPermissions();
60
        foreach ($tables as $table) {
61
            foreach ($permissions as $permission => $permission_lang) {
62
                $http_method = $this->generateHttpMethod($permission);
63
                $http_path = $this->generateHttpPath($table, $permission);
64
                $slug = $this->generateSlug($table, $permission);
65
                $name = $this->generateName($table, $permission_lang);
66
                $exists = Permission::where('slug', $slug)->exists();
67
                if (!$exists) {
68
                    Permission::create([
69
                        'name'        => $name,
70
                        'slug'        => $slug,
71
                        'http_method' => $http_method,
72
                        'http_path'   => $http_path,
73
                    ]);
74
                    $this->info("$slug is generated");
75
                } else {
76
                    $this->warn("$slug is existed");
77
                }
78
            }
79
        }
80
    }
81
82
    private function getAllTables()
83
    {
84
        return array_map('current', DB::select('SHOW TABLES'));
85
    }
86
87
    private function getIgnoreTables()
88
    {
89
        return [
90
            config('admin.database.users_table'),
91
            config('admin.database.roles_table'),
92
            config('admin.database.permissions_table'),
93
            config('admin.database.menu_table'),
94
            config('admin.database.operation_log_table'),
95
            config('admin.database.user_permissions_table'),
96
            config('admin.database.role_users_table'),
97
            config('admin.database.role_permissions_table'),
98
            config('admin.database.role_menu_table'),
99
        ];
100
    }
101
102
    private function getPermissions()
103
    {
104
        return [
105
            'list'   => __('admin.list'),
106
            'view'   => __('admin.view'),
107
            'create' => __('admin.create'),
108
            'edit'   => __('admin.edit'),
109
            'delete' => __('admin.delete'),
110
            'export' => __('admin.export'),
111
            'filter' => __('admin.filter'),
112
        ];
113
    }
114
115
    private function generateHttpMethod($permission)
116
    {
117
        switch ($permission) {
118
            case 'create':
119
                $http_method = ['POST'];
120
                break;
121
            case 'edit':
122
                $http_method = ['PUT', 'PATCH'];
123
                break;
124
            case 'delete':
125
                $http_method = ['DELETE'];
126
                break;
127
            case 'filter':
128
                $http_method = [];
129
                break;
130
            default:
131
                $http_method = ['GET'];
132
        }
133
134
        return $http_method;
135
    }
136
137
    private function generateHttpPath($table, $permission)
138
    {
139
        $resource = Str::kebab(Str::camel($table));
140
        switch ($permission) {
141
            case 'create':
142
                $http_path = '/'.$resource;
143
                break;
144
            case 'edit':
145
                $http_path = '/'.$resource.'/*';
146
                break;
147
            case 'delete':
148
                $http_path = '/'.$resource.'/*';
149
                break;
150
            case 'index':
151
                $http_path = '/'.$resource;
152
                break;
153
            case 'view':
154
                $http_path = '/'.$resource.'/*';
155
                break;
156
            default:
157
                $http_path = '';
158
        }
159
160
        return $http_path;
161
    }
162
163
    private function generateSlug($table, $permission)
164
    {
165
        return Str::kebab(Str::camel($table)).'.'.$permission;
166
    }
167
168
    private function generateName($table, $permission_lang)
169
    {
170
        return Str::upper(Str::kebab(Str::camel($table))).$permission_lang;
171
    }
172
}
173