Application.php 3.5 KB

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