Completed
Push — master ( 85d653...679971 )
by Arjay
12:36
created

AclServiceProvider::getPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
ccs 0
cts 5
cp 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Yajra\Acl;
4
5
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
6
use Illuminate\View\Compilers\BladeCompiler;
7
use Yajra\Acl\Models\Permission;
8
use Yajra\Acl\Models\Role;
9
10
class AclServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Register any application authentication / authorization services.
14
     *
15
     * @param GateRegistrar $gate
16
     * @return void
17
     */
18
    public function boot(GateRegistrar $gate)
19
    {
20
        $gate->register();
21
22
        $this->publishConfig();
23
        $this->publishMigrations();
24
        $this->registerPolicies();
25
        $this->registerCacheListener();
26
        $this->registerBladeDirectives();
27
    }
28
29
    /**
30
     * Publish package config file.
31
     */
32
    protected function publishConfig()
33
    {
34
        $this->publishes([
35
            __DIR__ . '/../config/acl.php' => config_path('acl.php'),
36
        ], 'laravel-acl');
37
    }
38
39
    /**
40
     * Publish package migration files.
41
     */
42
    protected function publishMigrations()
43
    {
44
        $this->loadMigrationsFrom(__DIR__ . '/../migrations');
45
        $this->publishes([
46
            __DIR__ . '/../migrations' => database_path('migrations'),
47
        ], 'laravel-acl');
48
    }
49
50
    /**
51
     * Register ACL models cache listener.
52
     */
53
    protected function registerCacheListener()
54
    {
55
        Permission::saved(function () {
56
            $this->app['cache.store']->forget('permissions.policies');
57
        });
58
59
        Permission::deleted(function () {
60
            $this->app['cache.store']->forget('permissions.policies');
61
        });
62
63
        Role::saved(function () {
64
            $this->app['cache.store']->forget('permissions.policies');
65
        });
66
67
        Role::deleted(function () {
68
            $this->app['cache.store']->forget('permissions.policies');
69
        });
70
    }
71
72
    /**
73
     * Register custom blade directives.
74
     */
75
    protected function registerBladeDirectives()
76
    {
77
        /** @var BladeCompiler $blade */
78
        $blade = $this->app['blade.compiler'];
79
        $blade->directive('canAtLeast', function ($expression) {
80
            return "<?php if (app('laravel-acl.directives.canAtLeast')->handle({$expression})): ?>";
81
        });
82
        $blade->directive('endCanAtLeast', function ($expression) {
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
            return '<?php endif; ?>';
84
        });
85
86
        /** @deprecated Please use @role directive. */
87
        $blade->directive('isRole', function ($expression) {
88
            return "<?php if (app('laravel-acl.directives.role')->handle({$expression})): ?>";
89
        });
90
        $blade->directive('endIsRole', function ($expression) {
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
            return '<?php endif; ?>';
92
        });
93
94
        $blade->directive('role', function ($expression) {
95
            return "<?php if (app('laravel-acl.directives.role')->handle({$expression})): ?>";
96
        });
97
        $blade->directive('endRole', function ($expression) {
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
            return '<?php endif; ?>';
99
        });
100
101
        $blade->directive('hasRole', function ($expression) {
102
            return "<?php if (app('laravel-acl.directives.hasRole')->handle({$expression})): ?>";
103
        });
104
        $blade->directive('endHasRole', function ($expression) {
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
            return '<?php endif; ?>';
106
        });
107
    }
108
109
    /**
110
     * Register providers.
111
     */
112
    public function register()
113
    {
114
        $this->app->singleton('laravel-acl.directives.canAtLeast', Directives\CanAtLeastDirective::class);
115
        $this->app->singleton('laravel-acl.directives.role', Directives\RoleDirective::class);
116
        $this->app->singleton('laravel-acl.directives.hasRole', Directives\HasRoleDirective::class);
117
    }
118
}
119