AdminServiceProvider.php 6.5 KB

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