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