Parcourir la source

add FormCommand

jqh il y a 5 ans
Parent
commit
124c295078
3 fichiers modifiés avec 111 ajouts et 0 suppressions
  1. 1 0
      src/AdminServiceProvider.php
  2. 63 0
      src/Console/FormCommand.php
  3. 47 0
      src/Console/stubs/form.stub

+ 1 - 0
src/AdminServiceProvider.php

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

+ 63 - 0
src/Console/FormCommand.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace Dcat\Admin\Console;
+
+use Illuminate\Console\GeneratorCommand;
+
+class FormCommand extends GeneratorCommand
+{
+    /**
+     * The console command name.
+     *
+     * @var string
+     */
+    protected $signature = 'admin:form {name} 
+        {--namespace=}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Make admin form widget';
+
+    /**
+     * Get the stub file for the generator.
+     *
+     * @return string
+     */
+    protected function getStub()
+    {
+        return __DIR__.'/stubs/form.stub';
+    }
+
+    /**
+     * Get the default namespace for the class.
+     *
+     * @param string $rootNamespace
+     *
+     * @return string
+     */
+    protected function getDefaultNamespace($rootNamespace)
+    {
+        if ($namespace = $this->option('namespace')) {
+            return $namespace;
+        }
+
+        return str_replace('Controllers', 'Forms', config('admin.route.namespace'));
+    }
+
+    /**
+     * Get the desired class name from the input.
+     *
+     * @return string
+     */
+    protected function getNameInput()
+    {
+        $name = trim($this->argument('name'));
+
+        $this->type = $this->qualifyClass($name);
+
+        return $name;
+    }
+}

+ 47 - 0
src/Console/stubs/form.stub

@@ -0,0 +1,47 @@
+<?php
+
+namespace DummyNamespace;
+
+use Dcat\Admin\Widgets\Form;
+use Symfony\Component\HttpFoundation\Response;
+
+class DummyClass extends Form
+{
+    /**
+     * Handle the form request.
+     *
+     * @param array $input
+     *
+     * @return Response
+     */
+    public function handle(array $input)
+    {
+        // dump($input);
+
+        // return $this->error('Your error message.');
+
+        return $this->success('Processed successfully.', admin_url('/'));
+    }
+
+    /**
+     * Build a form here.
+     */
+    public function form()
+    {
+        $this->text('name')->required();
+        $this->email('email')->rules('email');
+    }
+
+    /**
+     * The data of the form.
+     *
+     * @return array
+     */
+    public function default()
+    {
+        return [
+            'name'  => 'John Doe',
+            'email' => 'John.Doe@gmail.com',
+        ];
+    }
+}