Application.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. }
  34. public function getApps()
  35. {
  36. return $this->apps ?: ($this->apps = (array) config('admin.multi_app'));
  37. }
  38. /**
  39. * 设置当前应用配置.
  40. *
  41. * @param string $app
  42. */
  43. public function switch(string $app = null)
  44. {
  45. $this->withName($app);
  46. $this->withConfig($this->name);
  47. }
  48. /**
  49. * 设置应用名称.
  50. *
  51. * @param string $app
  52. */
  53. public function withName(string $app)
  54. {
  55. $this->name = $app;
  56. }
  57. /**
  58. * 获取当前应用名称.
  59. *
  60. * @return string
  61. */
  62. public function getName()
  63. {
  64. return $this->name ?: static::DEFAULT;
  65. }
  66. /**
  67. * 注册应用.
  68. */
  69. public function boot()
  70. {
  71. $this->registerRoute(static::DEFAULT);
  72. if ($this->getApps()) {
  73. $this->registerMultiAppRoutes();
  74. $this->switch(static::DEFAULT);
  75. }
  76. }
  77. /**
  78. * 注册路由.
  79. *
  80. * @param string|\Closure $pathOrCallback
  81. */
  82. public function routes($pathOrCallback)
  83. {
  84. $this->loadRoutesFrom($pathOrCallback, static::DEFAULT);
  85. if ($apps = $this->getApps()) {
  86. foreach ($apps as $app => $enable) {
  87. if ($enable) {
  88. $this->switch($app);
  89. $this->loadRoutesFrom($pathOrCallback, $app);
  90. }
  91. }
  92. $this->switch(static::DEFAULT);
  93. }
  94. }
  95. protected function registerMultiAppRoutes()
  96. {
  97. foreach ($this->getApps() as $app => $enable) {
  98. if ($enable) {
  99. $this->registerRoute($app);
  100. }
  101. }
  102. }
  103. /**
  104. * @return string
  105. */
  106. public function getCurrentApiRoutePrefix()
  107. {
  108. return $this->getApiRoutePrefix($this->getName());
  109. }
  110. /**
  111. * @param string|null $app
  112. *
  113. * @return string
  114. */
  115. public function getApiRoutePrefix(?string $app = null)
  116. {
  117. return $this->getRoutePrefix($app).'dcat-api.';
  118. }
  119. /**
  120. * 获取路由别名前缀.
  121. *
  122. * @param string|null $app
  123. *
  124. * @return string
  125. */
  126. public function getRoutePrefix(?string $app = null)
  127. {
  128. $app = $app ?: $this->getName();
  129. return 'dcat.'.$app.'.';
  130. }
  131. /**
  132. * 获取路由别名.
  133. *
  134. * @param string|null $route
  135. * @param array $params
  136. * @param bool $absolute
  137. *
  138. * @return string
  139. */
  140. public function getRoute(?string $route, array $params = [], $absolute = true)
  141. {
  142. return route($this->getRoutePrefix().$route, $params, $absolute);
  143. }
  144. /**
  145. * 注册应用路由.
  146. *
  147. * @param string|null $app
  148. */
  149. protected function registerRoute(?string $app)
  150. {
  151. $this->switch($app);
  152. $this->loadRoutesFrom(function () {
  153. Admin::registerApiRoutes();
  154. }, $app);
  155. if (is_file($routes = admin_path('routes.php'))) {
  156. $this->loadRoutesFrom($routes, $app);
  157. }
  158. }
  159. /**
  160. * 设置应用配置.
  161. *
  162. * @param string $app
  163. */
  164. protected function withConfig(string $app)
  165. {
  166. if (! isset($this->configs[$app])) {
  167. $this->configs[$app] = config($app);
  168. }
  169. config(['admin' => $this->configs[$app]]);
  170. }
  171. /**
  172. * 加载路由文件.
  173. *
  174. * @param string $path
  175. * @param string $app
  176. *
  177. * @return void
  178. */
  179. protected function loadRoutesFrom($path, ?string $app)
  180. {
  181. Route::group(array_filter([
  182. 'middleware' => 'admin.app:'.$app,
  183. 'domain' => config("{$app}.route.domain"),
  184. 'as' => $this->getRoutePrefix($app),
  185. ]), $path);
  186. }
  187. }