|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\Commands\Generators; |
15 | 15 |
|
16 | | -use CodeIgniter\CLI\BaseCommand; |
| 16 | +use CodeIgniter\CLI\AbstractGeneratorCommand; |
| 17 | +use CodeIgniter\CLI\Attributes\Command; |
| 18 | +use CodeIgniter\CLI\Attributes\GeneratorCommand; |
17 | 19 | use CodeIgniter\CLI\CLI; |
18 | | -use CodeIgniter\CLI\GeneratorTrait; |
19 | | - |
20 | | -/** |
21 | | - * Generates a skeleton Model file. |
22 | | - */ |
23 | | -class ModelGenerator extends BaseCommand |
| 20 | +use CodeIgniter\CLI\Input\Option; |
| 21 | + |
| 22 | +#[Command(name: 'make:model', description: 'Generates a new model file.', group: 'Generators')] |
| 23 | +#[GeneratorCommand( |
| 24 | + component: 'Model', |
| 25 | + template: 'model.tpl.php', |
| 26 | + directory: 'Models', |
| 27 | + classNameLang: 'CLI.generator.className.model', |
| 28 | +)] |
| 29 | +class ModelGenerator extends AbstractGeneratorCommand |
24 | 30 | { |
25 | | - use GeneratorTrait; |
26 | | - |
27 | | - /** |
28 | | - * The Command's Group |
29 | | - * |
30 | | - * @var string |
31 | | - */ |
32 | | - protected $group = 'Generators'; |
33 | | - |
34 | | - /** |
35 | | - * The Command's Name |
36 | | - * |
37 | | - * @var string |
38 | | - */ |
39 | | - protected $name = 'make:model'; |
40 | | - |
41 | | - /** |
42 | | - * The Command's Description |
43 | | - * |
44 | | - * @var string |
45 | | - */ |
46 | | - protected $description = 'Generates a new model file.'; |
47 | | - |
48 | | - /** |
49 | | - * The Command's Usage |
50 | | - * |
51 | | - * @var string |
52 | | - */ |
53 | | - protected $usage = 'make:model <name> [options]'; |
| 31 | + private const RETURN_TYPES = ['array', 'object', 'entity']; |
54 | 32 |
|
55 | | - /** |
56 | | - * The Command's Arguments |
57 | | - * |
58 | | - * @var array<string, string> |
59 | | - */ |
60 | | - protected $arguments = [ |
61 | | - 'name' => 'The model class name.', |
62 | | - ]; |
63 | | - |
64 | | - /** |
65 | | - * The Command's Options |
66 | | - * |
67 | | - * @var array<string, string> |
68 | | - */ |
69 | | - protected $options = [ |
70 | | - '--table' => 'Supply a table name. Default: "the lowercased plural of the class name".', |
71 | | - '--dbgroup' => 'Database group to use. Default: "default".', |
72 | | - '--return' => 'Return type, Options: [array, object, entity]. Default: "array".', |
73 | | - '--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".', |
74 | | - '--suffix' => 'Append the component title to the class name (e.g. User => UserModel).', |
75 | | - '--force' => 'Force overwrite existing file.', |
76 | | - ]; |
| 33 | + protected function configure(): void |
| 34 | + { |
| 35 | + parent::configure(); |
| 36 | + |
| 37 | + $this |
| 38 | + ->addOption(new Option( |
| 39 | + name: 'table', |
| 40 | + shortcut: 't', |
| 41 | + description: 'Table name. Defaults to the lowercased plural of the class name.', |
| 42 | + acceptsValue: true, |
| 43 | + valueLabel: 'name', |
| 44 | + )) |
| 45 | + ->addOption(new Option( |
| 46 | + name: 'dbgroup', |
| 47 | + shortcut: 'g', |
| 48 | + description: 'Database group to use.', |
| 49 | + acceptsValue: true, |
| 50 | + valueLabel: 'group', |
| 51 | + )) |
| 52 | + ->addOption(new Option( |
| 53 | + name: 'return', |
| 54 | + shortcut: 'r', |
| 55 | + description: 'Return type: "array", "object", or "entity".', |
| 56 | + requiresValue: true, |
| 57 | + valueLabel: 'type', |
| 58 | + default: 'array', |
| 59 | + )); |
| 60 | + } |
77 | 61 |
|
78 | | - /** |
79 | | - * Actually execute a command. |
80 | | - */ |
81 | | - public function run(array $params) |
| 62 | + protected function interact(array &$arguments, array &$options): void |
82 | 63 | { |
83 | | - $this->component = 'Model'; |
84 | | - $this->directory = 'Models'; |
85 | | - $this->template = 'model.tpl.php'; |
| 64 | + $return = $this->getUnboundOption('return', $options); |
86 | 65 |
|
87 | | - $this->classNameLang = 'CLI.generator.className.model'; |
88 | | - $this->generateClass($params); |
| 66 | + if (! is_string($return) || in_array($return, self::RETURN_TYPES, true)) { |
| 67 | + return; |
| 68 | + } |
89 | 69 |
|
90 | | - return EXIT_SUCCESS; |
| 70 | + $options['return'] = CLI::prompt(lang('CLI.generator.returnType'), self::RETURN_TYPES, 'required'); |
91 | 71 | } |
92 | 72 |
|
93 | | - /** |
94 | | - * Prepare options and do the necessary replacements. |
95 | | - */ |
96 | | - protected function prepare(string $class): string |
| 73 | + protected function execute(array $arguments, array $options): int |
97 | 74 | { |
98 | | - $table = $this->getOption('table'); |
99 | | - $dbGroup = $this->getOption('dbgroup'); |
100 | | - $return = $this->getOption('return'); |
| 75 | + $return = $this->getValidatedOption('return'); |
101 | 76 |
|
102 | | - $baseClass = class_basename($class); |
| 77 | + if (! in_array($return, self::RETURN_TYPES, true)) { |
| 78 | + CLI::error(lang('CLI.generator.invalidReturnType', [$return])); |
103 | 79 |
|
104 | | - if (preg_match('/^(\S+)Model$/i', $baseClass, $match) === 1) { |
105 | | - $baseClass = $match[1]; |
| 80 | + return EXIT_ERROR; |
106 | 81 | } |
107 | 82 |
|
108 | | - $table = is_string($table) ? $table : plural(strtolower($baseClass)); |
109 | | - $return = is_string($return) ? $return : 'array'; |
| 83 | + $exitCode = $this->generateClass(); |
110 | 84 |
|
111 | | - if (! in_array($return, ['array', 'object', 'entity'], true)) { |
112 | | - // @codeCoverageIgnoreStart |
113 | | - $return = CLI::prompt(lang('CLI.generator.returnType'), ['array', 'object', 'entity'], 'required'); |
114 | | - CLI::newLine(); |
115 | | - // @codeCoverageIgnoreEnd |
| 85 | + if ($exitCode !== EXIT_SUCCESS || $return !== 'entity') { |
| 86 | + return $exitCode; |
116 | 87 | } |
117 | 88 |
|
118 | | - if ($return === 'entity') { |
119 | | - // Build the fully-qualified entity class from the model class so |
120 | | - // that the generated Entity keeps any sub-namespaces (eg. Admin). |
121 | | - $entityClass = str_replace('Models', 'Entities', $class); |
| 89 | + $entityOptions = ['namespace' => $this->getValidatedOption('namespace')]; |
122 | 90 |
|
123 | | - if (preg_match('/^(\S+)Model$/i', $entityClass, $match) === 1) { |
124 | | - $entityClass = $match[1]; |
| 91 | + if ($this->getValidatedOption('force') === true) { |
| 92 | + $entityOptions['force'] = null; |
| 93 | + } |
| 94 | + |
| 95 | + return $this->call('make:entity', [$this->getEntityClass($this->qualifyClassName())], $entityOptions); |
| 96 | + } |
125 | 97 |
|
126 | | - if ($this->getOption('suffix')) { |
127 | | - $entityClass .= 'Entity'; |
128 | | - } |
129 | | - } |
| 98 | + protected function getReplacements(string $class): array |
| 99 | + { |
| 100 | + $table = $this->getValidatedOption('table'); |
| 101 | + $dbGroup = $this->getValidatedOption('dbgroup'); |
| 102 | + |
| 103 | + $return = $this->getValidatedOption('return') === 'entity' |
| 104 | + ? '\\' . $this->getEntityClass($class) . '::class' |
| 105 | + : sprintf("'%s'", $this->getValidatedOption('return')); |
| 106 | + |
| 107 | + return [ |
| 108 | + '{dbGroup}' => is_string($dbGroup) ? $dbGroup : '', |
| 109 | + '{table}' => is_string($table) ? $table : plural(strtolower($this->stripModelSuffix(class_basename($class)))), |
| 110 | + '{return}' => $return, |
| 111 | + ]; |
| 112 | + } |
130 | 113 |
|
131 | | - // Call the entity generator with the fully-qualified class name so |
132 | | - // it ends up under the correct sub-namespace/folder (eg. Admin). |
133 | | - $entityOptions = array_intersect_key($this->params, array_flip(['namespace', 'suffix', 'force'])); |
| 114 | + protected function getTemplateData(string $class): array |
| 115 | + { |
| 116 | + return ['dbGroup' => $this->getValidatedOption('dbgroup')]; |
| 117 | + } |
134 | 118 |
|
135 | | - $this->call('make:entity', array_merge([trim($entityClass, '\\')], $entityOptions)); |
| 119 | + /** |
| 120 | + * Derives the entity class from the qualified model class, keeping any sub-namespace. |
| 121 | + */ |
| 122 | + private function getEntityClass(string $class): string |
| 123 | + { |
| 124 | + $entity = $this->stripModelSuffix(str_replace('\\Models\\', '\\Entities\\', $class)); |
136 | 125 |
|
137 | | - $return = '\\' . trim($entityClass, '\\') . '::class'; |
138 | | - } else { |
139 | | - $return = "'{$return}'"; |
140 | | - } |
| 126 | + return $this->shouldAppendSuffix() ? $entity . 'Entity' : $entity; |
| 127 | + } |
141 | 128 |
|
142 | | - return $this->parseTemplate($class, ['{dbGroup}', '{table}', '{return}'], [$dbGroup, $table, $return], compact('dbGroup')); |
| 129 | + private function stripModelSuffix(string $class): string |
| 130 | + { |
| 131 | + return preg_replace('/^(.+)Model$/i', '$1', $class) ?? $class; |
143 | 132 | } |
144 | 133 | } |
0 commit comments