Completed
Pull Request — master (#785)
by
unknown
01:36
created

SyncPermissions::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Route;
7
use Illuminate\Routing\Route as Router;
8
9
10
use Spatie\Permission\Models\Permission;
11
12
class SyncPermissions extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'sync:permission';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'this commend will read all of the routes and add them to permissions';
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @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...
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        // if you want to exclude some of the routes add here
46
        $exept=collect(["register"=>"GET|HEAD",
47
      "register"=>"POST",
48
      "password.request"=>"GET|HEAD",
49
      "password.email"=>"POST",
50
      "password.reset"=>"GET|HEAD",
51
      "password.reset"=>"POST",
52
      "login"=>"GET|HEAD",
53
      "logout"=>"GET|HEAD",
54
    ]);
55
        $routes = collect(Route::getRoutes())->map(function ($route) {
56
            return [
57
                'host'   => $route->domain(),
58
                'method' => implode('|', $route->methods()),
59
                'uri'    => $route->uri(),
60
                'name'   => $route->getName(),
61
                'action' => $route->getActionName(),
62
            ];
63
        });
64
        $bar = $this->output->createProgressBar(count($routes));
65
66
        foreach ($routes as $route) {
67
            if (!isset($exept[$route['name']]) && $route['name']!=null) {
68
                Permission::firstOrCreate(['name' => $route['name']]);
69
            }
70
            $bar->advance();
71
        }
72
        $bar->finish();
73
    }
74
}
75