|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Articulate\Concise\Commands; |
| 5 | + |
| 6 | +use Illuminate\Console\GeneratorCommand; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 9 | + |
| 10 | +#[AsCommand('make:entity')] |
| 11 | +class MakeEntityCommand extends GeneratorCommand |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The console command signature. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $signature = 'make:entity {name : The name of the entity} |
| 19 | + {--table= : The entity table} |
| 20 | + {--identity= : The entity identity field} |
| 21 | + {--connection= : The connection the entity uses}'; |
| 22 | + |
| 23 | + /** |
| 24 | + * The console command description. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $description = 'Create a new entity'; |
| 29 | + |
| 30 | + protected $type = 'Entity'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Execute the console command. |
| 34 | + * |
| 35 | + * @return bool|null |
| 36 | + * |
| 37 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 38 | + */ |
| 39 | + public function handle(): bool|null |
| 40 | + { |
| 41 | + if (parent::handle() === false) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + $this->call('make:mapper', [ |
| 46 | + 'name' => $this->argument('name') . 'Mapper', |
| 47 | + 'class' => $this->argument('name'), |
| 48 | + '--identity' => $this->option('identity'), |
| 49 | + '--table' => $this->option('table'), |
| 50 | + '--connection' => $this->option('connection'), |
| 51 | + ]); |
| 52 | + |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get the stub file for the generator. |
| 58 | + * |
| 59 | + * @return string |
| 60 | + */ |
| 61 | + protected function getStub(): string |
| 62 | + { |
| 63 | + return $this->resolveStubPath('/../../resources/stubs/entity.stub'); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Resolve the fully qualified path to the stub. |
| 68 | + * |
| 69 | + * @param string $stub |
| 70 | + * |
| 71 | + * @return string |
| 72 | + */ |
| 73 | + protected function resolveStubPath(string $stub): string |
| 74 | + { |
| 75 | + return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) |
| 76 | + ? $customPath |
| 77 | + : __DIR__ . $stub; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get the default namespace for the class. |
| 82 | + * |
| 83 | + * @param string $rootNamespace |
| 84 | + * |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + protected function getDefaultNamespace($rootNamespace): string |
| 88 | + { |
| 89 | + return $rootNamespace . '\\Entities'; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Build the class with the given name. |
| 94 | + * |
| 95 | + * @param string $name |
| 96 | + * |
| 97 | + * @return string |
| 98 | + * |
| 99 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 100 | + */ |
| 101 | + protected function buildClass($name): string |
| 102 | + { |
| 103 | + /** @var string $identity */ |
| 104 | + $identity = $this->option('identity') ?? 'id'; |
| 105 | + |
| 106 | + return str_replace( |
| 107 | + ['{{ identityCamel }}', '{{ identity }}'], |
| 108 | + [$identity, Str::camel($identity)], |
| 109 | + parent::buildClass($name) |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments