|
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 command file. |
22 | | - */ |
23 | | -class CommandGenerator extends BaseCommand |
| 20 | +use CodeIgniter\CLI\Input\Option; |
| 21 | + |
| 22 | +#[Command(name: 'make:command', description: 'Generates a new spark command.', group: 'Generators')] |
| 23 | +#[GeneratorCommand( |
| 24 | + component: 'Command', |
| 25 | + template: 'command.tpl.php', |
| 26 | + directory: 'Commands', |
| 27 | + classNameLang: 'CLI.generator.className.command', |
| 28 | +)] |
| 29 | +class CommandGenerator 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:command'; |
40 | | - |
41 | | - /** |
42 | | - * The Command's Description |
43 | | - * |
44 | | - * @var string |
45 | | - */ |
46 | | - protected $description = 'Generates a new spark command.'; |
47 | | - |
48 | | - /** |
49 | | - * The Command's Usage |
50 | | - * |
51 | | - * @var string |
52 | | - */ |
53 | | - protected $usage = 'make:command <name> [options]'; |
54 | | - |
55 | | - /** |
56 | | - * The Command's Arguments |
57 | | - * |
58 | | - * @var array<string, string> |
59 | | - */ |
60 | | - protected $arguments = [ |
61 | | - 'name' => 'The command class name.', |
62 | | - ]; |
63 | | - |
64 | | - /** |
65 | | - * The Command's Options |
66 | | - * |
67 | | - * @var array<string, string> |
68 | | - */ |
69 | | - protected $options = [ |
70 | | - '--command' => 'The command name. Default: "command:name"', |
71 | | - '--type' => 'The command type. Options [basic, generator]. Default: "basic".', |
72 | | - '--group' => 'The command group. Default: [basic -> "App", generator -> "Generators"].', |
73 | | - '--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".', |
74 | | - '--suffix' => 'Append the component title to the class name (e.g. User => UserCommand).', |
75 | | - '--force' => 'Force overwrite existing file.', |
76 | | - ]; |
77 | | - |
78 | | - /** |
79 | | - * Actually execute a command. |
80 | | - */ |
81 | | - public function run(array $params) |
| 31 | + protected function configure(): void |
| 32 | + { |
| 33 | + parent::configure(); |
| 34 | + |
| 35 | + $this |
| 36 | + ->addOption(new Option( |
| 37 | + name: 'command', |
| 38 | + shortcut: 'c', |
| 39 | + description: 'The command name.', |
| 40 | + requiresValue: true, |
| 41 | + valueLabel: 'name', |
| 42 | + default: 'command:name', |
| 43 | + )) |
| 44 | + ->addOption(new Option( |
| 45 | + name: 'type', |
| 46 | + shortcut: 't', |
| 47 | + description: 'The command type: "basic" or "generator".', |
| 48 | + requiresValue: true, |
| 49 | + default: 'basic', |
| 50 | + )) |
| 51 | + ->addOption(new Option( |
| 52 | + name: 'group', |
| 53 | + shortcut: 'g', |
| 54 | + description: 'The command group. Defaults to "App" for basic and "Generators" for generator commands.', |
| 55 | + acceptsValue: true, |
| 56 | + )); |
| 57 | + } |
| 58 | + |
| 59 | + protected function interact(array &$arguments, array &$options): void |
82 | 60 | { |
83 | | - $this->component = 'Command'; |
84 | | - $this->directory = 'Commands'; |
85 | | - $this->template = 'command.tpl.php'; |
| 61 | + $type = $this->getUnboundOption('type', $options); |
86 | 62 |
|
87 | | - $this->classNameLang = 'CLI.generator.className.command'; |
88 | | - $this->generateClass($params); |
| 63 | + if (! is_string($type) || $type === 'basic' || $type === 'generator') { |
| 64 | + return; |
| 65 | + } |
89 | 66 |
|
90 | | - return EXIT_SUCCESS; |
| 67 | + $options['type'] = CLI::prompt(lang('CLI.generator.commandType'), ['basic', 'generator'], 'required'); |
91 | 68 | } |
92 | 69 |
|
93 | | - /** |
94 | | - * Prepare options and do the necessary replacements. |
95 | | - */ |
96 | | - protected function prepare(string $class): string |
| 70 | + protected function execute(array $arguments, array $options): int |
97 | 71 | { |
98 | | - $command = $this->getOption('command'); |
99 | | - $group = $this->getOption('group'); |
100 | | - $type = $this->getOption('type'); |
101 | | - |
102 | | - $command = is_string($command) ? $command : 'command:name'; |
103 | | - $type = is_string($type) ? $type : 'basic'; |
104 | | - |
105 | | - if (! in_array($type, ['basic', 'generator'], true)) { |
106 | | - // @codeCoverageIgnoreStart |
107 | | - $type = CLI::prompt(lang('CLI.generator.commandType'), ['basic', 'generator'], 'required'); |
108 | | - CLI::newLine(); |
109 | | - // @codeCoverageIgnoreEnd |
| 72 | + $type = $this->getValidatedOption('type'); |
| 73 | + |
| 74 | + if ($type !== 'basic' && $type !== 'generator') { |
| 75 | + CLI::error(lang('CLI.generator.invalidCommandType', [$type])); |
| 76 | + |
| 77 | + return EXIT_ERROR; |
110 | 78 | } |
111 | 79 |
|
| 80 | + return $this->generateClass(); |
| 81 | + } |
| 82 | + |
| 83 | + protected function getReplacements(string $class): array |
| 84 | + { |
| 85 | + $group = $this->getValidatedOption('group'); |
| 86 | + |
112 | 87 | if (! is_string($group)) { |
113 | | - $group = $type === 'generator' ? 'Generators' : 'App'; |
| 88 | + $group = $this->getValidatedOption('type') === 'generator' ? 'Generators' : 'App'; |
114 | 89 | } |
115 | 90 |
|
116 | | - return $this->parseTemplate( |
117 | | - $class, |
118 | | - ['{group}', '{command}'], |
119 | | - [$group, $command], |
120 | | - ['type' => $type], |
121 | | - ); |
| 91 | + return ['{command}' => $this->getValidatedOption('command'), '{group}' => $group]; |
| 92 | + } |
| 93 | + |
| 94 | + protected function getTemplateData(string $class): array |
| 95 | + { |
| 96 | + return ['type' => $this->getValidatedOption('type')]; |
122 | 97 | } |
123 | 98 | } |
0 commit comments