Application.php 3.5 KB

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