AdminServiceProvider.php 6.8 KB

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