We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
2 | |||
3 | namespace Backpack\CRUD; |
||
4 | |||
5 | use Backpack\Basset\Facades\Basset; |
||
6 | use Backpack\CRUD\app\Http\Middleware\EnsureEmailVerification; |
||
7 | use Backpack\CRUD\app\Http\Middleware\ThrottlePasswordRecovery; |
||
8 | use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
||
9 | use Backpack\CRUD\app\Library\Database\DatabaseSchema; |
||
10 | use Backpack\CRUD\app\Library\Uploaders\Support\UploadersRepository; |
||
11 | use Illuminate\Contracts\Debug\ExceptionHandler; |
||
12 | use Illuminate\Routing\Router; |
||
13 | use Illuminate\Support\Collection; |
||
14 | use Illuminate\Support\Facades\Blade; |
||
15 | use Illuminate\Support\ServiceProvider; |
||
16 | use Illuminate\Support\Str; |
||
17 | use Illuminate\View\Compilers\BladeCompiler; |
||
18 | |||
19 | class BackpackServiceProvider extends ServiceProvider |
||
20 | { |
||
21 | use Stats; |
||
22 | |||
23 | protected $commands = [ |
||
24 | app\Console\Commands\Install::class, |
||
25 | app\Console\Commands\AddMenuContent::class, |
||
26 | app\Console\Commands\AddCustomRouteContent::class, |
||
27 | app\Console\Commands\Version::class, |
||
28 | app\Console\Commands\CreateUser::class, |
||
29 | app\Console\Commands\PublishBackpackMiddleware::class, |
||
30 | app\Console\Commands\PublishView::class, |
||
31 | app\Console\Commands\Addons\RequireDevTools::class, |
||
32 | app\Console\Commands\Addons\RequireEditableColumns::class, |
||
33 | app\Console\Commands\Addons\RequirePro::class, |
||
34 | app\Console\Commands\Themes\RequireThemeTabler::class, |
||
35 | app\Console\Commands\Themes\RequireThemeCoreuiv2::class, |
||
36 | app\Console\Commands\Themes\RequireThemeCoreuiv4::class, |
||
37 | app\Console\Commands\Fix::class, |
||
38 | app\Console\Commands\PublishHeaderMetas::class, |
||
39 | ]; |
||
40 | |||
41 | // Indicates if loading of the provider is deferred. |
||
42 | protected $defer = false; |
||
43 | |||
44 | // Where the route file lives, both inside the package and in the app (if overwritten). |
||
45 | public $routeFilePath = '/routes/backpack/base.php'; |
||
46 | |||
47 | // Where custom routes can be written, and will be registered by Backpack. |
||
48 | public $customRoutesFilePath = '/routes/backpack/custom.php'; |
||
49 | |||
50 | /** |
||
51 | * Perform post-registration booting of services. |
||
52 | * |
||
53 | * @return void |
||
54 | */ |
||
55 | public function boot(Router $router) |
||
56 | { |
||
57 | $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack'); |
||
58 | $this->loadConfigs(); |
||
59 | $this->registerMiddlewareGroup($this->app->router); |
||
60 | $this->setupRoutes($this->app->router); |
||
61 | $this->setupCustomRoutes($this->app->router); |
||
62 | $this->publishFiles(); |
||
63 | $this->sendUsageStats(); |
||
64 | |||
65 | Basset::addViewPath(realpath(__DIR__.'/resources/views')); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Register any package services. |
||
70 | * |
||
71 | * @return void |
||
72 | */ |
||
73 | public function register() |
||
74 | { |
||
75 | // load the macros |
||
76 | include_once __DIR__.'/macros.php'; |
||
77 | |||
78 | $this->loadViewsWithFallbacks('crud'); |
||
79 | $this->loadViewsWithFallbacks('ui', 'backpack.ui'); |
||
80 | $this->loadViewNamespace('widgets', 'backpack.ui::widgets'); |
||
81 | $this->loadViewComponents(); |
||
82 | |||
83 | $this->registerBackpackErrorViews(); |
||
84 | |||
85 | // Bind the CrudPanel object to Laravel's service container |
||
86 | $this->app->scoped('crud', function ($app) { |
||
87 | return new CrudPanel(); |
||
88 | }); |
||
89 | |||
90 | $this->app->scoped('DatabaseSchema', function ($app) { |
||
91 | return new DatabaseSchema(); |
||
92 | }); |
||
93 | |||
94 | $this->app->singleton('BackpackViewNamespaces', function ($app) { |
||
95 | return new ViewNamespaces(); |
||
96 | }); |
||
97 | |||
98 | // Bind the widgets collection object to Laravel's service container |
||
99 | $this->app->singleton('widgets', function ($app) { |
||
100 | return new Collection(); |
||
101 | }); |
||
102 | |||
103 | $this->app->scoped('UploadersRepository', function ($app) { |
||
104 | return new UploadersRepository(); |
||
105 | }); |
||
106 | |||
107 | // register the helper functions |
||
108 | $this->loadHelpers(); |
||
109 | |||
110 | // register the artisan commands |
||
111 | $this->commands($this->commands); |
||
112 | } |
||
113 | |||
114 | public function registerMiddlewareGroup(Router $router) |
||
115 | { |
||
116 | $middleware_key = config('backpack.base.middleware_key'); |
||
117 | $middleware_class = config('backpack.base.middleware_class'); |
||
118 | |||
119 | if (! is_array($middleware_class)) { |
||
120 | $router->pushMiddlewareToGroup($middleware_key, $middleware_class); |
||
121 | |||
122 | return; |
||
123 | } |
||
124 | |||
125 | foreach ($middleware_class as $middleware_class) { |
||
126 | $router->pushMiddlewareToGroup($middleware_key, $middleware_class); |
||
127 | } |
||
128 | |||
129 | // register internal backpack middleware for throttling the password recovery functionality |
||
130 | // but only if functionality is enabled by developer in config |
||
131 | if (config('backpack.base.setup_password_recovery_routes')) { |
||
132 | $router->aliasMiddleware('backpack.throttle.password.recovery', ThrottlePasswordRecovery::class); |
||
133 | } |
||
134 | |||
135 | // register the email verification middleware, if the developer enabled it in the config. |
||
136 | if (config('backpack.base.setup_email_verification_routes', false) && config('backpack.base.setup_email_verification_middleware', true)) { |
||
137 | $router->pushMiddlewareToGroup($middleware_key, EnsureEmailVerification::class); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | public function publishFiles() |
||
142 | { |
||
143 | $backpack_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack')]; |
||
144 | $backpack_lang_files = [__DIR__.'/resources/lang' => app()->langPath().'/vendor/backpack']; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
145 | $backpack_config_files = [__DIR__.'/config' => config_path()]; |
||
146 | |||
147 | // sidebar content views, which are the only views most people need to overwrite |
||
148 | $backpack_menu_contents_view = [ |
||
149 | __DIR__.'/resources/views/ui/inc/menu_items.blade.php' => resource_path('views/vendor/backpack/ui/inc/menu_items.blade.php'), |
||
150 | ]; |
||
151 | $backpack_custom_routes_file = [__DIR__.$this->customRoutesFilePath => base_path($this->customRoutesFilePath)]; |
||
152 | |||
153 | // calculate the path from current directory to get the vendor path |
||
154 | $vendorPath = dirname(__DIR__, 3); |
||
155 | $gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()]; |
||
156 | |||
157 | // 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 |
||
158 | $minimum = array_merge( |
||
159 | // $backpack_views, |
||
160 | // $backpack_lang_files, |
||
161 | $backpack_config_files, |
||
162 | $backpack_menu_contents_view, |
||
163 | $backpack_custom_routes_file, |
||
164 | $gravatar_assets |
||
165 | ); |
||
166 | |||
167 | // register all possible publish commands and assign tags to each |
||
168 | $this->publishes($backpack_config_files, 'config'); |
||
169 | $this->publishes($backpack_lang_files, 'lang'); |
||
170 | $this->publishes($backpack_views, 'views'); |
||
171 | $this->publishes($backpack_menu_contents_view, 'menu_contents'); |
||
172 | $this->publishes($backpack_custom_routes_file, 'custom_routes'); |
||
173 | $this->publishes($gravatar_assets, 'gravatar'); |
||
174 | $this->publishes($minimum, 'minimum'); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Define the routes for the application. |
||
179 | * |
||
180 | * @param \Illuminate\Routing\Router $router |
||
181 | * @return void |
||
182 | */ |
||
183 | public function setupRoutes(Router $router) |
||
184 | { |
||
185 | // by default, use the routes file provided in vendor |
||
186 | $routeFilePathInUse = __DIR__.$this->routeFilePath; |
||
187 | |||
188 | // but if there's a file with the same name in routes/backpack, use that one |
||
189 | if (file_exists(base_path().$this->routeFilePath)) { |
||
190 | $routeFilePathInUse = base_path().$this->routeFilePath; |
||
191 | } |
||
192 | |||
193 | $this->loadRoutesFrom($routeFilePathInUse); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Load custom routes file. |
||
198 | * |
||
199 | * @param \Illuminate\Routing\Router $router |
||
200 | * @return void |
||
201 | */ |
||
202 | public function setupCustomRoutes(Router $router) |
||
203 | { |
||
204 | // if the custom routes file is published, register its routes |
||
205 | if (file_exists(base_path().$this->customRoutesFilePath)) { |
||
206 | $this->loadRoutesFrom(base_path().$this->customRoutesFilePath); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | public function loadViewNamespace($domain, $namespace) |
||
211 | { |
||
212 | ViewNamespaces::addFor($domain, $namespace); |
||
213 | } |
||
214 | |||
215 | public function loadViewsWithFallbacks($dir, $namespace = null) |
||
216 | { |
||
217 | $customFolder = resource_path('views/vendor/backpack/'.$dir); |
||
218 | $vendorFolder = realpath(__DIR__.'/resources/views/'.$dir); |
||
219 | $namespace = $namespace ?? $dir; |
||
220 | |||
221 | // first the published/overwritten views (in case they have any changes) |
||
222 | if (file_exists($customFolder)) { |
||
223 | $this->loadViewsFrom($customFolder, $namespace); |
||
224 | } |
||
225 | // then the stock views that come with the package, in case a published view might be missing |
||
226 | $this->loadViewsFrom($vendorFolder, $namespace); |
||
227 | } |
||
228 | |||
229 | protected function mergeConfigsFromDirectory($dir) |
||
230 | { |
||
231 | $configs = scandir(__DIR__."/config/backpack/$dir/"); |
||
232 | $configs = array_diff($configs, ['.', '..']); |
||
233 | |||
234 | if (! count($configs)) { |
||
235 | return; |
||
236 | } |
||
237 | |||
238 | foreach ($configs as $configFile) { |
||
239 | $this->mergeConfigFrom( |
||
240 | __DIR__."/config/backpack/$dir/$configFile", |
||
241 | "backpack.$dir.".substr($configFile, 0, strrpos($configFile, '.')) |
||
242 | ); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | public function loadConfigs() |
||
247 | { |
||
248 | // use the vendor configuration file as fallback |
||
249 | $this->mergeConfigFrom(__DIR__.'/config/backpack/crud.php', 'backpack.crud'); |
||
250 | $this->mergeConfigFrom(__DIR__.'/config/backpack/base.php', 'backpack.base'); |
||
251 | $this->mergeConfigFrom(__DIR__.'/config/backpack/ui.php', 'backpack.ui'); |
||
252 | $this->mergeConfigsFromDirectory('operations'); |
||
253 | |||
254 | // add the root disk to filesystem configuration |
||
255 | app()->config['filesystems.disks.'.config('backpack.base.root_disk_name')] = [ |
||
256 | 'driver' => 'local', |
||
257 | 'root' => base_path(), |
||
258 | ]; |
||
259 | |||
260 | /* |
||
261 | * Backpack login differs from the standard Laravel login. |
||
262 | * As such, Backpack uses its own authentication provider, password broker and guard. |
||
263 | * |
||
264 | * THe process below adds those configuration values on top of whatever is in config/auth.php. |
||
265 | * Developers can overwrite the backpack provider, password broker or guard by adding a |
||
266 | * provider/broker/guard with the "backpack" name inside their config/auth.php file. |
||
267 | * Or they can use another provider/broker/guard entirely, by changing the corresponding |
||
268 | * value inside config/backpack/base.php |
||
269 | */ |
||
270 | |||
271 | // add the backpack_users authentication provider to the configuration |
||
272 | app()->config['auth.providers'] = app()->config['auth.providers'] + |
||
273 | [ |
||
274 | 'backpack' => [ |
||
275 | 'driver' => 'eloquent', |
||
276 | 'model' => config('backpack.base.user_model_fqn'), |
||
277 | ], |
||
278 | ]; |
||
279 | |||
280 | // add the backpack_users password broker to the configuration |
||
281 | $laravelAuthPasswordBrokers = app()->config['auth.passwords']; |
||
282 | $laravelFirstPasswordBroker = is_array($laravelAuthPasswordBrokers) && current($laravelAuthPasswordBrokers) ? |
||
283 | current($laravelAuthPasswordBrokers)['table'] : |
||
284 | ''; |
||
285 | |||
286 | $backpackPasswordBrokerTable = config('backpack.base.password_resets_table') ?? |
||
287 | config('auth.passwords.users.table') ?? |
||
288 | $laravelFirstPasswordBroker; |
||
289 | |||
290 | app()->config['auth.passwords'] = $laravelAuthPasswordBrokers + |
||
291 | [ |
||
292 | 'backpack' => [ |
||
293 | 'provider' => 'backpack', |
||
294 | 'table' => $backpackPasswordBrokerTable, |
||
295 | 'expire' => config('backpack.base.password_recovery_token_expiration', 60), |
||
296 | 'throttle' => config('backpack.base.password_recovery_throttle_notifications'), |
||
297 | ], |
||
298 | ]; |
||
299 | |||
300 | // add the backpack_users guard to the configuration |
||
301 | app()->config['auth.guards'] = app()->config['auth.guards'] + |
||
302 | [ |
||
303 | 'backpack' => [ |
||
304 | 'driver' => 'session', |
||
305 | 'provider' => 'backpack', |
||
306 | ], |
||
307 | ]; |
||
308 | } |
||
309 | |||
310 | public function loadViewComponents() |
||
311 | { |
||
312 | $this->app->afterResolving(BladeCompiler::class, function () { |
||
313 | Blade::componentNamespace('Backpack\\CRUD\\app\\View\\Components', 'backpack'); |
||
314 | }); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Load the Backpack helper methods, for convenience. |
||
319 | */ |
||
320 | public function loadHelpers() |
||
321 | { |
||
322 | require_once __DIR__.'/helpers.php'; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Get the services provided by the provider. |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | public function provides() |
||
331 | { |
||
332 | return ['crud', 'widgets', 'BackpackViewNamespaces', 'DatabaseSchema', 'UploadersRepository']; |
||
333 | } |
||
334 | |||
335 | private function registerBackpackErrorViews() |
||
336 | { |
||
337 | // register the backpack error when the exception handler is resolved from the container |
||
338 | $this->callAfterResolving(ExceptionHandler::class, function ($handler) { |
||
339 | if (! Str::startsWith(request()->path(), config('backpack.base.route_prefix'))) { |
||
340 | return; |
||
341 | } |
||
342 | |||
343 | // parse the namespaces set in config |
||
344 | [$themeNamespace, $themeFallbackNamespace] = (function () { |
||
345 | $themeNamespace = config('backpack.ui.view_namespace'); |
||
346 | $themeFallbackNamespace = config('backpack.ui.view_namespace_fallback'); |
||
347 | |||
348 | return [ |
||
349 | Str::endsWith($themeNamespace, '::') ? substr($themeNamespace, 0, -2) : substr($themeNamespace, 0, -1), |
||
350 | Str::endsWith($themeFallbackNamespace, '::') ? substr($themeFallbackNamespace, 0, -2) : substr($themeFallbackNamespace, 0, -1), |
||
351 | ]; |
||
352 | })(); |
||
353 | |||
354 | $viewFinderHints = app('view')->getFinder()->getHints(); |
||
355 | |||
356 | // here we are going to generate the paths array containing: |
||
357 | // - theme paths |
||
358 | // - fallback theme paths |
||
359 | // - ui path |
||
360 | $themeErrorPaths = $viewFinderHints[$themeNamespace] ?? []; |
||
361 | $themeErrorPaths = $themeNamespace === $themeFallbackNamespace ? $themeErrorPaths : |
||
362 | array_merge($viewFinderHints[$themeFallbackNamespace] ?? [], $themeErrorPaths); |
||
363 | $uiErrorPaths = [base_path('vendor/backpack/crud/src/resources/views/ui')]; |
||
364 | $themeErrorPaths = array_merge($themeErrorPaths, $uiErrorPaths); |
||
365 | |||
366 | // merge the paths array with the view.paths defined in the application |
||
367 | app('config')->set('view.paths', array_merge($themeErrorPaths, config('view.paths', []))); |
||
368 | }); |
||
369 | } |
||
370 | } |
||
371 |