|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
|
6
|
|
|
use Illuminate\Routing\Router; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Facades\App; |
|
9
|
|
|
use Illuminate\Support\Facades\Event; |
|
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
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
// Indicates if loading of the provider is deferred. |
|
28
|
|
|
protected $defer = false; |
|
29
|
|
|
// Where the route file lives, both inside the package and in the app (if overwritten). |
|
30
|
|
|
public $routeFilePath = '/routes/backpack/base.php'; |
|
31
|
|
|
// Where custom routes can be written, and will be registered by Backpack. |
|
32
|
|
|
public $customRoutesFilePath = '/routes/backpack/custom.php'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Perform post-registration booting of services. |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
|
|
public function boot(\Illuminate\Routing\Router $router) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
$this->loadViewsWithFallbacks(); |
|
42
|
|
|
$this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack'); |
|
43
|
|
|
$this->loadConfigs(); |
|
44
|
|
|
$this->registerMiddlewareGroup($this->app->router); |
|
|
|
|
|
|
45
|
|
|
$this->setupRoutes($this->app->router); |
|
46
|
|
|
$this->setupCustomRoutes($this->app->router); |
|
|
|
|
|
|
47
|
|
|
$this->publishFiles(); |
|
48
|
|
|
$this->checkLicenseCodeExists(); |
|
49
|
|
|
$this->sendUsageStats(); |
|
50
|
|
|
$this->maybeApplySkin(); |
|
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
|
|
|
|
|
99
|
|
|
public function publishFiles() |
|
100
|
|
|
{ |
|
101
|
|
|
$error_views = [__DIR__.'/resources/error_views' => resource_path('views/errors')]; |
|
102
|
|
|
$backpack_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack')]; |
|
103
|
|
|
$backpack_public_assets = [__DIR__.'/public' => public_path()]; |
|
104
|
|
|
$backpack_lang_files = [__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')]; |
|
105
|
|
|
$backpack_config_files = [__DIR__.'/config' => config_path()]; |
|
106
|
|
|
|
|
107
|
|
|
// sidebar content views, which are the only views most people need to overwrite |
|
108
|
|
|
$backpack_menu_contents_view = [ |
|
109
|
|
|
__DIR__.'/resources/views/base/inc/sidebar_content.blade.php' => resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php'), |
|
110
|
|
|
__DIR__.'/resources/views/base/inc/topbar_left_content.blade.php' => resource_path('views/vendor/backpack/base/inc/topbar_left_content.blade.php'), |
|
111
|
|
|
__DIR__.'/resources/views/base/inc/topbar_right_content.blade.php' => resource_path('views/vendor/backpack/base/inc/topbar_right_content.blade.php'), |
|
112
|
|
|
]; |
|
113
|
|
|
$backpack_custom_routes_file = [__DIR__.$this->customRoutesFilePath => base_path($this->customRoutesFilePath)]; |
|
114
|
|
|
|
|
115
|
|
|
// calculate the path from current directory to get the vendor path |
|
116
|
|
|
$vendorPath = dirname(__DIR__, 3); |
|
117
|
|
|
$gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()]; |
|
118
|
|
|
|
|
119
|
|
|
// 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 |
|
120
|
|
|
$minimum = array_merge( |
|
121
|
|
|
// $backpack_views, |
|
122
|
|
|
// $backpack_lang_files, |
|
123
|
|
|
$error_views, |
|
124
|
|
|
$backpack_public_assets, |
|
125
|
|
|
$backpack_config_files, |
|
126
|
|
|
$backpack_menu_contents_view, |
|
127
|
|
|
$backpack_custom_routes_file, |
|
128
|
|
|
$gravatar_assets |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
// register all possible publish commands and assign tags to each |
|
132
|
|
|
$this->publishes($backpack_config_files, 'config'); |
|
133
|
|
|
$this->publishes($backpack_lang_files, 'lang'); |
|
134
|
|
|
$this->publishes($backpack_views, 'views'); |
|
135
|
|
|
$this->publishes($backpack_menu_contents_view, 'menu_contents'); |
|
136
|
|
|
$this->publishes($error_views, 'errors'); |
|
137
|
|
|
$this->publishes($backpack_public_assets, 'public'); |
|
138
|
|
|
$this->publishes($backpack_custom_routes_file, 'custom_routes'); |
|
139
|
|
|
$this->publishes($gravatar_assets, 'gravatar'); |
|
140
|
|
|
$this->publishes($minimum, 'minimum'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Define the routes for the application. |
|
145
|
|
|
* |
|
146
|
|
|
* @param \Illuminate\Routing\Router $router |
|
147
|
|
|
* |
|
148
|
|
|
* @return void |
|
149
|
|
|
*/ |
|
150
|
|
|
public function setupRoutes(Router $router) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
// by default, use the routes file provided in vendor |
|
153
|
|
|
$routeFilePathInUse = __DIR__.$this->routeFilePath; |
|
154
|
|
|
|
|
155
|
|
|
// but if there's a file with the same name in routes/backpack, use that one |
|
156
|
|
|
if (file_exists(base_path().$this->routeFilePath)) { |
|
157
|
|
|
$routeFilePathInUse = base_path().$this->routeFilePath; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$this->loadRoutesFrom($routeFilePathInUse); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Load custom routes file. |
|
165
|
|
|
* |
|
166
|
|
|
* @param \Illuminate\Routing\Router $router |
|
167
|
|
|
* |
|
168
|
|
|
* @return void |
|
169
|
|
|
*/ |
|
170
|
|
|
public function setupCustomRoutes(Router $router) |
|
|
|
|
|
|
171
|
|
|
{ |
|
172
|
|
|
// if the custom routes file is published, register its routes |
|
173
|
|
|
if (file_exists(base_path().$this->customRoutesFilePath)) { |
|
174
|
|
|
$this->loadRoutesFrom(base_path().$this->customRoutesFilePath); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* The route macro allows developers to generate the routes for a CrudController, |
|
180
|
|
|
* for all operations, using a simple syntax: Route::crud(). |
|
181
|
|
|
* |
|
182
|
|
|
* It will go to the given CrudController and get the setupRoutes() method on it. |
|
183
|
|
|
*/ |
|
184
|
|
|
private function addRouteMacro() |
|
185
|
|
|
{ |
|
186
|
|
|
Route::macro('crud', function ($name, $controller) { |
|
187
|
|
|
// put together the route name prefix, |
|
188
|
|
|
// as passed to the Route::group() statements |
|
189
|
|
|
$routeName = ''; |
|
190
|
|
|
if ($this->hasGroupStack()) { |
|
|
|
|
|
|
191
|
|
|
foreach ($this->getGroupStack() as $key => $groupStack) { |
|
|
|
|
|
|
192
|
|
|
if (isset($groupStack['name'])) { |
|
193
|
|
|
if (is_array($groupStack['name'])) { |
|
194
|
|
|
$routeName = implode('', $groupStack['name']); |
|
195
|
|
|
} else { |
|
196
|
|
|
$routeName = $groupStack['name']; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
// add the name of the current entity to the route name prefix |
|
202
|
|
|
// the result will be the current route name (not ending in dot) |
|
203
|
|
|
$routeName .= $name; |
|
204
|
|
|
|
|
205
|
|
|
// get an instance of the controller |
|
206
|
|
|
if ($this->hasGroupStack()) { |
|
207
|
|
|
$groupStack = $this->getGroupStack(); |
|
208
|
|
|
$groupNamespace = $groupStack && isset(end($groupStack)['namespace']) ? end($groupStack)['namespace'].'\\' : ''; |
|
209
|
|
|
} else { |
|
210
|
|
|
$groupNamespace = ''; |
|
211
|
|
|
} |
|
212
|
|
|
$namespacedController = $groupNamespace.$controller; |
|
213
|
|
|
$controllerInstance = App::make($namespacedController); |
|
214
|
|
|
|
|
215
|
|
|
return $controllerInstance->setupRoutes($name, $routeName, $controller); |
|
216
|
|
|
}); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function loadViewsWithFallbacks() |
|
220
|
|
|
{ |
|
221
|
|
|
$customBaseFolder = resource_path('views/vendor/backpack/base'); |
|
222
|
|
|
$customCrudFolder = resource_path('views/vendor/backpack/crud'); |
|
223
|
|
|
|
|
224
|
|
|
// - first the published/overwritten views (in case they have any changes) |
|
225
|
|
|
if (file_exists($customBaseFolder)) { |
|
226
|
|
|
$this->loadViewsFrom($customBaseFolder, 'backpack'); |
|
227
|
|
|
} |
|
228
|
|
|
if (file_exists($customCrudFolder)) { |
|
229
|
|
|
$this->loadViewsFrom($customCrudFolder, 'crud'); |
|
230
|
|
|
} |
|
231
|
|
|
// - then the stock views that come with the package, in case a published view might be missing |
|
232
|
|
|
$this->loadViewsFrom(realpath(__DIR__.'/resources/views/base'), 'backpack'); |
|
233
|
|
|
$this->loadViewsFrom(realpath(__DIR__.'/resources/views/crud'), 'crud'); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function loadConfigs() |
|
237
|
|
|
{ |
|
238
|
|
|
// use the vendor configuration file as fallback |
|
239
|
|
|
$this->mergeConfigFrom(__DIR__.'/config/backpack/crud.php', 'backpack.crud'); |
|
240
|
|
|
$this->mergeConfigFrom(__DIR__.'/config/backpack/base.php', 'backpack.base'); |
|
241
|
|
|
|
|
242
|
|
|
// add the root disk to filesystem configuration |
|
243
|
|
|
app()->config['filesystems.disks.'.config('backpack.base.root_disk_name')] = [ |
|
244
|
|
|
'driver' => 'local', |
|
245
|
|
|
'root' => base_path(), |
|
246
|
|
|
]; |
|
247
|
|
|
|
|
248
|
|
|
/* |
|
249
|
|
|
* Backpack login differs from the standard Laravel login. |
|
250
|
|
|
* As such, Backpack uses its own authentication provider, password broker and guard. |
|
251
|
|
|
* |
|
252
|
|
|
* THe process below adds those configuration values on top of whatever is in config/auth.php. |
|
253
|
|
|
* Developers can overwrite the backpack provider, password broker or guard by adding a |
|
254
|
|
|
* provider/broker/guard with the "backpack" name inside their config/auth.php file. |
|
255
|
|
|
* Or they can use another provider/broker/guard entirely, by changing the corresponding |
|
256
|
|
|
* value inside config/backpack/base.php |
|
257
|
|
|
*/ |
|
258
|
|
|
|
|
259
|
|
|
// add the backpack_users authentication provider to the configuration |
|
260
|
|
|
app()->config['auth.providers'] = app()->config['auth.providers'] + |
|
261
|
|
|
[ |
|
262
|
|
|
'backpack' => [ |
|
263
|
|
|
'driver' => 'eloquent', |
|
264
|
|
|
'model' => config('backpack.base.user_model_fqn'), |
|
265
|
|
|
], |
|
266
|
|
|
]; |
|
267
|
|
|
|
|
268
|
|
|
// add the backpack_users password broker to the configuration |
|
269
|
|
|
app()->config['auth.passwords'] = app()->config['auth.passwords'] + |
|
270
|
|
|
[ |
|
271
|
|
|
'backpack' => [ |
|
272
|
|
|
'provider' => 'backpack', |
|
273
|
|
|
'table' => 'password_resets', |
|
274
|
|
|
'expire' => 60, |
|
275
|
|
|
], |
|
276
|
|
|
]; |
|
277
|
|
|
|
|
278
|
|
|
// add the backpack_users guard to the configuration |
|
279
|
|
|
app()->config['auth.guards'] = app()->config['auth.guards'] + |
|
280
|
|
|
[ |
|
281
|
|
|
'backpack' => [ |
|
282
|
|
|
'driver' => 'session', |
|
283
|
|
|
'provider' => 'backpack', |
|
284
|
|
|
], |
|
285
|
|
|
]; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Load the Backpack helper methods, for convenience. |
|
290
|
|
|
*/ |
|
291
|
|
|
public function loadHelpers() |
|
292
|
|
|
{ |
|
293
|
|
|
require_once __DIR__.'/helpers.php'; |
|
294
|
|
|
} |
|
295
|
|
|
/** |
|
296
|
|
|
* Listens to Illuminate\Routing\Events\RouteMatched event and loads any skin into the config. |
|
297
|
|
|
* @return void |
|
298
|
|
|
*/ |
|
299
|
|
|
public function maybeApplySkin() |
|
300
|
|
|
{ |
|
301
|
|
|
if($skin = config('backpack.base.skin')){ |
|
302
|
|
|
backpack_apply_skin($skin); |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Get the services provided by the provider. |
|
308
|
|
|
* |
|
309
|
|
|
* @return array |
|
310
|
|
|
*/ |
|
311
|
|
|
public function provides() |
|
312
|
|
|
{ |
|
313
|
|
|
return ['crud', 'widgets']; |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.