AdminServiceProvider.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. 'admin.upload' => Middleware\WebUploader::class,
  51. ];
  52. /**
  53. * @var array
  54. */
  55. protected $middlewareGroups = [
  56. 'admin' => [
  57. 'admin.auth',
  58. 'admin.pjax',
  59. 'admin.log',
  60. 'admin.bootstrap',
  61. 'admin.permission',
  62. 'admin.session',
  63. 'admin.upload',
  64. ],
  65. ];
  66. public function boot()
  67. {
  68. $this->registerDefaultSections();
  69. $this->registerViews();
  70. $this->ensureHttps();
  71. $this->registerRoutes();
  72. $this->registerPublishing();
  73. $this->compatibleBlade();
  74. }
  75. public function register()
  76. {
  77. require_once __DIR__.'/Support/AdminSection.php';
  78. $this->aliasAdmin();
  79. $this->registerExtensionProviders();
  80. $this->loadAdminAuthConfig();
  81. $this->registerRouteMiddleware();
  82. $this->registerServices();
  83. $this->commands($this->commands);
  84. if (config('app.debug')) {
  85. $this->commands($this->devCommands);
  86. }
  87. }
  88. protected function aliasAdmin()
  89. {
  90. if (! class_exists(\Admin::class)) {
  91. class_alias(Admin::class, \Admin::class);
  92. }
  93. }
  94. protected function registerViews()
  95. {
  96. $this->loadViewsFrom(__DIR__.'/../resources/views', 'admin');
  97. }
  98. /**
  99. * 是否强制使用https.
  100. *
  101. * @return void
  102. */
  103. protected function ensureHttps()
  104. {
  105. if (config('admin.https') || config('admin.secure')) {
  106. \URL::forceScheme('https');
  107. $this->app['request']->server->set('HTTPS', true);
  108. }
  109. }
  110. /**
  111. * 路由注册.
  112. */
  113. protected function registerRoutes()
  114. {
  115. Admin::registerApiRoutes();
  116. if (is_file($routes = admin_path('routes.php'))) {
  117. $this->loadRoutesFrom($routes);
  118. }
  119. }
  120. /**
  121. * 禁止laravel 5.6或更高版本中启用双编码的默认特性.
  122. *
  123. * @return void
  124. */
  125. protected function compatibleBlade()
  126. {
  127. $bladeReflectionClass = new \ReflectionClass('\Illuminate\View\Compilers\BladeCompiler');
  128. if ($bladeReflectionClass->hasMethod('withoutDoubleEncoding')) {
  129. Blade::withoutDoubleEncoding();
  130. }
  131. }
  132. /**
  133. * 资源发布注册.
  134. *
  135. * @return void
  136. */
  137. protected function registerPublishing()
  138. {
  139. if ($this->app->runningInConsole()) {
  140. $this->publishes([__DIR__.'/../config' => config_path()], 'dcat-admin-config');
  141. $this->publishes([__DIR__.'/../resources/lang' => resource_path('lang')], 'dcat-admin-lang');
  142. $this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'dcat-admin-migrations');
  143. $this->publishes([__DIR__.'/../resources/dist' => public_path(Admin::asset()->getRealPath('@admin'))], 'dcat-admin-assets');
  144. }
  145. }
  146. /**
  147. * 扩展注册.
  148. */
  149. public function registerExtensionProviders()
  150. {
  151. foreach (Admin::availableExtensions() as $extension) {
  152. if ($provider = $extension->serviceProvider()) {
  153. $this->app->register($provider);
  154. }
  155. }
  156. }
  157. /**
  158. * 设置 auth 配置.
  159. *
  160. * @return void
  161. */
  162. protected function loadAdminAuthConfig()
  163. {
  164. config(Arr::dot(config('admin.auth', []), 'auth.'));
  165. }
  166. /**
  167. * 默认 section 注册.
  168. */
  169. protected function registerDefaultSections()
  170. {
  171. Content::composing(function () {
  172. if (! admin_has_default_section(\AdminSection::NAVBAR_USER_PANEL)) {
  173. admin_inject_default_section(\AdminSection::NAVBAR_USER_PANEL, function () {
  174. return view('admin::partials.navbar-user-panel', ['user' => Admin::user()]);
  175. });
  176. }
  177. if (! admin_has_default_section(\AdminSection::LEFT_SIDEBAR_USER_PANEL)) {
  178. admin_inject_default_section(\AdminSection::LEFT_SIDEBAR_USER_PANEL, function () {
  179. return view('admin::partials.sidebar-user-panel', ['user' => Admin::user()]);
  180. });
  181. }
  182. // Register menu
  183. Admin::menu()->register();
  184. }, true);
  185. }
  186. protected function registerServices()
  187. {
  188. $this->app->singleton('admin.asset', Asset::class);
  189. $this->app->singleton('admin.color', Color::class);
  190. $this->app->singleton('admin.sections', SectionManager::class);
  191. $this->app->singleton('admin.navbar', Navbar::class);
  192. $this->app->singleton('admin.menu', Menu::class);
  193. $this->app->singleton('admin.context', Fluent::class);
  194. }
  195. /**
  196. * 路由中间件注册.
  197. *
  198. * @return void
  199. */
  200. protected function registerRouteMiddleware()
  201. {
  202. $router = $this->app->make('router');
  203. // register route middleware.
  204. foreach ($this->routeMiddleware as $key => $middleware) {
  205. $router->aliasMiddleware($key, $middleware);
  206. }
  207. $disablePermission = ! config('admin.permission.enable');
  208. // register middleware group.
  209. foreach ($this->middlewareGroups as $key => $middleware) {
  210. if ($disablePermission && $middleware == 'admin.permission') {
  211. continue;
  212. }
  213. $router->middlewareGroup($key, $middleware);
  214. }
  215. }
  216. }