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