jqh 5 vuotta sitten
vanhempi
commit
f1684b2f94

+ 1 - 1
src/Actions/HasActionHandler.php

@@ -66,7 +66,7 @@ trait HasActionHandler
      */
     public function handlerRoute()
     {
-        return route('dcat.api.action');
+        return route(admin_api_route('action'));
     }
 
     /**

+ 13 - 3
src/Admin.php

@@ -230,14 +230,16 @@ class Admin
     /**
      * 注册api路由.
      *
+     * @param string $as
+     *
      * @return void
      */
-    public static function registerApiRoutes()
+    public static function registerApiRoutes(string $as = null)
     {
         $attributes = [
-            'prefix' => admin_base_path('dcat-api'),
+            'prefix'     => admin_base_path('dcat-api'),
             'middleware' => config('admin.route.middleware'),
-            'as' => 'dcat.api.',
+            'as'         => $as,
         ];
 
         app('router')->group($attributes, function ($router) {
@@ -311,6 +313,14 @@ class Admin
         return new Proxy($repository);
     }
 
+    /**
+     * @return Application
+     */
+    public static function app()
+    {
+        return app('admin.app');
+    }
+
     /**
      * 获取所有已注册的扩展.
      *

+ 6 - 7
src/AdminServiceProvider.php

@@ -33,6 +33,7 @@ class AdminServiceProvider extends ServiceProvider
         Console\ActionCommand::class,
         Console\MenuCacheCommand::class,
         Console\MinifyCommand::class,
+        Console\AppCommand::class,
     ];
 
     /**
@@ -55,6 +56,7 @@ class AdminServiceProvider extends ServiceProvider
         'admin.bootstrap'  => Middleware\Bootstrap::class,
         'admin.session'    => Middleware\Session::class,
         'admin.upload'     => Middleware\WebUploader::class,
+        'admin.app'        => Middleware\Application::class,
     ];
 
     /**
@@ -77,7 +79,7 @@ class AdminServiceProvider extends ServiceProvider
         $this->registerDefaultSections();
         $this->registerViews();
         $this->ensureHttps();
-        $this->registerRoutes();
+        $this->bootApplication();
         $this->registerPublishing();
         $this->compatibleBlade();
     }
@@ -127,13 +129,9 @@ class AdminServiceProvider extends ServiceProvider
     /**
      * 路由注册.
      */
-    protected function registerRoutes()
+    protected function bootApplication()
     {
-        Admin::registerApiRoutes();
-
-        if (is_file($routes = admin_path('routes.php'))) {
-            $this->loadRoutesFrom($routes);
-        }
+        Admin::app()->boot();
     }
 
     /**
@@ -211,6 +209,7 @@ class AdminServiceProvider extends ServiceProvider
 
     protected function registerServices()
     {
+        $this->app->singleton('admin.app', Application::class);
         $this->app->singleton('admin.asset', Asset::class);
         $this->app->singleton('admin.color', Color::class);
         $this->app->singleton('admin.sections', SectionManager::class);

+ 148 - 0
src/Application.php

@@ -0,0 +1,148 @@
+<?php
+
+namespace Dcat\Admin;
+
+use Illuminate\Contracts\Container\Container;
+use Illuminate\Support\Facades\Route;
+
+class Application
+{
+    const DEFAULT = 'admin';
+
+    /**
+     * @var Container
+     */
+    protected $app;
+
+    /**
+     * 所有启用应用的配置.
+     *
+     * @var array
+     */
+    protected $configs = [];
+
+    /**
+     * 当前应用名称.
+     *
+     * @var string
+     */
+    protected $name;
+
+    public function __construct(Container $app)
+    {
+        $this->app = $app;
+    }
+
+    /**
+     * 设置当前应用配置.
+     *
+     * @param string $app
+     */
+    public function current(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->app->runningInConsole()) {
+            return;
+        }
+        foreach ((array) config('admin.multi_app') 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)
+    {
+        return "dcat.api.{$app}.";
+    }
+
+    /**
+     * 注册应用路由.
+     *
+     * @param string|null $app
+     */
+    protected function registerRoute(?string $app)
+    {
+        $this->withConfig($app);
+
+        Admin::registerApiRoutes($this->getApiRoutePrefix($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);
+            $this->configs[$app]['current_app'] = $app;
+        }
+
+        config(['admin' => $this->configs[$app]]);
+    }
+
+    /**
+     * 加载路由文件.
+     *
+     * @param  string  $path
+     * @param  string  $app
+     *
+     * @return void
+     */
+    protected function loadRoutesFrom(string $path, ?string $app)
+    {
+        if (! $this->app->routesAreCached()) {
+            Route::middleware('admin.app:'.$app)->group($path);
+        }
+    }
+}

+ 65 - 0
src/Console/AppCommand.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace Dcat\Admin\Console;
+
+use Dcat\Admin\Support\Helper;
+use Illuminate\Filesystem\Filesystem;
+
+class AppCommand extends InstallCommand
+{
+    /**
+     * The console command name.
+     *
+     * @var string
+     */
+    protected $signature = 'admin:app {name}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Create new application';
+
+    /**
+     * Execute the console command.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $this->addConfig();
+        $this->initAdminDirectory();
+
+        $this->info('Done.');
+    }
+
+    protected function addConfig()
+    {
+        /* @var Filesystem $files */
+        $files = $this->laravel['files'];
+
+        $app = Helper::slug($namespace = $this->argument('name'));
+
+        $files->put(
+            $config = config_path($app.'.php'),
+            str_replace(
+                ['DummyNamespace', 'DummyApp'],
+                [$namespace, $app],
+                $files->get(__DIR__.'/stubs/config.stub')
+            )
+        );
+
+        config(['admin' => include $config]);
+    }
+
+    /**
+     * Set admin directory.
+     *
+     * @return void
+     */
+    protected function setDirectory()
+    {
+        $this->directory = app_path($this->argument('name'));
+    }
+}

