Bladeren bron

增加生成action类命令

jqh 5 jaren geleden
bovenliggende
commit
f279184cd6

+ 1 - 1
src/Actions/ActionHandler.php

@@ -231,7 +231,7 @@ JS;
     }
 
     /**
-     * @param Model|Authenticatable|HasPermissions $user
+     * @param Model|Authenticatable|HasPermissions|null $user
      *
      * @return bool
      */

+ 1 - 0
src/AdminServiceProvider.php

@@ -28,6 +28,7 @@ class AdminServiceProvider extends ServiceProvider
         Console\ExportSeedCommand::class,
         Console\IdeHelperCommand::class,
         Console\FormCommand::class,
+        Console\ActionCommand::class,
     ];
 
     /**

+ 155 - 0
src/Console/ActionCommand.php

@@ -0,0 +1,155 @@
+<?php
+
+namespace Dcat\Admin\Console;
+
+use Illuminate\Console\GeneratorCommand;
+use Illuminate\Support\Str;
+
+class ActionCommand extends GeneratorCommand
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'admin:action';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Make a admin action';
+
+    /**
+     * @var string
+     */
+    protected $choice;
+
+    /**
+     * @var string
+     */
+    protected $className;
+
+    /**
+     * @var string
+     */
+    protected $namespace;
+
+    /**
+     * @var array
+     */
+    protected $namespaceMap = [
+        'grid-batch' => 'Grid',
+        'grid-row'   => 'Grid',
+        'grid-tool'  => 'Grid',
+        'form-tool'  => 'Form',
+        'show-tool'  => 'Show',
+        'tree-tool'  => 'Tree',
+    ];
+
+    public function handle()
+    {
+        $this->choice = $this->choice(
+            "Which type of action would you like to make?",
+            $choices = $this->actionTyps()
+        );
+
+        INPUT_NAME:
+
+        $this->className = ucfirst(trim($this->ask('Please enter a name of action class')));
+
+        if (! $this->className) {
+            goto INPUT_NAME;
+        }
+
+        $this->namespace = ucfirst(trim($this->ask('Please enter the namespace of action class', $this->getDefaultNamespace(null))));
+
+        return parent::handle();
+    }
+
+    /**
+     * @return array
+     */
+    protected function actionTyps()
+    {
+        return [
+            'default',
+            'grid-batch',
+            'grid-row',
+            'grid-tool',
+            'form-tool',
+            'show-tool',
+            'tree-tool',
+        ];
+    }
+
+    /**
+     * Replace the class name for the given stub.
+     *
+     * @param string $stub
+     * @param string $name
+     *
+     * @return string
+     */
+    protected function replaceClass($stub, $name)
+    {
+        $stub = parent::replaceClass($stub, $name);
+
+        return str_replace(
+            [
+                'DummyName',
+            ],
+            [
+                $this->className,
+            ],
+            $stub
+        );
+    }
+
+    /**
+     * Get the stub file for the generator.
+     *
+     * @return string
+     */
+    public function getStub()
+    {
+        return __DIR__."/stubs/actions/{$this->choice}.stub";
+    }
+
+    /**
+     * Get the default namespace for the class.
+     *
+     * @param string $rootNamespace
+     *
+     * @return string
+     */
+    protected function getDefaultNamespace($rootNamespace)
+    {
+        if ($this->namespace) {
+            return $this->namespace;
+        }
+
+        $segments = explode('\\', config('admin.route.namespace'));
+        array_pop($segments);
+        array_push($segments, 'Actions');
+
+        if (isset($this->namespaceMap[$this->choice])) {
+            array_push($segments, $this->namespaceMap[$this->choice]);
+        }
+
+        return implode('\\', $segments);
+    }
+
+    /**
+     * Get the desired class name from the input.
+     *
+     * @return string
+     */
+    protected function getNameInput()
+    {
+        $this->type = $this->qualifyClass($this->className);
+
+        return $this->className;
+    }
+}

+ 61 - 0
src/Console/stubs/actions/default.stub

@@ -0,0 +1,61 @@
+<?php
+
+namespace DummyNamespace;
+
+use Dcat\Admin\Actions\Action;
+use Dcat\Admin\Actions\Response;
+use Dcat\Admin\Models\HasPermissions;
+use Illuminate\Contracts\Auth\Authenticatable;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Http\Request;
+
+class DummyClass extends Action
+{
+    /**
+     * Handle the action request.
+     *
+     * @param Request $request
+     *
+     * @return Response
+     */
+    public function handle(Request $request)
+    {
+        // dump($this->key());
+
+        return $this->response()->success('Processed successfully.')->redirect('/');
+    }
+
+    /**
+     * @return string
+     */
+    public function title()
+    {
+        return 'Title';
+    }
+
+    /**
+     * @return string|void
+     */
+    public function confirm()
+    {
+        // return 'Confirm?';
+    }
+
+    /**
+     * @param Model|Authenticatable|HasPermissions|null $user
+     *
+     * @return bool
+     */
+    protected function authorize($user): bool
+    {
+        return true;
+    }
+
+    /**
+     * @return array
+     */
+    protected function parameters()
+    {
+        return [];
+    }
+}

