1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Http\Middleware\ThrottlePasswordRecovery; |
6
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
7
|
|
|
use Illuminate\Routing\Router; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Support\Facades\App; |
10
|
|
|
use Illuminate\Support\Facades\Route; |
11
|
|
|
use Illuminate\Support\ServiceProvider; |
12
|
|
|
|
13
|
|
|
class BackpackServiceProvider extends ServiceProvider |
14
|
|
|
{ |
15
|
|
|
use Stats, LicenseCheck; |
16
|
|
|
|
17
|
|
|
protected $commands = [ |
18
|
|
|
\Backpack\CRUD\app\Console\Commands\Install::class, |
19
|
|
|
\Backpack\CRUD\app\Console\Commands\AddSidebarContent::class, |
20
|
|
|
\Backpack\CRUD\app\Console\Commands\AddCustomRouteContent::class, |
21
|
|
|
\Backpack\CRUD\app\Console\Commands\Version::class, |
22
|
|
|
\Backpack\CRUD\app\Console\Commands\CreateUser::class, |
23
|
|
|
\Backpack\CRUD\app\Console\Commands\PublishBackpackMiddleware::class, |
24
|
|
|
\Backpack\CRUD\app\Console\Commands\PublishView::class, |
25
|
|
|
\Backpack\CRUD\app\Console\Commands\RequireDevTools::class, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
// Indicates if loading of the provider is deferred. |
29
|
|
|
protected $defer = false; |
30
|
|
|
// Where the route file lives, both inside the package and in the app (if overwritten). |
31
|
|
|
public $routeFilePath = '/routes/backpack/base.php'; |
32
|
|
|
// Where custom routes can be written, and will be registered by Backpack. |
33
|
|
|
public $customRoutesFilePath = '/routes/backpack/custom.php'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Perform post-registration booting of services. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function boot(\Illuminate\Routing\Router $router) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$this->loadViewsWithFallbacks(); |
43
|
|
|
$this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack'); |
44
|
|
|
$this->loadConfigs(); |
45
|
|
|
$this->registerMiddlewareGroup($this->app->router); |
|
|
|
|
46
|
|
|
$this->setupRoutes($this->app->router); |
47
|
|
|
$this->setupCustomRoutes($this->app->router); |
|
|
|
|
48
|
|
|
$this->publishFiles(); |
49
|
|
|
$this->checkLicenseCodeExists(); |
50
|
|
|
$this->sendUsageStats(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Register any package services. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function register() |
59
|
|
|
{ |
60
|
|
|
// Bind the CrudPanel object to Laravel's service container |
61
|
|
|
$this->app->singleton('crud', function ($app) { |
62
|
|
|
return new CrudPanel($app); |
|
|
|
|
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
// Bind the widgets collection object to Laravel's service container |
66
|
|
|
$this->app->singleton('widgets', function ($app) { |
|
|
|
|
67
|
|
|
return new Collection(); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
// load a macro for Route, |
71
|
|
|
// helps developers load all routes for a CRUD resource in one line |
72
|
|
|
if (! Route::hasMacro('crud')) { |
73
|
|
|
$this->addRouteMacro(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// register the helper functions |
77
|
|
|
$this->loadHelpers(); |
78
|
|
|
|
79
|
|
|
// register the artisan commands |
80
|
|
|
$this->commands($this->commands); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function registerMiddlewareGroup(Router $router) |
84
|
|
|
{ |
85
|
|
|
$middleware_key = config('backpack.base.middleware_key'); |
86
|
|
|
$middleware_class = config('backpack.base.middleware_class'); |
87
|
|
|
|
88
|
|
|
if (! is_array($middleware_class)) { |
89
|
|
|
$router->pushMiddlewareToGroup($middleware_key, $middleware_class); |
90
|
|
|
|
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($middleware_class as $middleware_class) { |
95
|
|
|
$router->pushMiddlewareToGroup($middleware_key, $middleware_class); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// register internal backpack middleware for throttling the password recovery functionality |
99
|
|
|
// but only if functionality is enabled by developer in config |
100
|
|
|
if (config('backpack.base.setup_password_recovery_routes')) { |
101
|
|
|
$router->aliasMiddleware('backpack.throttle.password.recovery', ThrottlePasswordRecovery::class); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function publishFiles() |
106
|
|
|
{ |
107
|
|
|
$error_views = [__DIR__.'/resources/error_views' => resource_path('views/errors')]; |
108
|
|
|
$backpack_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack')]; |
109
|
|
|
$backpack_public_assets = [__DIR__.'/public' => public_path()]; |
110
|
|
|
$backpack_lang_files = [__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')]; |
111
|
|
|
$backpack_config_files = [__DIR__.'/config' => config_path()]; |
112
|
|
|
|
113
|
|
|
// sidebar content views, which are the only views most people need to overwrite |
114
|
|
|
$backpack_menu_contents_view = [ |
115
|
|
|
__DIR__.'/resources/views/base/inc/sidebar_content.blade.php' => resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php'), |
116
|
|
|
__DIR__.'/resources/views/base/inc/topbar_left_content.blade.php' => resource_path('views/vendor/backpack/base/inc/topbar_left_content.blade.php'), |
117
|
|
|
__DIR__.'/resources/views/base/inc/topbar_right_content.blade.php' => resource_path('views/vendor/backpack/base/inc/topbar_right_content.blade.php'), |
118
|
|
|
]; |
119
|
|
|
$backpack_custom_routes_file = [__DIR__.$this->customRoutesFilePath => base_path($this->customRoutesFilePath)]; |
120
|
|
|
|
121
|
|
|
// calculate the path from current directory to get the vendor path |
122
|
|
|
$vendorPath = dirname(__DIR__, 3); |
123
|
|
|
$gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()]; |
124
|
|
|
|
125
|
|
|
// establish the minimum amount of files that need to be published, for Backpack to work; there are the files that will be published by the install command |
126
|
|
|
$minimum = array_merge( |
127
|
|
|
// $backpack_views, |
128
|
|
|
// $backpack_lang_files, |
129
|
|
|
$error_views, |
130
|
|
|
$backpack_public_assets, |
131
|
|
|
$backpack_config_files, |
132
|
|
|
$backpack_menu_contents_view, |
133
|
|
|
$backpack_custom_routes_file, |
134
|
|
|
$gravatar_assets |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
// register all possible publish commands and assign tags to each |
138
|
|
|
$this->publishes($backpack_config_files, 'config'); |
139
|
|
|
$this->publishes($backpack_lang_files, 'lang'); |
140
|
|
|
$this->publishes($backpack_views, 'views'); |
141
|
|
|
$this->publishes($backpack_menu_contents_view, 'menu_contents'); |
142
|
|
|
$this->publishes($error_views, 'errors'); |
143
|
|
|
$this->publishes($backpack_public_assets, 'public'); |
144
|
|
|
$this->publishes($backpack_custom_routes_file, 'custom_routes'); |
145
|
|
|
$this->publishes($gravatar_assets, 'gravatar'); |
146
|
|
|
$this->publishes($minimum, 'minimum'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Define the routes for the application. |
151
|
|
|
* |
152
|
|
|
* @param \Illuminate\Routing\Router $router |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function setupRoutes(Router $router) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
// by default, use the routes file provided in vendor |
158
|
|
|
$routeFilePathInUse = __DIR__.$this->routeFilePath; |
159
|
|
|
|
160
|
|
|
// but if there's a file with the same name in routes/backpack, use that one |
161
|
|
|
if (file_exists(base_path().$this->routeFilePath)) { |
162
|
|
|
$routeFilePathInUse = base_path().$this->routeFilePath; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->loadRoutesFrom($routeFilePathInUse); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Load custom routes file. |
170
|
|
|
* |
171
|
|
|
* @param \Illuminate\Routing\Router $router |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public function setupCustomRoutes(Router $router) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
// if the custom routes file is published, register its routes |
177
|
|
|
if (file_exists(base_path().$this->customRoutesFilePath)) { |
178
|
|
|
$this->loadRoutesFrom(base_path().$this->customRoutesFilePath); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* The route macro allows developers to generate the routes for a CrudController, |
184
|
|
|
* for all operations, using a simple syntax: Route::crud(). |
185
|
|
|
* |
186
|
|
|
* It will go to the given CrudController and get the setupRoutes() method on it. |
187
|
|
|
*/ |
188
|
|
|
private function addRouteMacro() |
189
|
|
|
{ |
190
|
|
|
Route::macro('crud', function ($name, $controller) { |
191
|
|
|
// put together the route name prefix, |
192
|
|
|
// as passed to the Route::group() statements |
193
|
|
|
$routeName = ''; |
194
|
|
|
if ($this->hasGroupStack()) { |
|
|
|
|
195
|
|
|
foreach ($this->getGroupStack() as $key => $groupStack) { |
|
|
|
|
196
|
|
|
if (isset($groupStack['name'])) { |
197
|
|
|
if (is_array($groupStack['name'])) { |
198
|
|
|
$routeName = implode('', $groupStack['name']); |
199
|
|
|
} else { |
200
|
|
|
$routeName = $groupStack['name']; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
// add the name of the current entity to the route name prefix |
206
|
|
|
// the result will be the current route name (not ending in dot) |
207
|
|
|
$routeName .= $name; |
208
|
|
|
|
209
|
|
|
// get an instance of the controller |
210
|
|
|
if ($this->hasGroupStack()) { |
211
|
|
|
$groupStack = $this->getGroupStack(); |
212
|
|
|
$groupNamespace = $groupStack && isset(end($groupStack)['namespace']) ? end($groupStack)['namespace'].'\\' : ''; |
213
|
|
|
} else { |
214
|
|
|
$groupNamespace = ''; |
215
|
|
|
} |
216
|
|
|
$namespacedController = $groupNamespace.$controller; |
217
|
|
|
$controllerInstance = App::make($namespacedController); |
218
|
|
|
|
219
|
|
|
return $controllerInstance->setupRoutes($name, $routeName, $controller); |
220
|
|
|
}); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function loadViewsWithFallbacks() |
224
|
|
|
{ |
225
|
|
|
$customBaseFolder = resource_path('views/vendor/backpack/base'); |
226
|
|
|
$customCrudFolder = resource_path('views/vendor/backpack/crud'); |
227
|
|
|
|
228
|
|
|
// - first the published/overwritten views (in case they have any changes) |
229
|
|
|
if (file_exists($customBaseFolder)) { |
230
|
|
|
$this->loadViewsFrom($customBaseFolder, 'backpack'); |
231
|
|
|
} |
232
|
|
|
if (file_exists($customCrudFolder)) { |
233
|
|
|
$this->loadViewsFrom($customCrudFolder, 'crud'); |
234
|
|
|
} |
235
|
|
|
// - then the stock views that come with the package, in case a published view might be missing |
236
|
|
|
$this->loadViewsFrom(realpath(__DIR__.'/resources/views/base'), 'backpack'); |
237
|
|
|
$this->loadViewsFrom(realpath(__DIR__.'/resources/views/crud'), 'crud'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
protected function mergeConfigFromOperationsDirectory() |
241
|
|
|
{ |
242
|
|
|
$operationConfigs = scandir(__DIR__.'/config/backpack/operations/'); |
243
|
|
|
$operationConfigs = array_diff($operationConfigs, ['.', '..']); |
244
|
|
|
|
245
|
|
|
if (! count($operationConfigs)) { |
246
|
|
|
return; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
foreach ($operationConfigs as $configFile) { |
250
|
|
|
$this->mergeConfigFrom( |
251
|
|
|
__DIR__.'/config/backpack/operations/'.$configFile, |
252
|
|
|
'backpack.operations.'.substr($configFile, 0, strrpos($configFile, '.')) |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function loadConfigs() |
258
|
|
|
{ |
259
|
|
|
// use the vendor configuration file as fallback |
260
|
|
|
$this->mergeConfigFrom(__DIR__.'/config/backpack/crud.php', 'backpack.crud'); |
261
|
|
|
$this->mergeConfigFrom(__DIR__.'/config/backpack/base.php', 'backpack.base'); |
262
|
|
|
$this->mergeConfigFromOperationsDirectory(); |
263
|
|
|
|
264
|
|
|
// add the root disk to filesystem configuration |
265
|
|
|
app()->config['filesystems.disks.'.config('backpack.base.root_disk_name')] = [ |
266
|
|
|
'driver' => 'local', |
267
|
|
|
'root' => base_path(), |
268
|
|
|
]; |
269
|
|
|
|
270
|
|
|
/* |
271
|
|
|
* Backpack login differs from the standard Laravel login. |
272
|
|
|
* As such, Backpack uses its own authentication provider, password broker and guard. |
273
|
|
|
* |
274
|
|
|
* THe process below adds those configuration values on top of whatever is in config/auth.php. |
275
|
|
|
* Developers can overwrite the backpack provider, password broker or guard by adding a |
276
|
|
|
* provider/broker/guard with the "backpack" name inside their config/auth.php file. |
277
|
|
|
* Or they can use another provider/broker/guard entirely, by changing the corresponding |
278
|
|
|
* value inside config/backpack/base.php |
279
|
|
|
*/ |
280
|
|
|
|
281
|
|
|
// add the backpack_users authentication provider to the configuration |
282
|
|
|
app()->config['auth.providers'] = app()->config['auth.providers'] + |
283
|
|
|
[ |
284
|
|
|
'backpack' => [ |
285
|
|
|
'driver' => 'eloquent', |
286
|
|
|
'model' => config('backpack.base.user_model_fqn'), |
287
|
|
|
], |
288
|
|
|
]; |
289
|
|
|
|
290
|
|
|
// add the backpack_users password broker to the configuration |
291
|
|
|
app()->config['auth.passwords'] = app()->config['auth.passwords'] + |
292
|
|
|
[ |
293
|
|
|
'backpack' => [ |
294
|
|
|
'provider' => 'backpack', |
295
|
|
|
'table' => 'password_resets', |
296
|
|
|
'expire' => 60, |
297
|
|
|
'throttle' => config('backpack.base.password_recovery_throttle_notifications'), |
298
|
|
|
], |
299
|
|
|
]; |
300
|
|
|
|
301
|
|
|
// add the backpack_users guard to the configuration |
302
|
|
|
app()->config['auth.guards'] = app()->config['auth.guards'] + |
303
|
|
|
[ |
304
|
|
|
'backpack' => [ |
305
|
|
|
'driver' => 'session', |
306
|
|
|
'provider' => 'backpack', |
307
|
|
|
], |
308
|
|
|
]; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Load the Backpack helper methods, for convenience. |
313
|
|
|
*/ |
314
|
|
|
public function loadHelpers() |
315
|
|
|
{ |
316
|
|
|
require_once __DIR__.'/helpers.php'; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Get the services provided by the provider. |
321
|
|
|
* |
322
|
|
|
* @return array |
323
|
|
|
*/ |
324
|
|
|
public function provides() |
325
|
|
|
{ |
326
|
|
|
return ['crud', 'widgets']; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.