Completed
Pull Request — master (#823)
by Burak
02:27
created

PermissionServiceProvider::boot()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\View\Compilers\BladeCompiler;
7
use Spatie\Permission\Contracts\Role as RoleContract;
8
use Spatie\Permission\Contracts\Permission as PermissionContract;
9
10
class PermissionServiceProvider extends ServiceProvider
11
{
12
    public function boot(PermissionRegistrar $permissionLoader)
13
    {
14
        if (isNotLumen()) {
15
            $this->publishes([
16
                __DIR__.'/../config/permission.php' => config_path('permission.php'),
17
            ], 'config');
18
19
            if (! class_exists('CreatePermissionTables')) {
20
                $timestamp = date('Y_m_d_His', time());
21
22
                $this->publishes([
23
                    __DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_permission_tables.php",
24
                ], 'migrations');
25
            }
26
        }
27
28
        if ($this->app->runningInConsole()) {
29
            $this->commands([[
30
                Commands\AssignRole::class,
31
                Commands\CreateRole::class,
32
                Commands\CreatePermission::class,
33
                Commands\GivePermissionUser::class,
34
            ]);
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')', expecting ']'
Loading history...
35
        }
36
37
        $this->registerModelBindings();
38
39
        $permissionLoader->registerPermissions();
40
    }
41
42
    public function register()
43
    {
44
        if (isNotLumen()) {
45
            $this->mergeConfigFrom(
46
                __DIR__.'/../config/permission.php',
47
                'permission'
48
            );
49
        }
50
51
        $this->registerBladeExtensions();
52
    }
53
54
    protected function registerModelBindings()
55
    {
56
        $config = $this->app->config['permission.models'];
57
58
        $this->app->bind(PermissionContract::class, $config['permission']);
59
        $this->app->bind(RoleContract::class, $config['role']);
60
    }
61
62
    protected function registerBladeExtensions()
63
    {
64
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
65
            $bladeCompiler->directive('role', function ($arguments) {
66
                list($role, $guard) = explode(',', $arguments.',');
67
68
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
69
            });
70
            $bladeCompiler->directive('endrole', function () {
71
                return '<?php endif; ?>';
72
            });
73
74
            $bladeCompiler->directive('hasrole', function ($arguments) {
75
                list($role, $guard) = explode(',', $arguments.',');
76
77
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
78
            });
79
            $bladeCompiler->directive('endhasrole', function () {
80
                return '<?php endif; ?>';
81
            });
82
83
            $bladeCompiler->directive('hasanyrole', function ($arguments) {
84
                list($roles, $guard) = explode(',', $arguments.',');
85
86
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
87
            });
88
            $bladeCompiler->directive('endhasanyrole', function () {
89
                return '<?php endif; ?>';
90
            });
91
92
            $bladeCompiler->directive('hasallroles', function ($arguments) {
93
                list($roles, $guard) = explode(',', $arguments.',');
94
95
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
96
            });
97
            $bladeCompiler->directive('endhasallroles', function () {
98
                return '<?php endif; ?>';
99
            });
100
        });
101
    }
102
}
103