Application.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Dcat\Admin;
  3. use Illuminate\Contracts\Container\Container;
  4. use Illuminate\Support\Facades\Route;
  5. class Application
  6. {
  7. const DEFAULT = 'admin';
  8. /**
  9. * @var Container
  10. */
  11. protected $app;
  12. /**
  13. * 所有启用应用的配置.
  14. *
  15. * @var array
  16. */
  17. protected $configs = [];
  18. /**
  19. * 当前应用名称.
  20. *
  21. * @var string
  22. */
  23. protected $name;
  24. public function __construct(Container $app)
  25. {
  26. $this->app = $app;
  27. }
  28. /**
  29. * 设置当前应用配置.
  30. *
  31. * @param string $app
  32. */
  33. public function current(string $app = null)
  34. {
  35. $this->withName($app);
  36. $this->withConfig($this->name);
  37. }
  38. /**
  39. * 设置应用名称.
  40. *
  41. * @param string $app
  42. */
  43. public function withName(string $app)
  44. {
  45. $this->name = $app;
  46. }
  47. /**
  48. * 获取当前应用名称.
  49. *
  50. * @return string
  51. */
  52. public function getName()
  53. {
  54. return $this->name ?: static::DEFAULT;
  55. }
  56. /**
  57. * 注册应用.
  58. */
  59. public function boot()
  60. {
  61. $this->registerRoute(static::DEFAULT);
  62. if ($this->app->runningInConsole()) {
  63. return;
  64. }
  65. foreach ((array) config('admin.multi_app') as $app => $enable) {
  66. if ($enable) {
  67. $this->registerRoute($app);
  68. }
  69. }
  70. }
  71. /**
  72. * @return string
  73. */
  74. public function getCurrentApiRoutePrefix()
  75. {
  76. return $this->getApiRoutePrefix($this->getName());
  77. }
  78. /**
  79. * @param string|null $app
  80. *
  81. * @return string
  82. */
  83. public function getApiRoutePrefix(?string $app)
  84. {
  85. return "dcat.api.{$app}.";
  86. }
  87. /**
  88. * 注册应用路由.
  89. *
  90. * @param string|null $app
  91. */
  92. protected function registerRoute(?string $app)
  93. {
  94. $this->withConfig($app);
  95. Admin::registerApiRoutes($this->getApiRoutePrefix($app));
  96. if (is_file($routes = admin_path('routes.php'))) {
  97. $this->loadRoutesFrom($routes, $app);
  98. }
  99. }
  100. /**
  101. * 设置应用配置.
  102. *
  103. * @param string $app
  104. */
  105. protected function withConfig(string $app)
  106. {
  107. if (! isset($this->configs[$app])) {
  108. $this->configs[$app] = config($app);
  109. $this->configs[$app]['current_app'] = $app;
  110. }
  111. config(['admin' => $this->configs[$app]]);
  112. }
  113. /**
  114. * 加载路由文件.
  115. *
  116. * @param string $path
  117. * @param string $app
  118. *
  119. * @return void
  120. */
  121. protected function loadRoutesFrom(string $path, ?string $app)
  122. {
  123. if (! $this->app->routesAreCached()) {
  124. Route::middleware('admin.app:'.$app)->group($path);
  125. }
  126. }
  127. }