|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backpack\Generators\Console\Commands; |
| 4 | + |
| 5 | +use Backpack\CRUD\ViewNamespaces; |
| 6 | +use Illuminate\Console\GeneratorCommand; |
| 7 | +use Illuminate\Support\Str; |
| 8 | + |
| 9 | +class WidgetBackpackCommand extends GeneratorCommand |
| 10 | +{ |
| 11 | + use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput; |
| 12 | + /** |
| 13 | + * The console command name. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $name = 'backpack:widget'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The name and signature of the console command. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $signature = 'backpack:widget {name} {--from=}'; |
| 25 | + |
| 26 | + /** |
| 27 | + * The console command description. |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $description = 'Generate a Backpack widget'; |
| 32 | + |
| 33 | + /** |
| 34 | + * The type of class being generated. |
| 35 | + * |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + protected $type = 'Widget'; |
| 39 | + |
| 40 | + /** |
| 41 | + * Get the stub file for the generator. |
| 42 | + * |
| 43 | + * @return string |
| 44 | + */ |
| 45 | + protected function getStub() |
| 46 | + { |
| 47 | + return __DIR__.'/../stubs/widget.stub'; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Alias for the fire method. |
| 52 | + * |
| 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. |
| 55 | + */ |
| 56 | + public function handle() |
| 57 | + { |
| 58 | + $this->fire(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Execute the console command. |
| 63 | + * |
| 64 | + * @return bool|null |
| 65 | + */ |
| 66 | + public function fire() |
| 67 | + { |
| 68 | + $name = Str::of($this->getNameInput()); |
| 69 | + $path = $this->getPath($name); |
| 70 | + |
| 71 | + if ($this->alreadyExists($this->getNameInput())) { |
| 72 | + $this->error("Error : $this->type $name already existed!"); |
| 73 | + |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + $src = null; |
| 78 | + if ($this->option('from')) { |
| 79 | + $widget = Str::of($this->option('from')); |
| 80 | + $arr = ViewNamespaces::getFor('widgets'); |
| 81 | + foreach ($arr as $key => $value) { |
| 82 | + $viewPath = $value.'.'.$widget; |
| 83 | + if (view()->exists($viewPath)) { |
| 84 | + $src = view($viewPath)->getPath(); |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + if ($src == null) { |
| 89 | + $this->error("Error : $this->type $widget does not exist!"); |
| 90 | + |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + $this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}"); |
| 96 | + $this->progressBlock("Creating view <fg=blue>resources/views/vendor/backpack/base/widgets/{$name->snake('_')}.blade.php</>"); |
| 97 | + |
| 98 | + $this->makeDirectory($path); |
| 99 | + if ($src != null) { |
| 100 | + $this->files->copy($src, $path); |
| 101 | + } else { |
| 102 | + $this->files->put($path, $this->buildClass($name)); |
| 103 | + } |
| 104 | + |
| 105 | + $this->closeProgressBlock(); |
| 106 | + $this->newLine(); |
| 107 | + $this->info($this->type.' created successfully.'); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Determine if the class already exists. |
| 112 | + * |
| 113 | + * @param string $name |
| 114 | + * @return bool |
| 115 | + */ |
| 116 | + protected function alreadyExists($name) |
| 117 | + { |
| 118 | + return $this->files->exists($this->getPath($name)); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Get the destination class path. |
| 123 | + * |
| 124 | + * @param string $name |
| 125 | + * @return string |
| 126 | + */ |
| 127 | + protected function getPath($name) |
| 128 | + { |
| 129 | + $file = Str::of($name)->snake('_'); |
| 130 | + |
| 131 | + return resource_path("views/vendor/backpack/base/widgets/$file.blade.php"); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Build the class with the given name. |
| 136 | + * |
| 137 | + * @param string $name |
| 138 | + * @return string |
| 139 | + */ |
| 140 | + protected function buildClass($name) |
| 141 | + { |
| 142 | + $stub = $this->files->get($this->getStub()); |
| 143 | + $stub = str_replace('dummy_widget', $name->snake('_'), $stub); |
| 144 | + $stub = str_replace('dummyWidget', $name->camel(), $stub); |
| 145 | + $stub = str_replace('DummyWidget', $name->studly(), $stub); |
| 146 | + |
| 147 | + return $stub; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Get the console command options. |
| 152 | + * |
| 153 | + * @return array |
| 154 | + */ |
| 155 | + protected function getOptions() |
| 156 | + { |
| 157 | + return []; |
| 158 | + } |
| 159 | +} |
0 commit comments