Browse Source

增加action以及form生成命令对非app目录的支持

jqh 5 years ago
parent
commit
02ea24d8d5
3 changed files with 72 additions and 3 deletions
  1. 5 1
      src/Console/ActionCommand.php
  2. 17 2
      src/Console/FormCommand.php
  3. 50 0
      src/Console/GeneratorCommand.php

+ 5 - 1
src/Console/ActionCommand.php

@@ -2,7 +2,7 @@
 
 namespace Dcat\Admin\Console;
 
-use Illuminate\Console\GeneratorCommand;
+use Illuminate\Support\Str;
 
 class ActionCommand extends GeneratorCommand
 {
@@ -64,6 +64,10 @@ class ActionCommand extends GeneratorCommand
 
         $this->namespace = ucfirst(trim($this->ask('Please enter the namespace of action class', $this->getDefaultNamespace(null))));
 
+        if (! Str::startsWith(config('admin.route.namespace'), 'App')) {
+            $this->baseDirectory = trim($this->ask('Please enter the destination class path', 'app'));
+        }
+
         return parent::handle();
     }
 

+ 17 - 2
src/Console/FormCommand.php

@@ -2,7 +2,7 @@
 
 namespace Dcat\Admin\Console;
 
-use Illuminate\Console\GeneratorCommand;
+use Illuminate\Support\Str;
 
 class FormCommand extends GeneratorCommand
 {
@@ -12,7 +12,8 @@ class FormCommand extends GeneratorCommand
      * @var string
      */
     protected $signature = 'admin:form {name} 
-        {--namespace=}';
+        {--namespace=}
+        {--base=}';
 
     /**
      * The console command description.
@@ -21,6 +22,20 @@ class FormCommand extends GeneratorCommand
      */
     protected $description = 'Make admin form widget';
 
+    /**
+     * @return bool|null
+     *
+     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
+     */
+    public function handle()
+    {
+        if (! Str::startsWith(config('admin.route.namespace'), 'App')) {
+            $this->baseDirectory = trim($this->ask('Please enter the destination class path', 'app'));
+        }
+
+        return parent::handle(); // TODO: Change the autogenerated stub
+    }
+
     /**
      * Get the stub file for the generator.
      *

+ 50 - 0
src/Console/GeneratorCommand.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace Dcat\Admin\Console;
+
+use Illuminate\Console\GeneratorCommand as BaseCommand;
+use Illuminate\Support\Str;
+
+abstract class GeneratorCommand extends BaseCommand
+{
+    protected $baseDirectory;
+
+    /**
+     * Get the root namespace for the class.
+     *
+     * @return string
+     */
+    protected function rootNamespace()
+    {
+        return $this->getDefaultNamespace(null);
+    }
+
+    /**
+     * Get the destination class path.
+     *
+     * @param  string  $name
+     * @return string
+     */
+    protected function getPath($name)
+    {
+        $name = Str::replaceFirst(explode('\\', $this->rootNamespace())[0], '', $name);
+
+        return $this->getBaseDir().'/'.str_replace('\\', '/', $name).'.php';
+    }
+
+    /**
+     * @return string
+     */
+    protected function getBaseDir()
+    {
+        if ($this->baseDirectory) {
+            return trim(base_path($this->baseDirectory), '/');
+        }
+
+        if ($this->hasOption('base') && $this->option('base')) {
+            return trim(base_path($this->option('base')), '/');
+        }
+
+        return $this->laravel['path'];
+    }
+}