Application.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 = null)
  113. {
  114. return $this->getRoutePrefix($app).'dcat-api.';
  115. }
  116. /**
  117. * 获取路由别名前缀.
  118. *
  119. * @param string|null $app
  120. *
  121. * @return string
  122. */
  123. public function getRoutePrefix(?string $app = null)
  124. {
  125. $app = $app ?: $this->getName();
  126. return 'dcat.'.$app.'.';
  127. }
  128. /**
  129. * 获取路由别名.
  130. *
  131. * @param string|null $route
  132. * @param array $params
  133. * @param bool $absolute
  134. *
  135. * @return string
  136. */
  137. public function getRoute(?string $route, array $params = [], $absolute = true)
  138. {
  139. return route($this->getRoutePrefix().$route, $params, $absolute);
  140. }
  141. /**
  142. * 注册应用路由.
  143. *
  144. * @param string|null $app
  145. */
  146. protected function registerRoute(?string $app)
  147. {
  148. $this->switch($app);
  149. $this->loadRoutesFrom(function () {
  150. Admin::registerApiRoutes();
  151. }, $app);
  152. if (is_file($routes = admin_path('routes.php'))) {
  153. $this->loadRoutesFrom($routes, $app);
  154. }
  155. }
  156. /**
  157. * 设置应用配置.
  158. *
  159. * @param string $app
  160. */
  161. protected function withConfig(string $app)
  162. {
  163. if (! isset($this->configs[$app])) {
  164. $this->configs[$app] = config($app);
  165. }
  166. config(['admin' => $this->configs[$app]]);
  167. }
  168. /**
  169. * 加载路由文件.
  170. *
  171. * @param string $path
  172. * @param string $app
  173. *
  174. * @return void
  175. */
  176. protected function loadRoutesFrom($path, ?string $app)
  177. {
  178. Route::group(array_filter([
  179. 'middleware' => 'admin.app:'.$app,
  180. 'domain' => config("{$app}.route.domain"),
  181. 'as' => $this->getRoutePrefix($app),
  182. ]), $path);
  183. }
  184. }