Completed
Push — master ( f9e78a...edb43e )
by Abdelrahman
09:22
created

FortDeferredServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Providers;
17
18
use Collective\Html\FormFacade;
19
use Collective\Html\HtmlFacade;
20
use Rinvex\Fort\Services\AccessGate;
21
use Illuminate\Foundation\AliasLoader;
22
use Rinvex\Fort\Services\BrokerManager;
23
use Illuminate\Support\ServiceProvider;
24
use Collective\Html\HtmlServiceProvider;
25
use Illuminate\View\Compilers\BladeCompiler;
26
use Laravel\Socialite\SocialiteServiceProvider;
27
use Rinvex\Fort\Console\Commands\UserFindCommand;
28
use Rinvex\Fort\Console\Commands\RoleFindCommand;
29
use Rinvex\Fort\Console\Commands\RoleCreateCommand;
30
use Rinvex\Fort\Console\Commands\RoleUpdateCommand;
31
use Rinvex\Fort\Console\Commands\UserCreateCommand;
32
use Rinvex\Fort\Console\Commands\UserRemindCommand;
33
use Rinvex\Fort\Console\Commands\UserUpdateCommand;
34
use Rinvex\Fort\Console\Commands\AbilityFindCommand;
35
use Rinvex\Fort\Console\Commands\AbilityCreateCommand;
36
use Rinvex\Fort\Console\Commands\AbilityUpdateCommand;
37
use Rinvex\Fort\Console\Commands\UserAssignRoleCommand;
38
use Rinvex\Fort\Console\Commands\UserRemoveRoleCommand;
39
use Rinvex\Fort\Console\Commands\UserGiveAbilityCommand;
40
use Rinvex\Fort\Console\Commands\RoleGiveAbilityCommand;
41
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
42
use Rinvex\Fort\Console\Commands\RoleRevokeAbilityCommand;
43
use Rinvex\Fort\Console\Commands\UserRevokeAbilityCommand;
44
use Rinvex\Fort\Console\Commands\PasswordTokenClearCommand;
45
use Rinvex\Fort\Console\Commands\VerificationTokenClearCommand;
46
47
class FortDeferredServiceProvider extends ServiceProvider
48
{
49
    /**
50
     * Indicates if loading of the provider is deferred.
51
     *
52
     * @var bool
53
     */
54
    protected $defer = true;
55
56
    /**
57
     * The commands to be registered.
58
     *
59
     * @var array
60
     */
61
    protected $commands = [
62
63
        AbilityFindCommand::class,
64
        AbilityUpdateCommand::class,
65
        AbilityCreateCommand::class,
66
67
        RoleFindCommand::class,
68
        RoleUpdateCommand::class,
69
        RoleCreateCommand::class,
70
        RoleGiveAbilityCommand::class,
71
        RoleRevokeAbilityCommand::class,
72
73
        UserFindCommand::class,
74
        UserCreateCommand::class,
75
        UserUpdateCommand::class,
76
        UserRemindCommand::class,
77
        UserAssignRoleCommand::class,
78
        UserRemoveRoleCommand::class,
79
        UserGiveAbilityCommand::class,
80
        UserRevokeAbilityCommand::class,
81
82
        VerificationTokenClearCommand::class,
83
        PasswordTokenClearCommand::class,
84
85
    ];
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function register()
91
    {
92
        // Merge config
93
        $this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'rinvex.fort');
94
95
        // Register artisan commands
96
        $this->commands($this->commands);
97
98
        // Register bindings
99
        $this->registerAccessGate();
100
        $this->registerBrokerManagers();
101
        $this->registerBladeExtensions();
102
103
        // Register the Socialite Service Provider
104
        $this->app->register(SocialiteServiceProvider::class);
105
106
        // Register the LaravelCollective HTML Service Provider
107
        $this->app->register(HtmlServiceProvider::class);
108
109
        // Alias the LaravelCollective Form & HTML Facades
110
        AliasLoader::getInstance()->alias('Form', FormFacade::class);
111
        AliasLoader::getInstance()->alias('Html', HtmlFacade::class);
112
    }
113
114
    /**
115
     * Register the access gate service.
116
     *
117
     * @return void
118
     */
119
    protected function registerAccessGate()
120
    {
121
        $this->app->singleton(GateContract::class, function ($app) {
122
            return new AccessGate($app, function () use ($app) {
123
                return call_user_func($app['auth']->userResolver());
124
            });
125
        });
126
    }
127
128
    /**
129
     * Register the broker managers.
130
     *
131
     * @return void
132
     */
133
    protected function registerBrokerManagers()
134
    {
135
        // Register reset broker manager
136
        $this->app->singleton('rinvex.fort.passwordreset', function ($app) {
137
            return new BrokerManager($app, 'PasswordReset');
138
        });
139
140
        // Register verification broker manager
141
        $this->app->singleton('rinvex.fort.emailverification', function ($app) {
142
            return new BrokerManager($app, 'EmailVerification');
143
        });
144
    }
145
146
    /**
147
     * Register the blade extensions.
148
     *
149
     * @return void
150
     */
151
    protected function registerBladeExtensions()
152
    {
153
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
154
155
            // @role('writer') / @hasrole(['writer', 'editor'])
156
            $bladeCompiler->directive('role', function ($roles) {
157
                return "<?php if(auth()->user()->hasRole({$roles})): ?>";
158
            });
159
            $bladeCompiler->directive('endrole', function () {
160
                return '<?php endif; ?>';
161
            });
162
163
            // @hasrole('writer') / @hasrole(['writer', 'editor'])
164
            $bladeCompiler->directive('hasrole', function ($roles) {
165
                return "<?php if(auth()->user()->hasRole({$roles})): ?>";
166
            });
167
            $bladeCompiler->directive('endhasrole', function () {
168
                return '<?php endif; ?>';
169
            });
170
171
            // @hasanyrole(['writer', 'editor'])
172
            $bladeCompiler->directive('hasanyrole', function ($roles) {
173
                return "<?php if(auth()->user()->hasAnyRole({$roles})): ?>";
174
            });
175
            $bladeCompiler->directive('endhasanyrole', function () {
176
                return '<?php endif; ?>';
177
            });
178
179
            // @hasallroles(['writer', 'editor'])
180
            $bladeCompiler->directive('hasallroles', function ($roles) {
181
                return "<?php if(auth()->user()->hasAllRoles({$roles})): ?>";
182
            });
183
            $bladeCompiler->directive('endhasallroles', function () {
184
                return '<?php endif; ?>';
185
            });
186
        });
187
    }
188
189
    /**
190
     * Get the services provided by the provider.
191
     *
192
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<integer|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
193
     */
194
    public function provides()
195
    {
196
        return array_keys($this->commands) + [
197
                GateContract::class,
198
                'rinvex.fort.passwordreset',
199
                'rinvex.fort.emailverification',
200
                \Illuminate\Contracts\Debug\ExceptionHandler::class,
201
            ];
202
    }
203
}
204