AdminServiceProvider.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Dcat\Admin;
  3. use Dcat\Admin\Layout\Content;
  4. use Dcat\Admin\Layout\Menu;
  5. use Dcat\Admin\Layout\Navbar;
  6. use Dcat\Admin\Layout\SectionManager;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Facades\Blade;
  9. use Illuminate\Support\Fluent;
  10. use Illuminate\Support\ServiceProvider;
  11. class AdminServiceProvider extends ServiceProvider
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $commands = [
  17. Console\AdminCommand::class,
  18. Console\InstallCommand::class,
  19. Console\PublishCommand::class,
  20. Console\UninstallCommand::class,
  21. Console\ImportCommand::class,
  22. Console\CreateUserCommand::class,
  23. Console\ResetPasswordCommand::class,
  24. Console\ExtendCommand::class,
  25. Console\ExportSeedCommand::class,
  26. Console\IdeHelperCommand::class,
  27. Console\FormCommand::class,
  28. ];
  29. /**
  30. * The application's route middleware.
  31. *
  32. * @var array
  33. */
  34. protected $routeMiddleware = [
  35. 'admin.auth' => Middleware\Authenticate::class,
  36. 'admin.pjax' => Middleware\Pjax::class,
  37. 'admin.log' => Middleware\LogOperation::class,
  38. 'admin.permission' => Middleware\Permission::class,
  39. 'admin.bootstrap' => Middleware\Bootstrap::class,
  40. 'admin.session' => Middleware\Session::class,
  41. ];
  42. /**
  43. * The application's route middleware groups.
  44. *
  45. * @var array
  46. */
  47. protected $middlewareGroups = [
  48. 'admin' => [
  49. 'admin.auth',
  50. 'admin.pjax',
  51. 'admin.log',
  52. 'admin.bootstrap',
  53. 'admin.permission',
  54. 'admin.session',
  55. ],
  56. ];
  57. /**
  58. * Boot the service provider.
  59. *
  60. * @return void
  61. */
  62. public function boot()
  63. {
  64. $this->registerDefaultSections();
  65. $this->registerViews();
  66. $this->ensureHttps();
  67. $this->registerRoutes();
  68. $this->registerPublishing();
  69. $this->compatibleBlade();
  70. }
  71. /**
  72. * Register the service provider.
  73. *
  74. * @return void
  75. */
  76. public function register()
  77. {
  78. require_once __DIR__.'/Support/AdminSection.php';
  79. $this->registerExtensionProviders();
  80. $this->loadAdminAuthConfig();
  81. $this->registerRouteMiddleware();
  82. $this->registerServices();
  83. $this->commands($this->commands);
  84. }
  85. /**
  86. * Register the view file namespace.
  87. */
  88. protected function registerViews()
  89. {
  90. $this->loadViewsFrom(__DIR__.'/../resources/views', 'admin');
  91. }
  92. /**
  93. * Force to set https scheme if https enabled.
  94. *
  95. * @return void
  96. */
  97. protected function ensureHttps()
  98. {
  99. if (config('admin.https') || config('admin.secure')) {
  100. \URL::forceScheme('https');
  101. $this->app['request']->server->set('HTTPS', true);
  102. }
  103. }
  104. /**
  105. * Register routes.
  106. */
  107. protected function registerRoutes()
  108. {
  109. if (is_file($routes = admin_path('routes.php'))) {
  110. $this->loadRoutesFrom($routes);
  111. }
  112. }
  113. /**
  114. * Remove default feature of double encoding enable in laravel 5.6 or later.
  115. *
  116. * @return void
  117. */
  118. protected function compatibleBlade()
  119. {
  120. $bladeReflectionClass = new \ReflectionClass('\Illuminate\View\Compilers\BladeCompiler');
  121. if ($bladeReflectionClass->hasMethod('withoutDoubleEncoding')) {
  122. Blade::withoutDoubleEncoding();
  123. }
  124. }
  125. /**
  126. * Register the package's publishable resources.
  127. *
  128. * @return void
  129. */
  130. protected function registerPublishing()
  131. {
  132. if ($this->app->runningInConsole()) {
  133. $this->publishes([__DIR__.'/../config' => config_path()], 'dcat-admin-config');
  134. $this->publishes([__DIR__.'/../resources/lang' => resource_path('lang')], 'dcat-admin-lang');
  135. $this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'dcat-admin-migrations');
  136. $this->publishes([__DIR__.'/../resources/assets' => public_path('vendor/dcat-admin')], 'dcat-admin-assets');
  137. }
  138. }
  139. /**
  140. * Register the service provider of extensions.
  141. */
  142. public function registerExtensionProviders()
  143. {
  144. foreach (Admin::availableExtensions() as $extension) {
  145. if ($provider = $extension->serviceProvider()) {
  146. $this->app->register($provider);
  147. }
  148. }
  149. }
  150. /**
  151. * Setup auth configuration.
  152. *
  153. * @return void
  154. */
  155. protected function loadAdminAuthConfig()
  156. {
  157. config(Arr::dot(config('admin.auth', []), 'auth.'));
  158. }
  159. /**
  160. * Register default sections.
  161. */
  162. protected function registerDefaultSections()
  163. {
  164. Content::composing(function () {
  165. if (! admin_has_default_section(\AdminSection::NAVBAR_USER_PANEL)) {
  166. admin_inject_default_section(\AdminSection::NAVBAR_USER_PANEL, view('admin::partials.navbar-user-panel'));
  167. }
  168. if (! admin_has_default_section(\AdminSection::LEFT_SIDEBAR_USER_PANEL)) {
  169. admin_inject_default_section(\AdminSection::LEFT_SIDEBAR_USER_PANEL, view('admin::partials.sidebar-user-panel'));
  170. }
  171. // Register menu
  172. Admin::menu()->register();
  173. }, true);
  174. }
  175. /**
  176. * Register admin services.
  177. */
  178. protected function registerServices()
  179. {
  180. $this->app->singleton('sectionManager', SectionManager::class);
  181. $this->app->singleton('admin.navbar', Navbar::class);
  182. $this->app->singleton('admin.menu', Menu::class);
  183. $this->app->singleton('admin.temp', Fluent::class);
  184. }
  185. /**
  186. * Register the route middleware.
  187. *
  188. * @return void
  189. */
  190. protected function registerRouteMiddleware()
  191. {
  192. $router = $this->app->make('router');
  193. // register route middleware.
  194. foreach ($this->routeMiddleware as $key => $middleware) {
  195. $router->aliasMiddleware($key, $middleware);
  196. }
  197. $disablePermission = ! config('admin.permission.enable');
  198. // register middleware group.
  199. foreach ($this->middlewareGroups as $key => $middleware) {
  200. if ($disablePermission && $middleware == 'admin.permission') {
  201. continue;
  202. }
  203. $router->middlewareGroup($key, $middleware);
  204. }
  205. }
  206. }