1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Fort\Providers; |
6
|
|
|
|
7
|
|
|
use Rinvex\Fort\Models\Role; |
8
|
|
|
use Rinvex\Fort\Models\User; |
9
|
|
|
use Illuminate\Routing\Router; |
10
|
|
|
use Rinvex\Fort\Models\Ability; |
11
|
|
|
use Rinvex\Fort\Models\Session; |
12
|
|
|
use Rinvex\Fort\Models\Socialite; |
13
|
|
|
use Rinvex\Fort\Services\AccessGate; |
14
|
|
|
use Illuminate\Support\ServiceProvider; |
15
|
|
|
use Illuminate\Support\Facades\Validator; |
16
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
17
|
|
|
use Rinvex\Fort\Console\Commands\MigrateCommand; |
18
|
|
|
use Rinvex\Fort\Console\Commands\PublishCommand; |
19
|
|
|
use Rinvex\Fort\Console\Commands\RollbackCommand; |
20
|
|
|
use Rinvex\Fort\Services\PasswordResetBrokerManager; |
21
|
|
|
use Rinvex\Fort\Services\EmailVerificationBrokerManager; |
22
|
|
|
use Illuminate\Contracts\Auth\Access\Gate as GateContract; |
23
|
|
|
|
24
|
|
|
class FortServiceProvider extends ServiceProvider |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The commands to be registered. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $commands = [ |
32
|
|
|
MigrateCommand::class => 'command.rinvex.fort.migrate', |
33
|
|
|
PublishCommand::class => 'command.rinvex.fort.publish', |
34
|
|
|
RollbackCommand::class => 'command.rinvex.fort.rollback', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function register() |
41
|
|
|
{ |
42
|
|
|
// Register bindings |
43
|
|
|
$this->registerPasswordBroker(); |
44
|
|
|
$this->registerVerificationBroker(); |
45
|
|
|
|
46
|
|
|
// Register console commands |
47
|
|
|
! $this->app->runningInConsole() || $this->registerCommands(); |
48
|
|
|
|
49
|
|
|
// Merge config |
50
|
|
|
$this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'rinvex.fort'); |
51
|
|
|
|
52
|
|
|
// Register Access Gate Binding |
53
|
|
|
$this->registerAccessGate(); |
54
|
|
|
|
55
|
|
|
// Bind eloquent models to IoC container |
56
|
|
|
$this->app->singleton('rinvex.fort.role', $roleModel = $this->app['config']['rinvex.fort.models.role']); |
57
|
|
|
$roleModel === Role::class || $this->app->alias('rinvex.fort.role', Role::class); |
58
|
|
|
|
59
|
|
|
$this->app->singleton('rinvex.fort.ability', $abilityModel = $this->app['config']['rinvex.fort.models.ability']); |
|
|
|
|
60
|
|
|
$abilityModel === Ability::class || $this->app->alias('rinvex.fort.ability', Ability::class); |
61
|
|
|
|
62
|
|
|
$this->app->singleton('rinvex.fort.session', $sessionModel = $this->app['config']['rinvex.fort.models.session']); |
|
|
|
|
63
|
|
|
$sessionModel === Session::class || $this->app->alias('rinvex.fort.session', Session::class); |
64
|
|
|
|
65
|
|
|
$this->app->singleton('rinvex.fort.socialite', $socialiteModel = $this->app['config']['rinvex.fort.models.socialite']); |
|
|
|
|
66
|
|
|
$socialiteModel === Socialite::class || $this->app->alias('rinvex.fort.socialite', Socialite::class); |
67
|
|
|
|
68
|
|
|
$this->app->singleton('rinvex.fort.user', $userModel = $this->app['config']['auth.providers.'.$this->app['config']['auth.guards.'.$this->app['config']['auth.defaults.guard'].'.provider'].'.model']); |
|
|
|
|
69
|
|
|
$userModel === User::class || $this->app->alias('rinvex.fort.user', User::class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Register the access gate service. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
protected function registerAccessGate(): void |
78
|
|
|
{ |
79
|
|
|
$this->app->singleton(GateContract::class, function ($app) { |
80
|
|
|
return new AccessGate($app, function () use ($app) { |
81
|
|
|
return call_user_func($app['auth']->userResolver()); |
82
|
|
|
}); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function boot(Router $router) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
// Add country validation rule |
92
|
|
|
Validator::extend('country', function ($attribute, $value) { |
93
|
|
|
return in_array($value, array_keys(countries())); |
94
|
|
|
}, 'Country MUST be valid!'); |
95
|
|
|
|
96
|
|
|
// Add langauge validation rule |
97
|
|
|
Validator::extend('language', function ($attribute, $value) { |
98
|
|
|
return in_array($value, array_keys(languages())); |
99
|
|
|
}, 'Language MUST be valid!'); |
100
|
|
|
|
101
|
|
|
// Publish resources |
102
|
|
|
! $this->app->runningInConsole() || $this->publishResources(); |
103
|
|
|
|
104
|
|
|
// Share current user instance with all views |
105
|
|
|
$this->app['view']->composer('*', function ($view) { |
106
|
|
|
$view->with('currentUser', auth()->user()); |
|
|
|
|
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
// Register blade extensions |
110
|
|
|
$this->registerBladeExtensions(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Publish resources. |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
protected function publishResources(): void |
119
|
|
|
{ |
120
|
|
|
$this->publishes([realpath(__DIR__.'/../../config/config.php') => config_path('rinvex.fort.php')], 'rinvex-fort-config'); |
|
|
|
|
121
|
|
|
$this->publishes([realpath(__DIR__.'/../../database/migrations') => database_path('migrations')], 'rinvex-fort-migrations'); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Register the password broker. |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
protected function registerPasswordBroker(): void |
130
|
|
|
{ |
131
|
|
|
$this->app->singleton('auth.password', function ($app) { |
132
|
|
|
return new PasswordResetBrokerManager($app); |
133
|
|
|
}); |
134
|
|
|
|
135
|
|
|
$this->app->bind('auth.password.broker', function ($app) { |
136
|
|
|
return $app->make('auth.password')->broker(); |
137
|
|
|
}); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Register the verification broker. |
142
|
|
|
* |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
protected function registerVerificationBroker(): void |
146
|
|
|
{ |
147
|
|
|
$this->app->singleton('rinvex.fort.emailverification', function ($app) { |
148
|
|
|
return new EmailVerificationBrokerManager($app); |
149
|
|
|
}); |
150
|
|
|
|
151
|
|
|
$this->app->bind('rinvex.fort.emailverification.broker', function ($app) { |
152
|
|
|
return $app->make('rinvex.fort.emailverification')->broker(); |
153
|
|
|
}); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Register the blade extensions. |
158
|
|
|
* |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
protected function registerBladeExtensions(): void |
162
|
|
|
{ |
163
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
164
|
|
|
|
165
|
|
|
// @hasAnyRoles(['writer', 'editor']) |
166
|
|
|
$bladeCompiler->directive('hasAnyRoles', function ($expression) { |
167
|
|
|
return "<?php if(auth()->user()->hasAnyRoles({$expression})): ?>"; |
168
|
|
|
}); |
169
|
|
|
$bladeCompiler->directive('endHasAnyRoles', function () { |
170
|
|
|
return '<?php endif; ?>'; |
171
|
|
|
}); |
172
|
|
|
|
173
|
|
|
// @hasAllRoles(['writer', 'editor']) |
174
|
|
|
$bladeCompiler->directive('hasAllRoles', function ($expression) { |
175
|
|
|
return "<?php if(auth()->user()->hasAllRoles({$expression})): ?>"; |
176
|
|
|
}); |
177
|
|
|
$bladeCompiler->directive('endHasAllRoles', function () { |
178
|
|
|
return '<?php endif; ?>'; |
179
|
|
|
}); |
180
|
|
|
}); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Register console commands. |
185
|
|
|
* |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
protected function registerCommands(): void |
189
|
|
|
{ |
190
|
|
|
// Register artisan commands |
191
|
|
|
foreach ($this->commands as $key => $value) { |
192
|
|
|
$this->app->singleton($value, $key); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->commands(array_values($this->commands)); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.