|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Baum\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\GeneratorCommand; |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use Symfony\Component\Console\Input\InputOption; |
| 8 | + |
| 9 | +class ModelMakeCommand extends GeneratorCommand |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The console command name. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $name = 'make:baum'; |
| 17 | + |
| 18 | + /** |
| 19 | + * The console command description. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $description = 'Create a new Baum model class'; |
| 24 | + |
| 25 | + /** |
| 26 | + * The type of class being generated. |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $type = 'Model'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Execute the console command. |
| 34 | + * |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + public function handle() |
| 38 | + { |
| 39 | + if (parent::handle() === false && ! $this->option('force')) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + if ($this->option('all')) { |
| 44 | + $this->input->setOption('factory', true); |
| 45 | + $this->input->setOption('seed', true); |
| 46 | + $this->input->setOption('migration', true); |
| 47 | + $this->input->setOption('controller', true); |
| 48 | + $this->input->setOption('resource', true); |
| 49 | + } |
| 50 | + |
| 51 | + if ($this->option('factory')) { |
| 52 | + $this->createFactory(); |
| 53 | + } |
| 54 | + |
| 55 | + if ($this->option('migration')) { |
| 56 | + $this->createMigration(); |
| 57 | + } |
| 58 | + |
| 59 | + if ($this->option('seed')) { |
| 60 | + $this->createSeeder(); |
| 61 | + } |
| 62 | + |
| 63 | + if ($this->option('controller') || $this->option('resource') || $this->option('api')) { |
| 64 | + $this->createController(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Create a model factory for the model. |
| 70 | + * |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + protected function createFactory() |
| 74 | + { |
| 75 | + $factory = Str::studly(class_basename($this->argument('name'))); |
| 76 | + |
| 77 | + $this->call('make:factory', [ |
| 78 | + 'name' => "{$factory}Factory", |
| 79 | + '--model' => $this->qualifyClass($this->getNameInput()), |
| 80 | + ]); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Create a migration file for the model. |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + protected function createMigration() |
| 89 | + { |
| 90 | + $table = Str::snake(Str::pluralStudly(class_basename($this->argument('name')))); |
| 91 | + |
| 92 | +// if ($this->option('pivot')) { |
| 93 | +// $table = Str::singular($table); |
| 94 | +// } |
| 95 | + |
| 96 | + $this->call('make:migration', [ |
| 97 | + 'name' => "create_{$table}_table", |
| 98 | + '--create' => $table, |
| 99 | + ]); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Create a seeder file for the model. |
| 104 | + * |
| 105 | + * @return void |
| 106 | + */ |
| 107 | + protected function createSeeder() |
| 108 | + { |
| 109 | + $seeder = Str::studly(class_basename($this->argument('name'))); |
| 110 | + |
| 111 | + $this->call('make:seed', [ |
| 112 | + 'name' => "{$seeder}Seeder", |
| 113 | + ]); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Create a controller for the model. |
| 118 | + * |
| 119 | + * @return void |
| 120 | + */ |
| 121 | + protected function createController() |
| 122 | + { |
| 123 | + $controller = Str::studly(class_basename($this->argument('name'))); |
| 124 | + |
| 125 | + $modelName = $this->qualifyClass($this->getNameInput()); |
| 126 | + |
| 127 | + $this->call('make:controller', array_filter([ |
| 128 | + 'name' => "{$controller}Controller", |
| 129 | + '--model' => $this->option('resource') || $this->option('api') ? $modelName : null, |
| 130 | + '--api' => $this->option('api'), |
| 131 | + ])); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Get the stub file for the generator. |
| 136 | + * |
| 137 | + * @return string |
| 138 | + */ |
| 139 | + protected function getStub() |
| 140 | + { |
| 141 | +// return $this->option('pivot') |
| 142 | +// ? $this->resolveStubPath('/stubs/model.pivot.stub') |
| 143 | +// : $this->resolveStubPath('/stubs/baum.stub'); |
| 144 | + return $this->resolveStubPath('/stubs/baum.stub'); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Resolve the fully-qualified path to the stub. |
| 149 | + * |
| 150 | + * @param string $stub |
| 151 | + * @return string |
| 152 | + */ |
| 153 | + protected function resolveStubPath($stub) |
| 154 | + { |
| 155 | + return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) |
| 156 | + ? $customPath |
| 157 | + : __DIR__.$stub; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Get the console command options. |
| 162 | + * |
| 163 | + * @return array |
| 164 | + */ |
| 165 | + protected function getOptions() |
| 166 | + { |
| 167 | + return [ |
| 168 | + ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, and resource controller for the model'], |
| 169 | + ['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model'], |
| 170 | + ['factory', 'f', InputOption::VALUE_NONE, 'Create a new factory for the model'], |
| 171 | + ['force', null, InputOption::VALUE_NONE, 'Create the class even if the model already exists'], |
| 172 | + ['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model'], |
| 173 | + ['seed', 's', InputOption::VALUE_NONE, 'Create a new seeder file for the model'], |
| 174 | +// ['pivot', 'p', InputOption::VALUE_NONE, 'Indicates if the generated model should be a custom intermediate table model'], |
| 175 | + ['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'], |
| 176 | + ['api', null, InputOption::VALUE_NONE, 'Indicates if the generated controller should be an API controller'], |
| 177 | + ]; |
| 178 | + } |
| 179 | +} |
0 commit comments