Salah3id /
address-domains
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Salah3id\Domains\Commands; |
||||
| 4 | |||||
| 5 | use Illuminate\Support\Str; |
||||
| 6 | use Salah3id\Domains\Support\Config\GenerateConfigReader; |
||||
| 7 | use Salah3id\Domains\Support\Stub; |
||||
| 8 | use Salah3id\Domains\Traits\DomainCommandTrait; |
||||
| 9 | use Symfony\Component\Console\Input\InputArgument; |
||||
| 10 | use Symfony\Component\Console\Input\InputOption; |
||||
| 11 | |||||
| 12 | class ModelMakeCommand extends GeneratorCommand |
||||
| 13 | { |
||||
| 14 | use DomainCommandTrait; |
||||
| 15 | |||||
| 16 | /** |
||||
| 17 | * The name of argument name. |
||||
| 18 | * |
||||
| 19 | * @var string |
||||
| 20 | */ |
||||
| 21 | protected $argumentName = 'model'; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * The console command name. |
||||
| 25 | * |
||||
| 26 | * @var string |
||||
| 27 | */ |
||||
| 28 | protected $name = 'domain:make-model'; |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * The console command description. |
||||
| 32 | * |
||||
| 33 | * @var string |
||||
| 34 | */ |
||||
| 35 | protected $description = 'Create a new model for the specified domain.'; |
||||
| 36 | |||||
| 37 | public function handle(): int |
||||
| 38 | { |
||||
| 39 | if (parent::handle() === E_ERROR) { |
||||
| 40 | return E_ERROR; |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | $this->handleOptionalMigrationOption(); |
||||
| 44 | $this->handleOptionalControllerOption(); |
||||
| 45 | $this->handleOptionalSeedOption(); |
||||
| 46 | $this->handleOptionalRequestOption(); |
||||
| 47 | |||||
| 48 | return 0; |
||||
| 49 | } |
||||
| 50 | |||||
| 51 | /** |
||||
| 52 | * Create a proper migration name: |
||||
| 53 | * ProductDetail: product_details |
||||
| 54 | * Product: products |
||||
| 55 | * @return string |
||||
| 56 | */ |
||||
| 57 | private function createMigrationName() |
||||
| 58 | { |
||||
| 59 | $pieces = preg_split('/(?=[A-Z])/', $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 60 | |||||
| 61 | $string = ''; |
||||
| 62 | foreach ($pieces as $i => $piece) { |
||||
| 63 | if ($i+1 < count($pieces)) { |
||||
| 64 | $string .= strtolower($piece) . '_'; |
||||
| 65 | } else { |
||||
| 66 | $string .= Str::plural(strtolower($piece)); |
||||
| 67 | } |
||||
| 68 | } |
||||
| 69 | |||||
| 70 | return $string; |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | /** |
||||
| 74 | * Get the console command arguments. |
||||
| 75 | * |
||||
| 76 | * @return array |
||||
| 77 | */ |
||||
| 78 | protected function getArguments() |
||||
| 79 | { |
||||
| 80 | return [ |
||||
| 81 | ['model', InputArgument::REQUIRED, 'The name of model will be created.'], |
||||
| 82 | ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'], |
||||
| 83 | ]; |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | /** |
||||
| 87 | * Get the console command options. |
||||
| 88 | * |
||||
| 89 | * @return array |
||||
| 90 | */ |
||||
| 91 | protected function getOptions() |
||||
| 92 | { |
||||
| 93 | return [ |
||||
| 94 | ['fillable', null, InputOption::VALUE_OPTIONAL, 'The fillable attributes.', null], |
||||
| 95 | ['migration', 'm', InputOption::VALUE_NONE, 'Flag to create associated migrations', null], |
||||
| 96 | ['controller', 'c', InputOption::VALUE_NONE, 'Flag to create associated controllers', null], |
||||
| 97 | ['seed', 's', InputOption::VALUE_NONE, 'Create a new seeder for the model', null], |
||||
| 98 | ['request', 'r', InputOption::VALUE_NONE, 'Create a new request for the model', null] |
||||
| 99 | ]; |
||||
| 100 | } |
||||
| 101 | |||||
| 102 | /** |
||||
| 103 | * Create the migration file with the given model if migration flag was used |
||||
| 104 | */ |
||||
| 105 | private function handleOptionalMigrationOption() |
||||
| 106 | { |
||||
| 107 | if ($this->option('migration') === true) { |
||||
| 108 | $migrationName = 'create_' . $this->createMigrationName() . '_table'; |
||||
| 109 | $this->call('domain:make-migration', ['name' => $migrationName, 'domain' => $this->argument('domain')]); |
||||
| 110 | } |
||||
| 111 | } |
||||
| 112 | |||||
| 113 | /** |
||||
| 114 | * Create the controller file for the given model if controller flag was used |
||||
| 115 | */ |
||||
| 116 | private function handleOptionalControllerOption() |
||||
| 117 | { |
||||
| 118 | if ($this->option('controller') === true) { |
||||
| 119 | $controllerName = "{$this->getModelName()}Controller"; |
||||
| 120 | |||||
| 121 | $this->call('domain:make-controller', array_filter([ |
||||
| 122 | 'controller' => $controllerName, |
||||
| 123 | 'domain' => $this->argument('domain'), |
||||
| 124 | ])); |
||||
| 125 | } |
||||
| 126 | } |
||||
| 127 | |||||
| 128 | /** |
||||
| 129 | * Create a seeder file for the model. |
||||
| 130 | * |
||||
| 131 | * @return void |
||||
| 132 | */ |
||||
| 133 | protected function handleOptionalSeedOption() |
||||
| 134 | { |
||||
| 135 | if ($this->option('seed') === true) { |
||||
| 136 | $seedName = "{$this->getModelName()}Seeder"; |
||||
| 137 | |||||
| 138 | $this->call('domain:make-seed', array_filter([ |
||||
| 139 | 'name' => $seedName, |
||||
| 140 | 'domain' => $this->argument('domain') |
||||
| 141 | ])); |
||||
| 142 | } |
||||
| 143 | } |
||||
| 144 | |||||
| 145 | /** |
||||
| 146 | * Create a request file for the model. |
||||
| 147 | * |
||||
| 148 | * @return void |
||||
| 149 | */ |
||||
| 150 | protected function handleOptionalRequestOption() |
||||
| 151 | { |
||||
| 152 | if ($this->option('request') === true) { |
||||
| 153 | $requestName = "{$this->getModelName()}Request"; |
||||
| 154 | |||||
| 155 | $this->call('domain:make-request', array_filter([ |
||||
| 156 | 'name' => $requestName, |
||||
| 157 | 'domain' => $this->argument('domain') |
||||
| 158 | ])); |
||||
| 159 | } |
||||
| 160 | } |
||||
| 161 | |||||
| 162 | /** |
||||
| 163 | * @return mixed |
||||
| 164 | */ |
||||
| 165 | protected function getTemplateContents() |
||||
| 166 | { |
||||
| 167 | $domain = $this->laravel['domains']->findOrFail($this->getDomainName()); |
||||
| 168 | |||||
| 169 | return (new Stub('/model.stub', [ |
||||
| 170 | 'NAME' => $this->getModelName(), |
||||
| 171 | 'FILLABLE' => $this->getFillable(), |
||||
| 172 | 'NAMESPACE' => $this->getClassNamespace($domain), |
||||
| 173 | 'CLASS' => $this->getClass(), |
||||
| 174 | 'LOWER_NAME' => $domain->getLowerName(), |
||||
| 175 | 'DOMAIN' => $this->getDomainName(), |
||||
| 176 | 'STUDLY_NAME' => $domain->getStudlyName(), |
||||
| 177 | 'DOMAIN_NAMESPACE' => $this->laravel['domains']->config('namespace'), |
||||
| 178 | ]))->render(); |
||||
| 179 | } |
||||
| 180 | |||||
| 181 | /** |
||||
| 182 | * @return mixed |
||||
| 183 | */ |
||||
| 184 | protected function getDestinationFilePath() |
||||
| 185 | { |
||||
| 186 | $path = $this->laravel['domains']->getDomainPath($this->getDomainName()); |
||||
| 187 | |||||
| 188 | $modelPath = GenerateConfigReader::read('model'); |
||||
| 189 | |||||
| 190 | return $path . $modelPath->getPath() . '/' . $this->getModelName() . '.php'; |
||||
| 191 | } |
||||
| 192 | |||||
| 193 | /** |
||||
| 194 | * @return mixed|string |
||||
| 195 | */ |
||||
| 196 | private function getModelName() |
||||
| 197 | { |
||||
| 198 | return Str::studly($this->argument('model')); |
||||
|
0 ignored issues
–
show
It seems like
$this->argument('model') can also be of type array; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 199 | } |
||||
| 200 | |||||
| 201 | /** |
||||
| 202 | * @return string |
||||
| 203 | */ |
||||
| 204 | private function getFillable() |
||||
| 205 | { |
||||
| 206 | $fillable = $this->option('fillable'); |
||||
| 207 | |||||
| 208 | if (!is_null($fillable)) { |
||||
| 209 | $arrays = explode(',', $fillable); |
||||
| 210 | |||||
| 211 | return json_encode($arrays); |
||||
| 212 | } |
||||
| 213 | |||||
| 214 | return '[]'; |
||||
| 215 | } |
||||
| 216 | |||||
| 217 | /** |
||||
| 218 | * Get default namespace. |
||||
| 219 | * |
||||
| 220 | * @return string |
||||
| 221 | */ |
||||
| 222 | public function getDefaultNamespace(): string |
||||
| 223 | { |
||||
| 224 | $domain = $this->laravel['domains']; |
||||
| 225 | |||||
| 226 | return $domain->config('paths.generator.model.namespace') ?: $domain->config('paths.generator.model.path', 'Entities'); |
||||
| 227 | } |
||||
| 228 | } |
||||
| 229 |