AdminServiceProvider.php 6.5 KB

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