+ 71 - 0
src/Console/stubs/actions/form-tool.stub

@@ -0,0 +1,71 @@
+<?php
+
+namespace DummyNamespace;
+
+use Dcat\Admin\Form\AbstractTool;
+use Dcat\Admin\Actions\Response;
+use Dcat\Admin\Models\HasPermissions;
+use Illuminate\Contracts\Auth\Authenticatable;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Http\Request;
+
+class DummyClass extends AbstractTool
+{
+    /**
+     * Handle the action request.
+     *
+     * @param Request $request
+     *
+     * @return Response
+     */
+    public function handle(Request $request)
+    {
+        // dump($this->key());
+
+        return $this->response()
+            ->success('Processed successfully.')
+            ->redirect('/');
+    }
+
+    /**
+     * @return string
+     */
+    public function title()
+    {
+        return 'Title';
+    }
+
+    /**
+     * @return string|void
+     */
+    protected function href()
+    {
+        // return admin_url('auth/users');
+    }
+
+    /**
+     * @return string|void
+     */
+    public function confirm()
+    {
+        // return 'Confirm?';
+    }
+
+    /**
+     * @param Model|Authenticatable|HasPermissions|null $user
+     *
+     * @return bool
+     */
+    protected function authorize($user): bool
+    {
+        return true;
+    }
+
+    /**
+     * @return array
+     */
+    protected function parameters()
+    {
+        return [];
+    }
+}

+ 61 - 0
src/Console/stubs/actions/grid-batch.stub

@@ -0,0 +1,61 @@
+<?php
+
+namespace DummyNamespace;
+
+use Dcat\Admin\Grid\BatchAction;
+use Dcat\Admin\Actions\Response;
+use Dcat\Admin\Models\HasPermissions;
+use Illuminate\Contracts\Auth\Authenticatable;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Http\Request;
+
+class DummyClass extends BatchAction
+{
+    /**
+     * Handle the action request.
+     *
+     * @param Request $request
+     *
+     * @return Response
+     */
+    public function handle(Request $request)
+    {
+        return $this->response()
+            ->success('Processed successfully: '.json_encode($this->key()))
+            ->redirect('/');
+    }
+
+    /**
+     * @return string
+     */
+    public function title()
+    {
+        return 'Title';
+    }
+
+    /**
+     * @return string|void
+     */
+    public function confirm()
+    {
+        // return 'Confirm?';
+    }
+
+    /**
+     * @param Model|Authenticatable|HasPermissions|null $user
+     *
+     * @return bool
+     */
+    protected function authorize($user): bool
+    {
+        return true;
+    }
+
+    /**
+     * @return array
+     */
+    protected function parameters()
+    {
+        return [];
+    }
+}

+ 61 - 0
src/Console/stubs/actions/grid-row.stub

@@ -0,0 +1,61 @@
+<?php
+
+namespace DummyNamespace;
+
+use Dcat\Admin\Grid\RowAction;
+use Dcat\Admin\Actions\Response;
+use Dcat\Admin\Models\HasPermissions;
+use Illuminate\Contracts\Auth\Authenticatable;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Http\Request;
+
+class DummyClass extends RowAction
+{
+    /**
+     * Handle the action request.
+     *
+     * @param Request $request
+     *
+     * @return Response
+     */
+    public function handle(Request $request)
+    {
+        return $this->response()
+            ->success('Processed successfully: '.$this->key())
+            ->redirect('/');
+    }
+
+    /**
+     * @return string
+     */
+    public function title()
+    {
+        return 'Title';
+    }
+
+    /**
+     * @return string|void
+     */
+    public function confirm()
+    {
+        // return 'Confirm?';
+    }
+
+    /**
+     * @param Model|Authenticatable|HasPermissions|null $user
+     *
+     * @return bool
+     */
+    protected function authorize($user): bool
+    {
+        return true;
+    }
+
+    /**
+     * @return array
+     */
+    protected function parameters()
+    {
+        return [];
+    }
+}

+ 71 - 0
src/Console/stubs/actions/grid-tool.stub

