Test Setup Failed
Pull Request — master (#34)
by Jeremy
04:44
created

RolesServiceProvider::loadSeedsFrom()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 16
nop 0
dl 0
loc 23
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
namespace jeremykenedy\LaravelRoles;
4
5
use Illuminate\Support\ServiceProvider;
6
use jeremykenedy\LaravelRoles\Database\Seeds\DefaultConnectRelationshipsSeeder;
7
use jeremykenedy\LaravelRoles\Database\Seeds\DefaultPermissionsTableSeeder;
8
use jeremykenedy\LaravelRoles\Database\Seeds\DefaultRolesTableSeeder;
9
use jeremykenedy\LaravelRoles\Database\Seeds\DefaultUsersTableSeeder;
10
11
class RolesServiceProvider extends ServiceProvider
12
{
13
    private $_packageTag = 'laravelroles';
14
15
    /**
16
     * Indicates if loading of the provider is deferred.
17
     *
18
     * @var bool
19
     */
20
    protected $defer = false;
21
22
    /**
23
     * Bootstrap any application services.
24
     *
25
     * @return void
26
     */
27
    public function boot()
28
    {
29
        $this->registerBladeExtensions();
30
    }
31
32
    /**
33
     * Register any application services.
34
     *
35
     * @return void
36
     */
37
    public function register()
38
    {
39
        $this->mergeConfigFrom(__DIR__.'/../config/roles.php', 'roles');
40
        $this->loadMigrationsFrom(__DIR__.'/Database/Migrations');
41
        $this->publishFiles();
42
        $this->loadSeedsFrom();
43
    }
44
45
    /**
46
     * Loads a seeds.
47
     *
48
     * @return void
49
     */
50
    private function loadSeedsFrom()
51
    {
52
        if (config('roles.defaultSeeds.PermissionsTableSeeder')) {
53
            $this->app['seed.handler']->register(
54
                DefaultPermissionsTableSeeder::class
55
            );
56
        }
57
58
        if (config('roles.defaultSeeds.RolesTableSeeder')) {
59
            $this->app['seed.handler']->register(
60
                DefaultRolesTableSeeder::class
61
            );
62
        }
63
64
        if (config('roles.defaultSeeds.ConnectRelationshipsSeeder')) {
65
            $this->app['seed.handler']->register(
66
                DefaultConnectRelationshipsSeeder::class
67
            );
68
        }
69
70
        if (config('roles.defaultSeeds.UsersTableSeeder')) {
71
            $this->app['seed.handler']->register(
72
                DefaultUsersTableSeeder::class
73
            );
74
        }
75
    }
76
77
    /**
78
     * Publish files for package.
79
     *
80
     * @return void
81
     */
82
    private function publishFiles()
83
    {
84
        $publishTag = $this->_packageTag;
85
86
        $this->publishes([
87
            __DIR__.'/../config/roles.php' => config_path('roles.php'),
88
        ], $publishTag.'-config');
89
90
        $this->publishes([
91
            __DIR__.'/Database/Migrations' => database_path('migrations'),
92
        ], $publishTag.'-migrations');
93
94
        $this->publishes([
95
            __DIR__.'/Database/Seeds/publish' => database_path('seeds'),
96
        ], $publishTag.'-seeds');
97
98
        $this->publishes([
99
            __DIR__.'/../config/roles.php'    => config_path('roles.php'),
100
            __DIR__.'/Database/Migrations'    => database_path('migrations'),
101
            __DIR__.'/Database/Seeds/publish' => database_path('seeds'),
102
        ], $publishTag);
103
    }
104
105
    /**
106
     * Register Blade extensions.
107
     *
108
     * @return void
109
     */
110
    protected function registerBladeExtensions()
111
    {
112
        $blade = $this->app['view']->getEngineResolver()->resolve('blade')->getCompiler();
113
114
        $blade->directive('role', function ($expression) {
115
            return "<?php if (Auth::check() && Auth::user()->hasRole({$expression})): ?>";
116
        });
117
118
        $blade->directive('endrole', function () {
119
            return '<?php endif; ?>';
120
        });
121
122
        $blade->directive('permission', function ($expression) {
123
            return "<?php if (Auth::check() && Auth::user()->hasPermission({$expression})): ?>";
124
        });
125
126
        $blade->directive('endpermission', function () {
127
            return '<?php endif; ?>';
128
        });
129
130
        $blade->directive('level', function ($expression) {
131
            $level = trim($expression, '()');
132
133
            return "<?php if (Auth::check() && Auth::user()->level() >= {$level}): ?>";
134
        });
135
136
        $blade->directive('endlevel', function () {
137
            return '<?php endif; ?>';
138
        });
139
140
        $blade->directive('allowed', function ($expression) {
141
            return "<?php if (Auth::check() && Auth::user()->allowed({$expression})): ?>";
142
        });
143
144
        $blade->directive('endallowed', function () {
145
            return '<?php endif; ?>';
146
        });
147
    }
148
}
149