123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace Dcat\Admin;
- use Illuminate\Contracts\Container\Container;
- use Illuminate\Support\Facades\Route;
- use Illuminate\Support\Traits\Macroable;
- class Application
- {
- use Macroable;
- const DEFAULT = 'admin';
- /**
- * @var Container
- */
- protected $container;
- /**
- * @var array
- */
- protected $apps;
- /**
- * 所有启用应用的配置.
- *
- * @var array
- */
- protected $configs = [];
- /**
- * 当前应用名称.
- *
- * @var string
- */
- protected $name;
- public function __construct(Container $app)
- {
- $this->container = $app;
- }
- public function getApps()
- {
- return $this->apps ?: ($this->apps = (array) config('admin.multi_app'));
- }
- /**
- * 设置当前应用配置.
- *
- * @param string $app
- */
- public function switch(string $app = null)
- {
- $this->withName($app);
- $this->withConfig($this->name);
- }
- /**
- * 设置应用名称.
- *
- * @param string $app
- */
- public function withName(string $app)
- {
- $this->name = $app;
- }
- /**
- * 获取当前应用名称.
- *
- * @return string
- */
- public function getName()
- {
- return $this->name ?: static::DEFAULT;
- }
- /**
- * 注册应用.
- */
- public function boot()
- {
- $this->registerRoute(static::DEFAULT);
- if ($this->getApps()) {
- $this->registerMultiAppRoutes();
- $this->switch(static::DEFAULT);
- }
- }
- /**
- * 注册路由.
- *
- * @param string|\Closure $pathOrCallback
- */
- public function routes($pathOrCallback)
- {
- $this->loadRoutesFrom($pathOrCallback, static::DEFAULT);
- if ($apps = $this->getApps()) {
- foreach ($apps as $app => $enable) {
- if ($enable) {
- $this->switch($app);
- $this->loadRoutesFrom($pathOrCallback, $app);
- }
- }
- $this->switch(static::DEFAULT);
- }
- }
- protected function registerMultiAppRoutes()
- {
- foreach ($this->getApps() as $app => $enable) {
- if ($enable) {
- $this->registerRoute($app);
- }
- }
- }
- /**
- * @return string
- */
- public function getCurrentApiRoutePrefix()
- {
- return $this->getApiRoutePrefix($this->getName());
- }
- /**
- * @param string|null $app
- *
- * @return string
- */
- public function getApiRoutePrefix(?string $app = null)
- {
- return $this->getRoutePrefix($app).'dcat-api.';
- }
- /**
- * 获取路由别名前缀.
- *
- * @param string|null $app
- *
- * @return string
- */
- public function getRoutePrefix(?string $app = null)
- {
- $app = $app ?: $this->getName();
- return 'dcat.'.$app.'.';
- }
- /**
- * 获取路由别名.
- *
- * @param string|null $route
- * @param array $params
- * @param bool $absolute
- *
- * @return string
- */
- public function getRoute(?string $route, array $params = [], $absolute = true)
- {
- return route($this->getRoutePrefix().$route, $params, $absolute);
- }
- /**
- * 注册应用路由.
- *
- * @param string|null $app
- */
- protected function registerRoute(?string $app)
- {
- $this->switch($app);
- $this->loadRoutesFrom(function () {
- Admin::registerApiRoutes();
- }, $app);
- if (is_file($routes = admin_path('routes.php'))) {
- $this->loadRoutesFrom($routes, $app);
- }
- }
- /**
- * 设置应用配置.
- *
- * @param string $app
- */
- protected function withConfig(string $app)
- {
- if (! isset($this->configs[$app])) {
- $this->configs[$app] = config($app);
- }
- config(['admin' => $this->configs[$app]]);
- }
- /**
- * 加载路由文件.
- *
- * @param string $path
- * @param string $app
- *
- * @return void
- */
- protected function loadRoutesFrom($path, ?string $app)
- {
- Route::group(array_filter([
- 'middleware' => 'admin.app:'.$app,
- 'domain' => config("{$app}.route.domain"),
- 'as' => $this->getRoutePrefix($app),
- ]), $path);
- }
- }
|