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