+ 12 - 2
src/Console/InstallCommand.php

@@ -59,13 +59,23 @@ class InstallCommand extends Command
     }
 
     /**
-     * Initialize the admAin directory.
+     * Set admin directory.
      *
      * @return void
      */
-    protected function initAdminDirectory()
+    protected function setDirectory()
     {
         $this->directory = config('admin.directory');
+    }
+
+    /**
+     * Initialize the admin directory.
+     *
+     * @return void
+     */
+    protected function initAdminDirectory()
+    {
+        $this->setDirectory();
 
         if (is_dir($this->directory)) {
             $this->warn("{$this->directory} directory already exists !");

+ 359 - 0
src/Console/stubs/config.stub

@@ -0,0 +1,359 @@
+<?php
+
+return [
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin name
+    |--------------------------------------------------------------------------
+    |
+    | This value is the name of dcat-admin, This setting is displayed on the
+    | login page.
+    |
+    */
+    'name' => 'Dcat Admin',
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin logo
+    |--------------------------------------------------------------------------
+    |
+    | The logo of all admin pages. You can also set it as an image by using a
+    | `img` tag, eg '<img src="http://logo-url" alt="Admin logo">'.
+    |
+    */
+    'logo' => '<img src="/vendors/dcat-admin/images/logo.png" width="35"> &nbsp;Dcat Admin',
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin mini logo
+    |--------------------------------------------------------------------------
+    |
+    | The logo of all admin pages when the sidebar menu is collapsed. You can
+    | also set it as an image by using a `img` tag, eg
+    | '<img src="http://logo-url" alt="Admin logo">'.
+    |
+    */
+    'logo-mini' => '<img src="/vendors/dcat-admin/images/logo.png">',
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin route settings
+    |--------------------------------------------------------------------------
+    |
+    | The routing configuration of the admin page, including the path prefix,
+    | the controller namespace, and the default middleware. If you want to
+    | access through the root path, just set the prefix to empty string.
+    |
+    */
+    'route' => [
+
+        'prefix' => env('ADMIN_ROUTE_PREFIX', 'DummyApp'),
+
+        'namespace' => 'App\\DummyNamespace\\Controllers',
+
+        'middleware' => ['web', 'admin'],
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin install directory
+    |--------------------------------------------------------------------------
+    |
+    | The installation directory of the controller and routing configuration
+    | files of the administration page. The default is `app/Admin`, which must
+    | be set before running `artisan admin::install` to take effect.
+    |
+    */
+    'directory' => app_path('DummyNamespace'),
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin html title
+    |--------------------------------------------------------------------------
+    |
+    | Html title for all pages.
+    |
+    */
+    'title' => 'Admin',
+
+    /*
+    |--------------------------------------------------------------------------
+    | Assets hostname
+    |--------------------------------------------------------------------------
+    |
+   */
+    'assets_server' => env('ADMIN_ASSETS_SERVER'),
+
+    /*
+    |--------------------------------------------------------------------------
+    | Access via `https`
+    |--------------------------------------------------------------------------
+    |
+    | If your page is going to be accessed via https, set it to `true`.
+    |
+    */
+    'https' => env('ADMIN_HTTPS', false),
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin auth setting
+    |--------------------------------------------------------------------------
+    |
+    | Authentication settings for all admin pages. Include an authentication
+    | guard and a user provider setting of authentication driver.
+    |
+    | You can specify a controller for `login` `logout` and other auth routes.
+    |
+    */
+    'auth' => [
+        'enable' => true,
+
+        'controller' => App\DummyNamespace\Controllers\AuthController::class,
+
+        'guard' => 'admin',
+
+        'guards' => [
+            'admin' => [
+                'driver'   => 'session',
+                'provider' => 'admin',
+            ],
+        ],
+
+        'providers' => [
+            'admin' => [
+                'driver' => 'eloquent',
+                'model'  => Dcat\Admin\Models\Administrator::class,
+            ],
+        ],
+
+        // Add "remember me" to login form
+        'remember' => true,
+
+        // All method to path like: auth/users/*/edit
+        // or specific method to path like: get:auth/users.
+        'except' => [
+            'auth/login',
+            'auth/logout',
+        ],
+
+    ],
+
+    'grid' => [
+
+        /*
+        |--------------------------------------------------------------------------
+        | The global Grid action display class.
+        |--------------------------------------------------------------------------
+        */
+        'grid_action_class' => Dcat\Admin\Grid\Displayers\DropdownActions::class,
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin helpers setting.
+    |--------------------------------------------------------------------------
+    */
+    'helpers' => [
+        'enable' => true,
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin permission setting
+    |--------------------------------------------------------------------------
+    |
+    | Permission settings for all admin pages.
+    |
+    */
+    'permission' => [
+        // Whether enable permission.
+        'enable' => true,
+
+        // All method to path like: auth/users/*/edit
+        // or specific method to path like: get:auth/users.
+        'except' => [
+            '/',
+            'auth/login',
+            'auth/logout',
+            'auth/setting',
+        ],
+
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin menu setting
+    |--------------------------------------------------------------------------
+    |
+    */
+    'menu' => [
+        'cache' => [
+            // enable cache or not
+            'enable' => false,
+            'store'  => 'file',
+        ],
+
+        // Whether enable menu bind to a permission.
+        'bind_permission' => true,
+
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin upload setting
+    |--------------------------------------------------------------------------
+    |
+    | File system configuration for form upload files and images, including
+    | disk and upload path.
+    |
+    */
+    'upload' => [
+
+        // Disk in `config/filesystem.php`.
+        'disk' => 'admin',
+
+        // Image and file upload path under the disk above.
+        'directory' => [
+            'image' => 'images',
+            'file'  => 'files',
+        ],
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | dcat-admin database settings
+    |--------------------------------------------------------------------------
+    |
+    | Here are database settings for dcat-admin builtin model & tables.
+    |
+    */
+    'database' => [
+
+        // Database connection for following tables.
+        'connection' => '',
+
+        // User tables and model.
+        'users_table' => 'admin_users',
+        'users_model' => Dcat\Admin\Models\Administrator::class,
+
+        // Role table and model.
+        'roles_table' => 'admin_roles',
+        'roles_model' => Dcat\Admin\Models\Role::class,
+
+        // Permission table and model.
+        'permissions_table' => 'admin_permissions',
+        'permissions_model' => Dcat\Admin\Models\Permission::class,
+
+        // Menu table and model.
+        'menu_table' => 'admin_menu',
+        'menu_model' => Dcat\Admin\Models\Menu::class,
+
+        // Pivot table for table above.
+        'operation_log_table'    => 'admin_operation_log',
+        'user_permissions_table' => 'admin_user_permissions',
+        'role_users_table'       => 'admin_role_users',
+        'role_permissions_table' => 'admin_role_permissions',
+        'role_menu_table'        => 'admin_role_menu',
+        'permission_menu_table'  => 'admin_permission_menu',
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | User operation log setting
+    |--------------------------------------------------------------------------
+    |
+    | By setting this option to open or close operation log in dcat-admin.
+    |
+    */
+    'operation_log' => [
+
+        'enable' => true,
+
+        // Only logging allowed methods in the list
+        'allowed_methods' => ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'],
+
+        'secret_fields' => [
+            'password',
+            'password_confirmation',
+        ],
+
+        // Routes that will not log to database.
+        // All method to path like: auth/logs/*/edit
+        // or specific method to path like: get:auth/logs.
+        'except' => [
+            'auth/logs*',
+        ],
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | Admin map field provider
+    |--------------------------------------------------------------------------
+    |
+    | Supported: "tencent", "google", "yandex".
+    |
+    */
+    'map_provider' => 'google',
+
+    /*
+    |--------------------------------------------------------------------------
+    | Application layout
+    |--------------------------------------------------------------------------
+    |
+    | This value is the layout of admin pages.
+    */
+    'layout' => [
+        // indigo, blue, blue-light, blue-dark, green
+        'color' => 'indigo',
+
+        'body_class' => '',
+
+        'sidebar_collapsed' => false,
+
+        'sidebar_dark' => false,
+
+        // bg-primary, bg-info, bg-warning, bg-success, bg-danger, bg-dark
+        'navbar_color' => '',
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | Login page background image
+    |--------------------------------------------------------------------------
+    |
+    | This value is used to set the background image of login page.
+    |
+    */
+    'login_background_image' => '',
+
+    /*
+    |--------------------------------------------------------------------------
+    | The exception handler class
+    |--------------------------------------------------------------------------
+    |
+    */
+    'exception_handler' => \Dcat\Admin\Exception\Handler::class,
+
+    /*
+    |--------------------------------------------------------------------------
+    | Enable default breadcrumb
+    |--------------------------------------------------------------------------
+    |
+    | Whether enable default breadcrumb for every page content.
+    */
+    'enable_default_breadcrumb' => true,
+
+    /*
+    |--------------------------------------------------------------------------
+    | Settings for extensions.
+    |--------------------------------------------------------------------------
+    |
+    | You can find all available extensions here
+    | https://github.com/dcat-admin-extensions.
+    |
+    */
+    'extensions' => [
+
+    ],
+];

+ 1 - 1
src/Form/Field/Editor.php

@@ -135,7 +135,7 @@ class Editor extends Field
      */
     protected function defaultImageUploadUrl()
     {
-        return $this->formatUrl(route('dcat.api.tinymce.upload'));
+        return $this->formatUrl(route(admin_api_route('tinymce.upload')));
     }
 
     /**

+ 1 - 1
src/Form/Field/Markdown.php

@@ -166,7 +166,7 @@ JS
      */
     protected function defaultImageUploadUrl()
     {
-        return $this->formatUrl(route('dcat.api.editor-md.upload'));
+        return $this->formatUrl(route(admin_api_route('editor-md.upload')));
     }
 
     /**

+ 24 - 0
src/Middleware/Application.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace Dcat\Admin\Middleware;
+
+use Dcat\Admin\Admin;
+
+class Application
+{
+    public function handle($request, \Closure $next, $app = null)
+    {
+        if ($app) {
+            Admin::app()->current($app);
+
+            $this->withSessionPath();
+        }
+
+        return $next($request);
+    }
+
+    protected function withSessionPath()
+    {
+        config(['session.path' => '/'.trim(config('admin.route.prefix'), '/')]);
+    }
+}

+ 1 - 1
src/Middleware/LogOperation.php

@@ -98,7 +98,7 @@ class LogOperation
      */
     protected function inExceptArray($request)
     {
-        if ($request->routeIs('dcat.api.value')) {
+        if ($request->routeIs(admin_api_route('value'))) {
             return true;
         }
 

+ 15 - 0
src/Support/helpers.php

@@ -1,5 +1,6 @@
 <?php
 
+use Dcat\Admin\Admin;
 use Dcat\Admin\Support\Helper;
 use Illuminate\Contracts\Support\Htmlable;
 use Illuminate\Contracts\Support\Renderable;
@@ -381,3 +382,17 @@ if (! function_exists('admin_asset')) {
         return Dcat\Admin\Admin::asset()->url($path);
     }
 }
+
+
+if (! function_exists('admin_api_route')) {
+
+    /**
+     * @param string $path
+     *
+     * @return string
+     */
+    function admin_api_route(string $path = '')
+    {
+        return Dcat\Admin\Admin::app()->getCurrentApiRoutePrefix().$path;
+    }
+}

+ 1 - 1
src/Traits/InteractsWithApi.php

@@ -78,7 +78,7 @@ trait InteractsWithApi
      */
     public function getRequestUrl()
     {
-        return $this->url ?: route('dcat.api.value');
+        return $this->url ?: route(admin_api_route('value'));
     }
 
     /**

+ 1 - 1
src/Widgets/Form.php

@@ -651,7 +651,7 @@ JS
     {
         if (method_exists($this, 'handle')) {
             $this->method('POST');
-            $this->action(route('dcat.api.form'));
+            $this->action(route(admin_api_route('form')));
             $this->hidden('_form_')->default(get_called_class());
             $this->hidden('_current_')->default($this->getCurrentUrl());
         }