AdminServiceProvider.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace Dcat\Admin;
  3. use Dcat\Admin\Layout\Asset;
  4. use Dcat\Admin\Layout\Content;
  5. use Dcat\Admin\Layout\Menu;
  6. use Dcat\Admin\Layout\Navbar;
  7. use Dcat\Admin\Layout\SectionManager;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\Facades\Blade;
  10. use Illuminate\Support\Fluent;
  11. use Illuminate\Support\ServiceProvider;
  12. class AdminServiceProvider extends ServiceProvider
  13. {
  14. /**
  15. * @var array
  16. */
  17. protected $commands = [
  18. Console\AdminCommand::class,
  19. Console\InstallCommand::class,
  20. Console\PublishCommand::class,
  21. Console\UninstallCommand::class,
  22. Console\ImportCommand::class,
  23. Console\CreateUserCommand::class,
  24. Console\ResetPasswordCommand::class,
  25. Console\ExtendCommand::class,
  26. Console\ExportSeedCommand::class,
  27. Console\IdeHelperCommand::class,
  28. Console\FormCommand::class,
  29. Console\ActionCommand::class,
  30. Console\MenuCacheCommand::class,
  31. ];
  32. /**
  33. * 开发环境命令.
  34. *
  35. * @var array
  36. */
  37. protected $devCommands = [
  38. Console\Development\LinkCommand::class,
  39. ];
  40. /**
  41. * @var array
  42. */
  43. protected $routeMiddleware = [
  44. 'admin.auth' => Middleware\Authenticate::class,
  45. 'admin.pjax' => Middleware\Pjax::class,
  46. 'admin.log' => Middleware\LogOperation::class,
  47. 'admin.permission' => Middleware\Permission::class,
  48. 'admin.bootstrap' => Middleware\Bootstrap::class,
  49. 'admin.session' => Middleware\Session::class,
  50. ];
  51. /**
  52. * @var array
  53. */
  54. protected $middlewareGroups = [
  55. 'admin' => [
  56. 'admin.auth',
  57. 'admin.pjax',
  58. 'admin.log',
  59. 'admin.bootstrap',
  60. 'admin.permission',
  61. 'admin.session',
  62. ],
  63. ];
  64. public function boot()
  65. {
  66. $this->registerDefaultSections();
  67. $this->registerViews();
  68. $this->ensureHttps();
  69. $this->registerRoutes();
  70. $this->registerPublishing();
  71. $this->compatibleBlade();
  72. }
  73. public function register()
  74. {
  75. require_once __DIR__.'/Support/AdminSection.php';
  76. $this->aliasAdmin();
  77. $this->registerExtensionProviders();
  78. $this->loadAdminAuthConfig();
  79. $this->registerRouteMiddleware();
  80. $this->registerServices();
  81. $this->commands($this->commands);
  82. if (config('app.debug') && config('app.env') === 'local') {
  83. $this->commands($this->devCommands);
  84. }
  85. }
  86. protected function aliasAdmin()
  87. {
  88. if (! class_exists(\Admin::class)) {
  89. class_alias(Admin::class, \Admin::class);
  90. }
  91. }
  92. protected function registerViews()
  93. {
  94. $this->loadViewsFrom(__DIR__.'/../resources/views', 'admin');
  95. }
  96. /**
  97. * 是否强制使用https
  98. *
  99. * @return void
  100. */
  101. protected function ensureHttps()
  102. {
  103. if (config('admin.https') || config('admin.secure')) {
  104. \URL::forceScheme('https');
  105. $this->app['request']->server->set('HTTPS', true);
  106. }
  107. }
  108. /**
  109. * 路由注册.
  110. */
  111. protected function registerRoutes()
  112. {
  113. if (is_file($routes = admin_path('routes.php'))) {
  114. $this->loadRoutesFrom($routes);
  115. }
  116. }
  117. /**
  118. * 禁止laravel 5.6或更高版本中启用双编码的默认特性.
  119. *
  120. * @return void
  121. */
  122. protected function compatibleBlade()
  123. {
  124. $bladeReflectionClass = new \ReflectionClass('\Illuminate\View\Compilers\BladeCompiler');
  125. if ($bladeReflectionClass->hasMethod('withoutDoubleEncoding')) {
  126. Blade::withoutDoubleEncoding();
  127. }
  128. }
  129. /**
  130. * 资源发布注册.
  131. *
  132. * @return void
  133. */
  134. protected function registerPublishing()
  135. {
  136. if ($this->app->runningInConsole()) {
  137. $this->publishes([__DIR__.'/../config' => config_path()], 'dcat-admin-config');
  138. $this->publishes([__DIR__.'/../resources/lang' => resource_path('lang')], 'dcat-admin-lang');
  139. $this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'dcat-admin-migrations');
  140. $this->publishes([__DIR__.'/../resources/assets/dist' => public_path(Admin::asset()->getRealPath('@admin'))], 'dcat-admin-assets');
  141. }
  142. }
  143. /**
  144. * 扩展注册.
  145. */
  146. public function registerExtensionProviders()
  147. {
  148. foreach (Admin::availableExtensions() as $extension) {
  149. if ($provider = $extension->serviceProvider()) {
  150. $this->app->register($provider);
  151. }
  152. }
  153. }
  154. /**
  155. * 设置 auth 配置.
  156. *
  157. * @return void
  158. */
  159. protected function loadAdminAuthConfig()
  160. {
  161. config(Arr::dot(config('admin.auth', []), 'auth.'));
  162. }
  163. /**
  164. * 默认 section 注册.
  165. */
  166. protected function registerDefaultSections()
  167. {
  168. Content::composing(function () {
  169. if (! admin_has_default_section(\AdminSection::NAVBAR_USER_PANEL)) {
  170. admin_inject_default_section(\AdminSection::NAVBAR_USER_PANEL, function () {
  171. return view('admin::partials.navbar-user-panel', ['user' => Admin::user()]);
  172. });
  173. }
  174. if (! admin_has_default_section(\AdminSection::LEFT_SIDEBAR_USER_PANEL)) {
  175. admin_inject_default_section(\AdminSection::LEFT_SIDEBAR_USER_PANEL, function () {
  176. return view('admin::partials.sidebar-user-panel', ['user' => Admin::user()]);
  177. });
  178. }
  179. // Register menu
  180. Admin::menu()->register();
  181. }, true);
  182. }
  183. protected function registerServices()
  184. {
  185. $this->app->singleton('admin.asset', Asset::class);
  186. $this->app->singleton('admin.color', Color::class);
  187. $this->app->singleton('admin.sections', SectionManager::class);
  188. $this->app->singleton('admin.navbar', Navbar::class);
  189. $this->app->singleton('admin.menu', Menu::class);
  190. $this->app->singleton('admin.context', Fluent::class);
  191. }
  192. /**
  193. * 路由中间件注册.
  194. *
  195. * @return void
  196. */
  197. protected function registerRouteMiddleware()
  198. {
  199. $router = $this->app->make('router');
  200. // register route middleware.
  201. foreach ($this->routeMiddleware as $key => $middleware) {
  202. $router->aliasMiddleware($key, $middleware);
  203. }
  204. $disablePermission = ! config('admin.permission.enable');
  205. // register middleware group.
  206. foreach ($this->middlewareGroups as $key => $middleware) {
  207. if ($disablePermission && $middleware == 'admin.permission') {
  208. continue;
  209. }
  210. $router->middlewareGroup($key, $middleware);
  211. }
  212. }
  213. }