Просмотр исходного кода

:construction: extension command

jqh 5 лет назад
Родитель
Сommit
65c7f78f37

+ 11 - 11
resources/views/form/hasmany.blade.php

@@ -4,15 +4,15 @@
     <div class="{{$viewClass['field']}}"></div>
 </div>
 
-<hr style="margin-top: 0px;">
+<hr class="mt-0">
 
-<div class="has-many-{{$column}}">
+<div class="has-many-{{$columnClass}}">
 
-    <div class="has-many-{{$column}}-forms">
+    <div class="has-many-{{$columnClass}}-forms">
 
         @foreach($forms as $pk => $form)
 
-            <div class="has-many-{{$column}}-form fields-group">
+            <div class="has-many-{{$columnClass}}-form fields-group">
 
                 @foreach($form->fields() as $field)
                     {!! $field->render() !!}
@@ -33,8 +33,8 @@
     </div>
     
 
-    <template class="{{$column}}-tpl">
-        <div class="has-many-{{$column}}-form fields-group">
+    <template class="{{$columnClass}}-tpl">
+        <div class="has-many-{{$columnClass}}-form fields-group">
 
             {!! $template !!}
 
@@ -61,8 +61,8 @@
 
 <script>
     var nestedIndex = {!! $count !!},
-        container = '.has-many-{{ $column }}',
-        forms = '.has-many-{{ $column  }}-forms';
+        container = '.has-many-{{ $columnClass }}',
+        forms = '.has-many-{{ $columnClass  }}-forms';
 
     function replaceNestedFormIndex(value) {
         return String(value).replace(/{{ Dcat\Admin\Form\NestedForm::DEFAULT_KEY_NAME }}/g, nestedIndex);
@@ -70,7 +70,7 @@
 
     $(container).on('click', '.add', function () {
 
-        var tpl = $('template.{{ $column }}-tpl');
+        var tpl = $('template.{{ $columnClass }}-tpl');
 
         nestedIndex++;
 
@@ -80,7 +80,7 @@
     });
 
     $(container).on('click', '.remove', function () {
-        $(this).closest('.has-many-{{ $column  }}-form').hide();
-        $(this).closest('.has-many-{{ $column  }}-form').find('.{{ Dcat\Admin\Form\NestedForm::REMOVE_FLAG_CLASS }}').val(1);
+        $(this).closest('.has-many-{{ $columnClass  }}-form').hide();
+        $(this).closest('.has-many-{{ $columnClass  }}-form').find('.{{ Dcat\Admin\Form\NestedForm::REMOVE_FLAG_CLASS }}').val(1);
     });
 </script>

+ 4 - 4
resources/views/form/hasmanytab.blade.php

@@ -11,7 +11,7 @@
         display: none;
     }
 </style>
-<div class="nav-tabs-custom has-many-{{$column}}">
+<div class="nav-tabs-custom has-many-{{$columnClass}}">
     <div class="row header">
         <div class="{{$viewClass['label']}}"><h4 class="pull-right">{!! $label !!}</h4></div>
         <div class="{{$viewClass['field']}}" style="margin-bottom: 5px">
@@ -33,10 +33,10 @@
 
     </ul>
     
-    <div class="tab-content has-many-{{$column}}-forms">
+    <div class="tab-content has-many-{{$columnClass}}-forms">
 
         @foreach($forms as $pk => $form)
-            <div class="tab-pane fields-group has-many-{{$column}}-form @if ($form == reset($forms)) active @endif" id="{{ $relationName . '_' . $pk }}">
+            <div class="tab-pane fields-group has-many-{{$columnClass}}-form @if ($form == reset($forms)) active @endif" id="{{ $relationName . '_' . $pk }}">
                 @foreach($form->fields() as $field)
                     {!! $field->render() !!}
                 @endforeach
@@ -61,7 +61,7 @@
 </div>
 
 <script>