@@ -0,0 +1,71 @@
+<?php
+
+namespace DummyNamespace;
+
+use Dcat\Admin\Grid\Tools\AbstractTool;
+use Dcat\Admin\Actions\Response;
+use Dcat\Admin\Models\HasPermissions;
+use Illuminate\Contracts\Auth\Authenticatable;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Http\Request;
+
+class DummyClass extends AbstractTool
+{
+    /**
+     * Handle the action request.
+     *
+     * @param Request $request
+     *
+     * @return Response
+     */
+    public function handle(Request $request)
+    {
+        // dump($this->key());
+
+        return $this->response()
+            ->success('Processed successfully.')
+            ->redirect('/');
+    }
+
+    /**
+     * @return string
+     */
+    public function title()
+    {
+        return 'Title';
+    }
+
+    /**
+     * @return string|void
+     */
+    protected function href()
+    {
+        // return admin_url('auth/users');
+    }
+
+    /**
+     * @return string|void
+     */
+    public function confirm()
+    {
+        // return 'Confirm?';
+    }
+
+    /**
+     * @param Model|Authenticatable|HasPermissions|null $user
+     *
+     * @return bool
+     */
+    protected function authorize($user): bool
+    {
+        return true;
+    }
+
+    /**
+     * @return array
+     */
+    protected function parameters()
+    {
+        return [];
+    }
+}

+ 71 - 0
src/Console/stubs/actions/show-tool.stub

@@ -0,0 +1,71 @@
+<?php
+
+namespace DummyNamespace;
+
+use Dcat\Admin\Show\AbstractTool;
+use Dcat\Admin\Actions\Response;
+use Dcat\Admin\Models\HasPermissions;
+use Illuminate\Contracts\Auth\Authenticatable;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Http\Request;
+
+class DummyClass extends AbstractTool
+{
+    /**
+     * Handle the action request.
+     *
+     * @param Request $request
+     *
+     * @return Response
+     */
+    public function handle(Request $request)
+    {
+        // dump($this->key());
+
+        return $this->response()
+            ->success('Processed successfully.')
+            ->redirect('/');
+    }
+
+    /**
+     * @return string
+     */
+    public function title()
+    {
+        return 'Title';
+    }
+
+    /**
+     * @return string|void
+     */
+    protected function href()
+    {
+        // return admin_url('auth/users');
+    }
+
+    /**
+     * @return string|void
+     */
+    public function confirm()
+    {
+        // return 'Confirm?';
+    }
+
+    /**
+     * @param Model|Authenticatable|HasPermissions|null $user
+     *
+     * @return bool
+     */
+    protected function authorize($user): bool
+    {
+        return true;
+    }
+
+    /**
+     * @return array
+     */
+    protected function parameters()
+    {
+        return [];
+    }
+}

+ 63 - 0
src/Console/stubs/actions/tree-tool.stub

@@ -0,0 +1,63 @@
+<?php
+
+namespace DummyNamespace;
+
+use Dcat\Admin\Tree\AbstractTool;
+use Dcat\Admin\Actions\Response;
+use Dcat\Admin\Models\HasPermissions;
+use Illuminate\Contracts\Auth\Authenticatable;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Http\Request;
+
+class DummyClass extends AbstractTool
+{
+    /**
+     * Handle the action request.
+     *
+     * @param Request $request
+     *
+     * @return Response
+     */
+    public function handle(Request $request)
+    {
+        // dump($this->key());
+
+        return $this->response()
+            ->success('Processed successfully.')
+            ->redirect('/');
+    }
+
+    /**
+     * @return string
+     */
+    public function title()
+    {
+        return 'Title';
+    }
+
+    /**
+     * @return string|void
+     */
+    protected function href()
+    {
+        // return admin_url('auth/users');
+    }
+
+    /**
+     * @return string|void
+     */
+    public function confirm()
+    {
+        // return 'Confirm?';
+    }
+
+    /**
+     * @param Model|Authenticatable|HasPermissions|null $user
+     *
+     * @return bool
+     */
+    protected function authorize($user): bool
+    {
+        return true;
+    }
+}

+ 1 - 1
src/Console/stubs/form.stub

@@ -20,7 +20,7 @@ class DummyClass extends Form
 
         // return $this->error('Your error message.');
 
-        return $this->success('Processed successfully.', admin_url('/'));
+        return $this->success('Processed successfully.', '/');
     }
 
     /**

+ 2 - 2
src/Traits/HasFormResponse.php

@@ -22,7 +22,7 @@ trait HasFormResponse
             return response()->json([
                 'status'   => $status,
                 'message'  => $message,
-                'redirect' => $redirect,
+                'redirect' => admin_url($redirect),
             ]);
         }
 
@@ -133,7 +133,7 @@ trait HasFormResponse
             admin_alert($message);
         }
 
-        return redirect($url, $status);
+        return redirect(admin_url($url), $status);
     }
 
     /**