|
9 | 9 | class ColumnBackpackCommand extends GeneratorCommand |
10 | 10 | { |
11 | 11 | use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput; |
| 12 | + |
12 | 13 | /** |
13 | 14 | * The console command name. |
14 | 15 | * |
@@ -38,73 +39,77 @@ class ColumnBackpackCommand extends GeneratorCommand |
38 | 39 | protected $type = 'Column'; |
39 | 40 |
|
40 | 41 | /** |
41 | | - * Get the stub file for the generator. |
| 42 | + * View Namespace. |
42 | 43 | * |
43 | | - * @return string |
| 44 | + * @var string |
44 | 45 | */ |
45 | | - protected function getStub() |
46 | | - { |
47 | | - return __DIR__.'/../stubs/column.stub'; |
48 | | - } |
| 46 | + protected $viewNamespace = 'columns'; |
49 | 47 |
|
50 | 48 | /** |
51 | | - * Alias for the fire method. |
| 49 | + * Get the stub file for the generator. |
52 | 50 | * |
53 | | - * In Laravel 5.5 the fire() method has been renamed to handle(). |
54 | | - * This alias provides support for both Laravel 5.4 and 5.5. |
| 51 | + * @return string |
55 | 52 | */ |
56 | | - public function handle() |
| 53 | + protected function getStub() |
57 | 54 | { |
58 | | - $this->fire(); |
| 55 | + return __DIR__.'/../stubs/column.stub'; |
59 | 56 | } |
60 | 57 |
|
61 | 58 | /** |
62 | 59 | * Execute the console command. |
63 | 60 | * |
64 | 61 | * @return bool|null |
65 | 62 | */ |
66 | | - public function fire() |
| 63 | + public function handle() |
67 | 64 | { |
68 | 65 | $name = Str::of($this->getNameInput()); |
69 | | - $path = $this->getPath($name); |
| 66 | + $path = Str::of($this->getPath($name)); |
| 67 | + $pathRelative = $path->after(base_path())->replace('\\', '/')->trim('/'); |
| 68 | + |
| 69 | + $this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}"); |
| 70 | + $this->progressBlock("Creating view <fg=blue>{$pathRelative}</>"); |
70 | 71 |
|
71 | | - if ($this->alreadyExists($this->getNameInput())) { |
72 | | - $this->error("Error : $this->type $name already existed!"); |
| 72 | + if ($this->alreadyExists($name)) { |
| 73 | + $this->closeProgressBlock('Already existed', 'yellow'); |
73 | 74 |
|
74 | 75 | return false; |
75 | 76 | } |
76 | 77 |
|
77 | | - $src = null; |
| 78 | + $source = null; |
78 | 79 | if ($this->option('from')) { |
79 | | - $column = Str::of($this->option('from')); |
80 | | - $arr = ViewNamespaces::getFor('columns'); |
81 | | - foreach ($arr as $key => $value) { |
82 | | - $viewPath = $value.'.'.$column; |
| 80 | + $from = $this->option('from'); |
| 81 | + $namespaces = ViewNamespaces::getFor($this->viewNamespace); |
| 82 | + foreach ($namespaces as $namespace) { |
| 83 | + $viewPath = "$namespace.$from"; |
83 | 84 | if (view()->exists($viewPath)) { |
84 | | - $src = view($viewPath)->getPath(); |
| 85 | + $source = view($viewPath)->getPath(); |
85 | 86 | break; |
86 | 87 | } |
87 | 88 | } |
88 | | - if ($src == null) { |
89 | | - $this->error("Error : $this->type $column does not exist!"); |
| 89 | + |
| 90 | + // full file path may be provided |
| 91 | + if (file_exists($from)) { |
| 92 | + $source = $from; |
| 93 | + } |
| 94 | + |
| 95 | + if (! $source) { |
| 96 | + $this->errorProgressBlock(); |
| 97 | + $this->note("$this->type '$from' does not exist!", 'red'); |
| 98 | + $this->newLine(); |
90 | 99 |
|
91 | 100 | return false; |
92 | 101 | } |
93 | 102 | } |
94 | 103 |
|
95 | | - $this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}"); |
96 | | - $this->progressBlock("Creating view <fg=blue>resources/views/vendor/backpack/crud/columns/{$name->snake('_')}.blade.php</>"); |
97 | | - |
98 | 104 | $this->makeDirectory($path); |
99 | | - if ($src != null) { |
100 | | - $this->files->copy($src, $path); |
| 105 | + |
| 106 | + if ($source) { |
| 107 | + $this->files->copy($source, $path); |
101 | 108 | } else { |
102 | 109 | $this->files->put($path, $this->buildClass($name)); |
103 | 110 | } |
104 | 111 |
|
105 | 112 | $this->closeProgressBlock(); |
106 | | - $this->newLine(); |
107 | | - $this->info($this->type.' created successfully.'); |
108 | 113 | } |
109 | 114 |
|
110 | 115 | /** |
@@ -145,12 +150,15 @@ protected function buildClass($name) |
145 | 150 | } |
146 | 151 |
|
147 | 152 | /** |
148 | | - * Get the console command options. |
| 153 | + * Get the desired class name from the input. |
149 | 154 | * |
150 | | - * @return array |
| 155 | + * @return string |
151 | 156 | */ |
152 | | - protected function getOptions() |
| 157 | + protected function getNameInput() |
153 | 158 | { |
154 | | - return []; |
| 159 | + return Str::of($this->argument('name')) |
| 160 | + ->trim() |
| 161 | + ->snake('_') |
| 162 | + ->value; |
155 | 163 | } |
156 | 164 | } |
0 commit comments