|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backpack\Generators\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\GeneratorCommand; |
| 6 | +use Illuminate\Support\Str; |
| 7 | + |
| 8 | +class ButtonBackpackCommand extends GeneratorCommand |
| 9 | +{ |
| 10 | + use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput; |
| 11 | + /** |
| 12 | + * The console command name. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $name = 'backpack:button'; |
| 17 | + |
| 18 | + /** |
| 19 | + * The name and signature of the console command. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $signature = 'backpack:button {name}'; |
| 24 | + |
| 25 | + /** |
| 26 | + * The console command description. |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $description = 'Generate a Backpack button'; |
| 31 | + |
| 32 | + /** |
| 33 | + * The type of class being generated. |
| 34 | + * |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + protected $type = 'Button'; |
| 38 | + |
| 39 | + /** |
| 40 | + * Get the stub file for the generator. |
| 41 | + * |
| 42 | + * @return string |
| 43 | + */ |
| 44 | + protected function getStub() |
| 45 | + { |
| 46 | + return __DIR__.'/../stubs/button.stub'; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Alias for the fire method. |
| 51 | + * |
| 52 | + * In Laravel 5.5 the fire() method has been renamed to handle(). |
| 53 | + * This alias provides support for both Laravel 5.4 and 5.5. |
| 54 | + */ |
| 55 | + public function handle() |
| 56 | + { |
| 57 | + $this->fire(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Execute the console command. |
| 62 | + * |
| 63 | + * @return bool|null |
| 64 | + */ |
| 65 | + public function fire() |
| 66 | + { |
| 67 | + $name = Str::of($this->getNameInput()); |
| 68 | + $path = $this->getPath($name); |
| 69 | + |
| 70 | + if ($this->alreadyExists($this->getNameInput())) { |
| 71 | + $this->error($this->type.' already existed!'); |
| 72 | + |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + $this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}"); |
| 77 | + $this->progressBlock("Creating view <fg=blue>resources/views/vendor/backpack/crud/buttons/${name}.blade.php</>"); |
| 78 | + |
| 79 | + $this->makeDirectory($path); |
| 80 | + $this->files->put($path, $this->buildClass($name)); |
| 81 | + |
| 82 | + $this->closeProgressBlock(); |
| 83 | + $this->newLine(); |
| 84 | + $this->info($this->type.' created successfully.'); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Determine if the class already exists. |
| 89 | + * |
| 90 | + * @param string $name |
| 91 | + * @return bool |
| 92 | + */ |
| 93 | + protected function alreadyExists($name) |
| 94 | + { |
| 95 | + return $this->files->exists($this->getPath($name)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Get the destination class path. |
| 100 | + * |
| 101 | + * @param string $name |
| 102 | + * @return string |
| 103 | + */ |
| 104 | + protected function getPath($name) |
| 105 | + { |
| 106 | + return resource_path("views/vendor/backpack/crud/buttons/$name.blade.php"); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Build the class with the given name. |
| 111 | + * |
| 112 | + * @param string $name |
| 113 | + * @return string |
| 114 | + */ |
| 115 | + protected function buildClass($name) |
| 116 | + { |
| 117 | + $stub = $this->files->get($this->getStub()); |
| 118 | + $stub = str_replace('dummy', $name, $stub); |
| 119 | + |
| 120 | + return $stub; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Get the console command options. |
| 125 | + * |
| 126 | + * @return array |
| 127 | + */ |
| 128 | + protected function getOptions() |
| 129 | + { |
| 130 | + return [ |
| 131 | + |
| 132 | + ]; |
| 133 | + } |
| 134 | +} |
0 commit comments