-    var container = '.has-many-{{ $column }}';
+    var container = '.has-many-{{ $columnClass }}';
     
     $(container+' > .nav').off('click', 'i.close-tab').on('click', 'i.close-tab', function(){
         var $navTab = $(this).siblings('a');

+ 12 - 12
resources/views/form/hasmanytable.blade.php

@@ -9,8 +9,8 @@
 
         <span name="{{$column}}"></span> {{-- 用于显示错误信息 --}}
 
-        <div class="has-many-{{$column}}" >
-            <table class="table table-has-many has-many-{{$column}}">
+        <div class="has-many-{{$columnClass}}" >
+            <table class="table table-has-many has-many-{{$columnClass}}">
                 <thead>
                 <tr>
                     @foreach($headers as $header)
@@ -24,15 +24,15 @@
                     @endif
                 </tr>
                 </thead>
-                <tbody class="has-many-{{$column}}-forms">
+                <tbody class="has-many-{{$columnClass}}-forms">
                 @foreach($forms as $pk => $form)
-                    <tr class="has-many-{{$column}}-form fields-group">
+                    <tr class="has-many-{{$columnClass}}-form fields-group">
 
                         <?php $hidden = ''; ?>
 
                         @foreach($form->fields() as $field)
 
-                            @if (is_a($field, \Dcat\Admin\Form\Field\Hidden::class))
+                            @if (is_a($field, Dcat\Admin\Form\Field\Hidden::class))
                                 <?php $hidden .= $field->render(); ?>
                                 @continue
                             @endif
@@ -54,8 +54,8 @@
                 </tbody>
             </table>
 
-            <template class="{{$column}}-tpl">
-                <tr class="has-many-{{$column}}-form fields-group">
+            <template class="{{$columnClass}}-tpl">
+                <tr class="has-many-{{$columnClass}}-form fields-group">
 
                     {!! $template !!}
 
@@ -82,25 +82,25 @@
 
 <script>
     var nestedIndex = {!! $count !!},
-        container = '.has-many-{{ $column }}';
+        container = '.has-many-{{ $columnClass }}';
 
     function replaceNestedFormIndex(value) {
         return String(value).replace(/{{ Dcat\Admin\Form\NestedForm::DEFAULT_KEY_NAME }}/g, nestedIndex);
     }
 
     $(container).on('click', '.add', function () {
-        var tpl = $('template.{{ $column }}-tpl');
+        var tpl = $('template.{{ $columnClass }}-tpl');
 
         nestedIndex++;
 
         var template = replaceNestedFormIndex(tpl.html());
-        $('.has-many-{{ $column }}-forms').append(template);
+        $('.has-many-{{ $columnClass }}-forms').append(template);
         {!! $templateScript !!}
     });
 
     $(container).on('click', '.remove', function () {
-        $(this).closest('.has-many-{{ $column }}-form').hide();
-        $(this).closest('.has-many-{{ $column }}-form').find('.{{ Dcat\Admin\Form\NestedForm::REMOVE_FLAG_CLASS }}').val(1);
+        $(this).closest('.has-many-{{ $columnClass }}-form').hide();
+        $(this).closest('.has-many-{{ $columnClass }}-form').find('.{{ Dcat\Admin\Form\NestedForm::REMOVE_FLAG_CLASS }}').val(1);
     });
 </script>
 

+ 1 - 1
src/Console/Development/LinkCommand.php

@@ -13,7 +13,7 @@ class LinkCommand extends Command
      *
      * @var string
      */
-    protected $signature = 'admin:dev:link';
+    protected $signature = 'admin:dev';
 
     /**
      * Execute the console command.

+ 12 - 1
src/Console/ExtensionDiableCommand.php

@@ -2,16 +2,27 @@
 
 namespace Dcat\Admin\Console;
 
+use Dcat\Admin\Admin;
 use Illuminate\Console\Command;
 
 class ExtensionDiableCommand extends Command
 {
     protected $signature = 'admin:extension-disable {name : The name of the extension. Eg: author-name/extension-name} ';
 
-    protected $description = 'Disable an existing extension.';
+    protected $description = 'Disable an existing extension';
 
     public function handle()
     {
+        $extensionManager = Admin::extension();
+
         $name = $this->argument('name');
+
+        if (! $extensionManager->has($name)) {
+            return $this->error(sprintf('Unable to find a registered extension called "%s"', $name));
+        }
+
+        $extensionManager->enable($name, false);
+
+        $this->output->writeln(sprintf('<info>%s:</info> disabled.', $name));
     }
 }

+ 9 - 1
src/Console/ExtensionEnableCommand.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Console;
 
+use Dcat\Admin\Admin;
 use Illuminate\Console\Command;
 
 class ExtensionEnableCommand extends Command
@@ -9,13 +10,20 @@ class ExtensionEnableCommand extends Command
     protected $signature = 'admin:extension-enable 
     {name : The name of the extension. Eg: author-name/extension-name}';
 
-    protected $description = 'Enable an existing extension.';
+    protected $description = 'Enable an existing extension';
 
     public function handle()
     {
+        $extensionManager = Admin::extension();
+
         $name = $this->argument('name');
 
+        if (! $extensionManager->has($name)) {
+            return $this->error(sprintf('Unable to find a registered extension called "%s"', $name));
+        }
 
+        $extensionManager->enable($name);
 
+        $this->output->writeln(sprintf('<info>%s:</info> enabled.', $name));
     }
 }

+ 1 - 1
src/Console/ExtensionInstallCommand.php

@@ -12,7 +12,7 @@ class ExtensionInstallCommand extends Command
     {name : The name of the extension. Eg: author-name/extension-name} 
     {--path= : The path of the extension.}';
 
-    protected $description = 'Install an extension.';
+    protected $description = 'Install an extension';
 
     public function handle()
     {

+ 22 - 2
src/Console/ExtensionRefreshCommand.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Console;
 
+use Dcat\Admin\Admin;
 use Illuminate\Console\Command;
 
 class ExtensionRefreshCommand extends Command
@@ -10,11 +11,30 @@ class ExtensionRefreshCommand extends Command
     {name : The name of the extension. Eg: author-name/extension-name} 
     {--path= : The path of the extension.}';
 
-    protected $description = 'Removes and re-adds an existing extension.';
+    protected $description = 'Removes and re-adds an existing extension';
 
     public function handle()
     {
         $name = $this->argument('name');
-        $path = $this->argument('path');
+
+        if (! Admin::extension()->has($name)) {
+            throw new \InvalidArgumentException(sprintf('Plugin "%s" not found.', $name));
+        }
+
+        $confirmQuestion = 'Please confirm that you wish to remove and re-add this extension?';
+
+        if (! $this->confirm($confirmQuestion)) {
+            return;
+        }
+
+        $manager = Admin::extension()
+            ->updateManager()
+            ->setOutPut($this->output);
+
+        $manager->rollback($name);
+
+        $this->output->writeln('<info>Reinstalling extension...</info>');
+
+        $manager->update($name);
     }
 }

+ 34 - 2
src/Console/ExtensionRollbackCommand.php

@@ -2,18 +2,50 @@
 
 namespace Dcat\Admin\Console;
 
+use Dcat\Admin\Admin;
 use Illuminate\Console\Command;
 
 class ExtensionRollbackCommand extends Command
 {
     protected $signature = 'admin:extension-rollback 
      {name : The name of the extension. Eg: author-name/extension-name} 
-     {--ver= : If this parameter is specified, the process will stop on the specified version, if not, it will completely rollback the extension. Example: 1.3.9} 
+     {ver : If this parameter is specified, the process will stop on the specified version, if not, it will completely rollback the extension. Example: 1.3.9} 
      {--force : Force rollback}';
 
-    protected $description = 'Rollback an existing extension.';
+    protected $description = 'Rollback an existing extension';
 
     public function handle()
     {
+        $name = $this->argument('name');
+
+        if (! Admin::extension()->has($name)) {
+            throw new \InvalidArgumentException('Extension not found');
+        }
+
+        $stopOnVersion = ltrim(($this->argument('ver') ?: null), 'v');
+
+        if ($stopOnVersion) {
+            if (! Admin::extension()->versionManager()->hasDatabaseVersion($name, $stopOnVersion)) {
+                throw new \InvalidArgumentException('Extension version not found');
+            }
+            $confirmQuestion = 'Please confirm that you wish to revert the extension to version '.$stopOnVersion.'. This may result in changes to your database and potential data loss.';
+        } else {
+            $confirmQuestion = 'Please confirm that you wish to completely rollback this extension. This may result in potential data loss.';
+        }
+
+        if ($this->option('force') || $this->confirm($confirmQuestion)) {
+            try {
+                Admin::extension()
+                    ->updateManager()
+                    ->setOutPut($this->output)
+                    ->rollback($name, $stopOnVersion);
+            } catch (\Throwable $exception) {
+                $lastVersion = Admin::extension()->versionManager()->getCurrentVersion($name);
+
+                $this->output->writeln(sprintf("<comment>An exception occurred during the rollback and the process has been stopped. The extension was rolled back to version v%s.</comment>", $lastVersion));
+
+                throw $exception;
+            }
+        }
     }
 }

+ 20 - 1
src/Console/ExtensionUninstallCommand.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Console;
 
+use Dcat\Admin\Admin;
 use Illuminate\Console\Command;
 
 class ExtensionUninstallCommand extends Command
@@ -9,9 +10,27 @@ class ExtensionUninstallCommand extends Command
     protected $signature = 'admin:extension-uninstall 
     {name : The name of the extension. Eg: author-name/extension-name}';
 
-    protected $description = 'Uninstall an existing extension.';
+    protected $description = 'Uninstall an existing extension';
 
     public function handle()
     {
+        $name = $this->argument('name');
+
+        $confirmQuestion = 'Please confirm that you wish to completely rollback this extension. This may result in potential data loss.';
+
+        if ($this->confirm($confirmQuestion)) {
+            try {
+                Admin::extension()
+                    ->updateManager()
+                    ->setOutPut($this->output)
+                    ->rollback($name);
+            } catch (\Throwable $exception) {
+                $lastVersion = Admin::extension()->versionManager()->getCurrentVersion($name);
+
+                $this->output->writeln(sprintf("<comment>An exception occurred during the rollback and the process has been stopped. The extension was rolled back to version v%s.</comment>", $lastVersion));
+
+                throw $exception;
+            }
+        }
     }
 }

+ 2 - 4
src/Console/ExtensionUpdateCommand.php

@@ -11,12 +11,12 @@ class ExtensionUpdateCommand extends Command
     {name : The name of the extension. Eg: author-name/extension-name}
     {--ver= : If this parameter is specified, the process will stop on the specified version, if not, it will update to the latest version. Example: 1.3.9}';
 
-    protected $description = 'Update an existing extension.';
+    protected $description = 'Update an existing extension';
 
     public function handle()
     {
         $name = $this->argument('name');
-        $version = $this->option('ver');
+        $version = ltrim($this->option('ver'), 'v');
 
         Admin::extension()->load();
 
@@ -25,6 +25,4 @@ class ExtensionUpdateCommand extends Command
             ->setOutPut($this->output)
             ->update($name, $version);
     }
-
-
 }

+ 41 - 4
src/Extend/Manager.php

@@ -4,7 +4,9 @@ namespace Dcat\Admin\Extend;
 
 use Dcat\Admin\Admin;
 use Dcat\Admin\Exception\AdminException;
+use Dcat\Admin\Exception\RuntimeException;
 use Dcat\Admin\Models\Extension as ExtensionModel;
+use Dcat\Admin\Models\Extension;
 use Dcat\Admin\Support\Composer;
 use Dcat\Admin\Support\Zip;
 use Illuminate\Contracts\Container\Container;
@@ -82,13 +84,36 @@ class Manager
     /**
      * 判断扩展是否启用.
      *
-     * @param string|null $slug
+     * @param string|null $name
      *
      * @return bool
      */
-    public function enabled(?string $slug)
+    public function enabled(?string $name)
     {
-        return (bool) optional($this->settings()->get($slug))->is_enabled;
+        return (bool) optional($this->settings()->get($name))->is_enabled;
+    }
+
+    /**
+     * 启用或禁用扩展.
+     *
+     * @param string|null $name
+     * @param bool        $enable
+     *
+     * @return void
+     */
+    public function enable(?string $name, bool $enable = true)
+    {
+        $name = $this->getName($name);
+
+        $extension = Extension::where('name', $name)->first();
+
+        if (! $extension) {
+            throw new RuntimeException(sprintf('Please install the extension(%s) first!', $name));
+        }
+
+        $extension->is_enabled = $enable;
+
+        $extension->save();
     }
 
     /**
@@ -138,6 +163,18 @@ class Manager
         return $this->extensions->get($this->formatName($name));
     }
 
+    /**
+     * 判断插件是否存在.
+     *
+     * @param string $name
+     *
+     * @return bool
+     */
+    public function has($name)
+    {
+        return $this->extensions->has($this->formatName($name));
+    }
+
     /**
      * @param string $name
      *
@@ -317,7 +354,7 @@ class Manager
     {
         if ($this->settings === null) {
             try {
-                $this->settings = ExtensionModel::all()->keyBy('slug');
+                $this->settings = ExtensionModel::all()->keyBy('name');
             } catch (\Throwable $e) {
                 $this->reportException($e);
 

+ 3 - 5
src/Extend/ServiceProvider.php

@@ -94,9 +94,7 @@ abstract class ServiceProvider extends LaravelServiceProvider
             $this->registerRoutes($routes);
         }
 
-        $this->publishAssets();
-
-        $this->registerAssets();
+        $this->aliasAssets();
     }
 
     /**
@@ -215,7 +213,7 @@ abstract class ServiceProvider extends LaravelServiceProvider
     /**
      * 发布静态资源.
      */
-    protected function publishAssets()
+    public function publishable()
     {
         if ($assets = $this->getAssetPath()) {
             $this->publishes([
@@ -366,7 +364,7 @@ abstract class ServiceProvider extends LaravelServiceProvider
     /**
      * 注册别名.
      */
-    protected function registerAssets()
+    protected function aliasAssets()
     {
         if ($this->js || $this->css) {
             Admin::asset()->alias($this->getName(), $this->js, $this->css);

+ 10 - 1
src/Extend/UpdateManager.php

@@ -94,8 +94,17 @@ class UpdateManager
         return $this;
     }
 
-    protected function publish($name)
+    /**
+     * 发布扩展资源.
+     *
+     * @param string $name
+     */
+    public function publish($name)
     {
+        $name = $this->manager->getName($name);
+
+        $this->manager->get($name)->publishable();
+
         Artisan::call('vendor:publish', ['--force' => true, '--tag' => $name]);
     }
 

+ 4 - 4
src/Extend/VersionManager.php

@@ -110,18 +110,18 @@ class VersionManager
         $extensionHistory = array_reverse($extensionHistory);
 
         $stopOnNextVersion = false;
-        $newPluginVersion = null;
+        $newExtensionVersion = null;
 
         try {
             foreach ($extensionHistory as $history) {
                 if ($stopCurrentVersion && $stopOnVersion === $history->version) {
-                    $newPluginVersion = $history->version;
+                    $newExtensionVersion = $history->version;
 
                     break;
                 }
 
                 if ($stopOnNextVersion && $history->version !== $stopOnVersion) {
-                    $newPluginVersion = $history->version;
+                    $newExtensionVersion = $history->version;
 
                     break;
                 }
@@ -144,7 +144,7 @@ class VersionManager
             throw $exception;
         }
 
-        $this->setDatabaseVersion($name, $newPluginVersion);
+        $this->setDatabaseVersion($name, $newExtensionVersion);
 
         if (isset($this->fileVersions[$name])) {
             unset($this->fileVersions[$name]);

+ 2 - 2
src/Form/Field.php

@@ -614,7 +614,7 @@ class Field implements Renderable
             return $this->value;
         }
 
-        $this->value = $value;
+        $this->value = value($value);
 
         return $this;
     }
@@ -670,7 +670,7 @@ class Field implements Renderable
             return $this->default;
         }
 
-        $this->default = $default;
+        $this->default = value($default);
         $this->allowDefaultValueInEditPage = $edit;
 
         return $this;

+ 11 - 0
src/Form/Field/HasMany.php

@@ -73,6 +73,8 @@ class HasMany extends Field
         'allowDelete' => true,
     ];
 
+    protected $columnClass;
+
     /**
      * Create a new HasMany field instance.
      *
@@ -85,6 +87,8 @@ class HasMany extends Field
 
         $this->column = $relationName;
 
+        $this->columnClass = $this->formatClass($relationName);
+
         if (count($arguments) == 1) {
             $this->label = $this->formatLabel();
             $this->builder = $arguments[0];
@@ -95,6 +99,11 @@ class HasMany extends Field
         }
     }
 
+    protected function formatClass(string $column)
+    {
+        return str_replace('.', '-', $column);
+    }
+
     /**
      * Get validator for this field.
      *
@@ -496,6 +505,7 @@ class HasMany extends Field
             'options'        => $this->options,
             'templateScript' => $script,
             'count'          => count($this->value()),
+            'columnClass'    => $this->columnClass,
         ]);
 
         return parent::render();
@@ -558,6 +568,7 @@ class HasMany extends Field
             'options'        => $this->options,
             'templateScript' => implode(";\r\n", $scripts),
             'count'          => count($this->value()),
+            'columnClass'    => $this->columnClass,
         ]);
 
         return parent::render();