Jiang qinghua 6 anni fa
parent
commit
c86a53bd72
2 ha cambiato i file con 142 aggiunte e 24 eliminazioni
  1. 60 19
      src/AdminServiceProvider.php
  2. 82 5
      src/Form/Concerns/FieldValidator.php

+ 60 - 19
src/AdminServiceProvider.php

@@ -63,26 +63,70 @@ class AdminServiceProvider extends ServiceProvider
      * @return void
      */
     public function boot()
+    {
+        $this->registerViews();
+        $this->ensureHttps();
+        $this->registerRoutes();
+        $this->registerPublishing();
+        $this->compatibleBlade();
+    }
+
+    /**
+     * Register the service provider.
+     *
+     * @return void
+     */
+    public function register()
+    {
+        require_once __DIR__ . '/Support/AdminSection.php';
+
+        $this->registerExtensionProviders();
+        $this->loadAdminAuthConfig();
+        $this->registerRouteMiddleware();
+        $this->registerService();
+        $this->registerDefaultSections();
+
+        $this->commands($this->commands);
+    }
+
+    /**
+     * Register the view file namespace.
+     */
+    protected function registerViews()
     {
         $this->loadViewsFrom(__DIR__.'/../resources/views', 'admin');
+    }
 
+    /**
+     * Force to set https scheme if https enabled.
+     *
+     * @return void
+     */
+    protected function ensureHttps()
+    {
         if (config('admin.https') || config('admin.secure')) {
             \URL::forceScheme('https');
             $this->app['request']->server->set('HTTPS', true);
         }
+    }
 
+    /**
+     * Register routes
+     */
+    protected function registerRoutes()
+    {
         if (is_file($routes = admin_path('routes.php'))) {
             $this->loadRoutesFrom($routes);
         }
+    }
 
-        if ($this->app->runningInConsole()) {
-            $this->publishes([__DIR__.'/../config' => config_path()], 'dcat-admin-config');
-            $this->publishes([__DIR__.'/../resources/lang' => resource_path('lang')], 'dcat-admin-lang');
-            $this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'dcat-admin-migrations');
-            $this->publishes([__DIR__.'/../resources/assets' => public_path('vendor/dcat-admin')], 'dcat-admin-assets');
-        }
-
-        //remove default feature of double encoding enable in laravel 5.6 or later.
+    /**
+     * Remove default feature of double encoding enable in laravel 5.6 or later.
+     *
+     * @return void
+     */
+    protected function compatibleBlade()
+    {
         $bladeReflectionClass = new \ReflectionClass('\Illuminate\View\Compilers\BladeCompiler');
         if ($bladeReflectionClass->hasMethod('withoutDoubleEncoding')) {
             Blade::withoutDoubleEncoding();
@@ -90,21 +134,18 @@ class AdminServiceProvider extends ServiceProvider
     }
 
     /**
-     * Register the service provider.
+     * Register the package's publishable resources.
      *
      * @return void
      */
-    public function register()
+    protected function registerPublishing()
     {
-        require_once __DIR__ . '/Support/AdminSection.php';
-
-        $this->registerExtensionProviders();
-        $this->loadAdminAuthConfig();
-        $this->registerRouteMiddleware();
-        $this->registerService();
-        $this->registerDefaultSections();
-
-        $this->commands($this->commands);
+        if ($this->app->runningInConsole()) {
+            $this->publishes([__DIR__.'/../config' => config_path()], 'dcat-admin-config');
+            $this->publishes([__DIR__.'/../resources/lang' => resource_path('lang')], 'dcat-admin-lang');
+            $this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'dcat-admin-migrations');
+            $this->publishes([__DIR__.'/../resources/assets' => public_path('vendor/dcat-admin')], 'dcat-admin-assets');
+        }
     }
 
     /**

+ 82 - 5
src/Form/Concerns/FieldValidator.php

@@ -187,21 +187,84 @@ trait FieldValidator
         return $rules;
     }
 
+    /**
+     * @param string $rule
+     *
+     * @return $this
+     */
+    public function removeUpdateRule($rule)
+    {
+        $this->deleteRuleByKeyword($this->updateRules, $rule);
+
+        return $this;
+    }
+
+    /**
+     * @param string $rule
+     *
+     * @return $this
+     */
+    public function removeCreationRule($rule)
+    {
+        $this->deleteRuleByKeyword($this->creationRules, $rule);
+
+        return $this;
+    }
+
     /**
      * Remove a specific rule by keyword.
      *
      * @param string $rule
      *
-     * @return void
+     * @return $this
      */
     public function removeRule($rule)
     {
-        if (!$this->rules || !is_string($this->rules)) {
+        $this->deleteRuleByKeyword($this->rules, $rule);
+
+        return $this;
+    }
+
+    /**
+     * @param $rules
+     * @param $rule
+     * @return void
+     */
+    protected function deleteRuleByKeyword(&$rules, $rule)
+    {
+        if (is_array($rules)) {
+            array_delete($rules, $rule);
+
+            return;
+        }
+
+        if (!is_string($rules)) {
             return;
         }
 
         $pattern = "/{$rule}[^\|]?(\||$)/";
-        $this->rules = preg_replace($pattern, '', $this->rules, -1);
+
+        $rules = preg_replace($pattern, '', $rules, -1);
+    }
+
+    /**
+     * @param string $rule
+     *
+     * @return bool
+     */
+    public function hasUpdateRule($rule)
+    {
+        return $this->isRuleExists($this->updateRules, $rule);
+    }
+
+    /**
+     * @param string $rule
+     *
+     * @return bool
+     */
+    public function hasCreationRule($rule)
+    {
+        return $this->isRuleExists($this->creationRules, $rule);
     }
 
     /**
@@ -211,13 +274,27 @@ trait FieldValidator
      */
     public function hasRule($rule)
     {
-        if (!$this->rules || !is_string($this->rules)) {
+        return $this->isRuleExists($this->rules, $rule);
+    }
+
+    /**
+     * @param $rules
+     * @param $rule
+     * @return bool
+     */
+    protected function isRuleExists($rules, $rule)
+    {
+        if (is_array($rules)) {
+            return in_array($rule, $rules);
+        }
+
+        if (!is_string($rules)) {
             return false;
         }
 
         $pattern = "/{$rule}[^\|]?(\||$)/";
 
-        return preg_match($pattern, $this->rules);
+        return (bool)preg_match($pattern, $rules);
     }
 
     /**