Completed
Pull Request — master (#143)
by Ian
03:27 queued 01:47
created

PermissionServiceProvider::booted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Permission;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\View\Compilers\BladeCompiler;
7
use Spatie\Permission\Contracts\Permission as PermissionContract;
8
use Spatie\Permission\Contracts\Role as RoleContract;
9
10
class PermissionServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Bootstrap the application services.
14
     *
15
     */
16
    public function boot()
17
    {
18
        $this->publishes([
19
            __DIR__.'/../resources/config/laravel-permission.php' => $this->app->configPath().'/'.'laravel-permission.php',
20
        ], 'config');
21
22
        if (!class_exists('CreatePermissionTables')) {
23
            // Publish the migration
24
            $timestamp = date('Y_m_d_His', time());
25
            $this->publishes([
26
                __DIR__.'/../resources/migrations/create_permission_tables.php.stub' => $this->app->databasePath().'/migrations/'.$timestamp.'_create_permission_tables.php',
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
27
            ], 'migrations');
28
        }
29
    }
30
    
31
    /**
32
     * Finish configuring the application.
33
     *
34
     * @param PermissionRegistrar $permissionLoader
35
     */
36
    public function booted(PermissionRegistrar $permissionLoader)
37
    {
38
        $permissionLoader->registerPermissions();
39
    }
40
41
    /**
42
     * Register the application services.
43
     */
44
    public function register()
45
    {
46
        $this->mergeConfigFrom(__DIR__.'/../resources/config/laravel-permission.php', 'laravel-permission');
47
48
        $this->registerModelBindings();
49
50
        $this->registerBladeExtensions();
51
    }
52
53
    /**
54
     * Bind the Permission and Role model into the IoC.
55
     */
56
    protected function registerModelBindings()
57
    {
58
        $config = $this->app->config['laravel-permission.models'];
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
59
60
        $this->app->bind(PermissionContract::class, $config['permission']);
61
        $this->app->bind(RoleContract::class, $config['role']);
62
    }
63
64
    /**
65
     * Register the blade extensions.
66
     */
67
    protected function registerBladeExtensions()
68
    {
69
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
70
            $bladeCompiler->directive('role', function ($role) {
71
                return "<?php if(auth()->check() && auth()->user()->hasRole({$role})): ?>";
72
            });
73
            $bladeCompiler->directive('endrole', function () {
74
                return '<?php endif; ?>';
75
            });
76
77
            $bladeCompiler->directive('hasrole', function ($role) {
78
                return "<?php if(auth()->check() && auth()->user()->hasRole({$role})): ?>";
79
            });
80
            $bladeCompiler->directive('endhasrole', function () {
81
                return '<?php endif; ?>';
82
            });
83
84
            $bladeCompiler->directive('hasanyrole', function ($roles) {
85
                return "<?php if(auth()->check() && auth()->user()->hasAnyRole({$roles})): ?>";
86
            });
87
            $bladeCompiler->directive('endhasanyrole', function () {
88
                return '<?php endif; ?>';
89
            });
90
91
            $bladeCompiler->directive('hasallroles', function ($roles) {
92
                return "<?php if(auth()->check() && auth()->user()->hasAllRoles({$roles})): ?>";
93
            });
94
            $bladeCompiler->directive('endhasallroles', function () {
95
                return '<?php endif; ?>';
96
            });
97
        });
98
    }
99
}
100