|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Fort\Providers; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\ServiceProvider; |
|
8
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
|
9
|
|
|
use Rinvex\Fort\Console\Commands\SeedCommand; |
|
10
|
|
|
use Rinvex\Fort\Console\Commands\MigrateCommand; |
|
11
|
|
|
use Rinvex\Fort\Console\Commands\PublishCommand; |
|
12
|
|
|
use Rinvex\Fort\Console\Commands\MakeAuthCommand; |
|
13
|
|
|
use Rinvex\Fort\Console\Commands\RollbackCommand; |
|
14
|
|
|
use Rinvex\Fort\Services\PasswordResetBrokerManager; |
|
15
|
|
|
use Rinvex\Fort\Services\EmailVerificationBrokerManager; |
|
16
|
|
|
|
|
17
|
|
|
class FortDeferredServiceProvider extends ServiceProvider |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* The commands to be registered. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $commands = [ |
|
25
|
|
|
SeedCommand::class => 'command.rinvex.fort.seed', |
|
26
|
|
|
MigrateCommand::class => 'command.rinvex.fort.migrate', |
|
27
|
|
|
PublishCommand::class => 'command.rinvex.fort.publish', |
|
28
|
|
|
RollbackCommand::class => 'command.rinvex.fort.rollback', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Indicates if loading of the provider is deferred. |
|
33
|
|
|
* |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $defer = true; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function register() |
|
42
|
|
|
{ |
|
43
|
|
|
// Register bindings |
|
44
|
|
|
$this->registerPasswordBroker(); |
|
45
|
|
|
$this->registerVerificationBroker(); |
|
46
|
|
|
|
|
47
|
|
|
// Register console commands |
|
48
|
|
|
! $this->app->runningInConsole() || $this->registerCommands(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Bootstrap the application events. |
|
53
|
|
|
* |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
|
|
public function boot() |
|
57
|
|
|
{ |
|
58
|
|
|
// Register blade extensions |
|
59
|
|
|
$this->registerBladeExtensions(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Register the password broker. |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function registerPasswordBroker() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->app->singleton('auth.password', function ($app) { |
|
70
|
|
|
return new PasswordResetBrokerManager($app); |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
$this->app->bind('auth.password.broker', function ($app) { |
|
74
|
|
|
return $app->make('auth.password')->broker(); |
|
75
|
|
|
}); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Register the verification broker. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function registerVerificationBroker() |
|
84
|
|
|
{ |
|
85
|
|
|
$this->app->singleton('rinvex.fort.emailverification', function ($app) { |
|
86
|
|
|
return new EmailVerificationBrokerManager($app); |
|
87
|
|
|
}); |
|
88
|
|
|
|
|
89
|
|
|
$this->app->bind('rinvex.fort.emailverification.broker', function ($app) { |
|
90
|
|
|
return $app->make('rinvex.fort.emailverification')->broker(); |
|
91
|
|
|
}); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Register the blade extensions. |
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function registerBladeExtensions() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
|
102
|
|
|
|
|
103
|
|
|
// @role('writer') / @hasrole(['writer', 'editor']) |
|
104
|
|
|
$bladeCompiler->directive('role', function ($expression) { |
|
105
|
|
|
return "<?php if(auth()->user()->hasRole({$expression})): ?>"; |
|
106
|
|
|
}); |
|
107
|
|
|
$bladeCompiler->directive('endrole', function () { |
|
108
|
|
|
return '<?php endif; ?>'; |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
// @hasrole('writer') / @hasrole(['writer', 'editor']) |
|
112
|
|
|
$bladeCompiler->directive('hasrole', function ($expression) { |
|
113
|
|
|
return "<?php if(auth()->user()->hasRole({$expression})): ?>"; |
|
114
|
|
|
}); |
|
115
|
|
|
$bladeCompiler->directive('endhasrole', function () { |
|
116
|
|
|
return '<?php endif; ?>'; |
|
117
|
|
|
}); |
|
118
|
|
|
|
|
119
|
|
|
// @hasanyrole(['writer', 'editor']) |
|
120
|
|
|
$bladeCompiler->directive('hasanyrole', function ($expression) { |
|
121
|
|
|
return "<?php if(auth()->user()->hasAnyRole({$expression})): ?>"; |
|
122
|
|
|
}); |
|
123
|
|
|
$bladeCompiler->directive('endhasanyrole', function () { |
|
124
|
|
|
return '<?php endif; ?>'; |
|
125
|
|
|
}); |
|
126
|
|
|
|
|
127
|
|
|
// @hasallroles(['writer', 'editor']) |
|
128
|
|
|
$bladeCompiler->directive('hasallroles', function ($expression) { |
|
129
|
|
|
return "<?php if(auth()->user()->hasAllRoles({$expression})): ?>"; |
|
130
|
|
|
}); |
|
131
|
|
|
$bladeCompiler->directive('endhasallroles', function () { |
|
132
|
|
|
return '<?php endif; ?>'; |
|
133
|
|
|
}); |
|
134
|
|
|
}); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get the services provided by the provider. |
|
139
|
|
|
* |
|
140
|
|
|
* @return array |
|
|
|
|
|
|
141
|
|
|
*/ |
|
142
|
|
|
public function provides() |
|
143
|
|
|
{ |
|
144
|
|
|
return [ |
|
145
|
|
|
'auth.password', |
|
146
|
|
|
'rinvex.fort.emailverification', |
|
147
|
|
|
\Illuminate\Contracts\Debug\ExceptionHandler::class, |
|
148
|
|
|
]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Register console commands. |
|
153
|
|
|
* |
|
154
|
|
|
* @return void |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function registerCommands() |
|
157
|
|
|
{ |
|
158
|
|
|
if (config('rinvex.fort.boot.override_makeauth_command')) { |
|
159
|
|
|
$this->app->singleton('command.auth.make', function ($app) { |
|
|
|
|
|
|
160
|
|
|
return new MakeAuthCommand(); |
|
161
|
|
|
}); |
|
162
|
|
|
$this->commands('command.auth.make'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// Register artisan commands |
|
166
|
|
|
foreach ($this->commands as $key => $value) { |
|
167
|
|
|
$this->app->singleton($value, function ($app) use ($key) { |
|
|
|
|
|
|
168
|
|
|
return new $key(); |
|
169
|
|
|
}); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$this->commands(array_values($this->commands)); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.