AdminServiceProvider.php 6.2 KB

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