Application.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. public function getEnabledApps()
  39. {
  40. return array_filter($this->getApps());
  41. }
  42. public function switch(string $app = null)
  43. {
  44. $this->withName($app);
  45. $this->withConfig($this->name);
  46. }
  47. public function withName(string $app)
  48. {
  49. $this->name = $app;
  50. }
  51. public function getName()
  52. {
  53. return $this->name ?: static::DEFAULT;
  54. }
  55. public function boot()
  56. {
  57. $this->registerRoute(static::DEFAULT);
  58. if ($this->getApps()) {
  59. $this->registerMultiAppRoutes();
  60. $this->switch(static::DEFAULT);
  61. }
  62. }
  63. public function routes($pathOrCallback)
  64. {
  65. $this->loadRoutesFrom($pathOrCallback, static::DEFAULT);
  66. if ($apps = $this->getApps()) {
  67. foreach ($apps as $app => $enable) {
  68. if ($enable) {
  69. $this->switch($app);
  70. $this->loadRoutesFrom($pathOrCallback, $app);
  71. }
  72. }
  73. $this->switch(static::DEFAULT);
  74. }
  75. }
  76. protected function registerMultiAppRoutes()
  77. {
  78. foreach ($this->getApps() as $app => $enable) {
  79. if ($enable) {
  80. $this->registerRoute($app);
  81. }
  82. }
  83. }
  84. public function getCurrentApiRoutePrefix()
  85. {
  86. return $this->getApiRoutePrefix($this->getName());
  87. }
  88. public function getApiRoutePrefix(?string $app = null)
  89. {
  90. return $this->getRoutePrefix($app).'dcat-api.';
  91. }
  92. public function getRoutePrefix(?string $app = null)
  93. {
  94. $app = $app ?: $this->getName();
  95. return 'dcat.'.$app.'.';
  96. }
  97. public function getRoute(?string $route, array $params = [], $absolute = true)
  98. {
  99. return route($this->getRoutePrefix().$route, $params, $absolute);
  100. }
  101. protected function registerRoute(?string $app)
  102. {
  103. $this->switch($app);
  104. $this->loadRoutesFrom(function () {
  105. Admin::registerApiRoutes();
  106. }, $app);
  107. if (is_file($routes = admin_path('routes.php'))) {
  108. $this->loadRoutesFrom($routes, $app);
  109. }
  110. }
  111. protected function withConfig(string $app)
  112. {
  113. if (! isset($this->configs[$app])) {
  114. $this->configs[$app] = config($app);
  115. }
  116. config(['admin' => $this->configs[$app]]);
  117. }
  118. protected function loadRoutesFrom($path, ?string $app)
  119. {
  120. Route::group(array_filter([
  121. 'middleware' => 'admin.app:'.$app,
  122. 'domain' => config("{$app}.route.domain"),
  123. 'as' => $this->getRoutePrefix($app),
  124. ]), $path);
  125. }
  126. }