|
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; |
| 20 | +use CodeIgniter\CLI\Input\Option; |
19 | 21 | use CodeIgniter\Controller; |
20 | 22 | use CodeIgniter\RESTful\ResourceController; |
21 | 23 | use CodeIgniter\RESTful\ResourcePresenter; |
22 | 24 |
|
23 | | -/** |
24 | | - * Generates a skeleton controller file. |
25 | | - */ |
26 | | -class ControllerGenerator extends BaseCommand |
| 25 | +#[Command(name: 'make:controller', description: 'Generates a new controller file.', group: 'Generators')] |
| 26 | +#[GeneratorCommand( |
| 27 | + component: 'Controller', |
| 28 | + template: 'controller.tpl.php', |
| 29 | + directory: 'Controllers', |
| 30 | + classNameLang: 'CLI.generator.className.controller', |
| 31 | +)] |
| 32 | +class ControllerGenerator extends AbstractGeneratorCommand |
27 | 33 | { |
28 | | - use GeneratorTrait; |
| 34 | + protected function configure(): void |
| 35 | + { |
| 36 | + parent::configure(); |
| 37 | + |
| 38 | + $this |
| 39 | + ->addOption(new Option( |
| 40 | + name: 'bare', |
| 41 | + shortcut: 'b', |
| 42 | + description: 'Extend CodeIgniter\Controller instead of BaseController.', |
| 43 | + )) |
| 44 | + ->addOption(new Option( |
| 45 | + name: 'restful', |
| 46 | + shortcut: 'r', |
| 47 | + description: 'Extend a RESTful resource: "controller" (default when no value is given) or "presenter".', |
| 48 | + acceptsValue: true, |
| 49 | + valueLabel: 'type', |
| 50 | + )); |
| 51 | + } |
29 | 52 |
|
30 | | - /** |
31 | | - * The Command's Group |
32 | | - * |
33 | | - * @var string |
34 | | - */ |
35 | | - protected $group = 'Generators'; |
| 53 | + protected function interact(array &$arguments, array &$options): void |
| 54 | + { |
| 55 | + $type = $this->getUnboundOption('restful', $options); |
36 | 56 |
|
37 | | - /** |
38 | | - * The Command's Name |
39 | | - * |
40 | | - * @var string |
41 | | - */ |
42 | | - protected $name = 'make:controller'; |
| 57 | + if (! is_string($type) || $type === 'controller' || $type === 'presenter') { |
| 58 | + return; |
| 59 | + } |
43 | 60 |
|
44 | | - /** |
45 | | - * The Command's Description |
46 | | - * |
47 | | - * @var string |
48 | | - */ |
49 | | - protected $description = 'Generates a new controller file.'; |
| 61 | + $options['restful'] = CLI::prompt(lang('CLI.generator.parentClass'), ['controller', 'presenter'], 'required'); |
| 62 | + } |
50 | 63 |
|
51 | | - /** |
52 | | - * The Command's Usage |
53 | | - * |
54 | | - * @var string |
55 | | - */ |
56 | | - protected $usage = 'make:controller <name> [options]'; |
| 64 | + protected function execute(array $arguments, array $options): int |
| 65 | + { |
| 66 | + $type = $this->getResourceType(); |
57 | 67 |
|
58 | | - /** |
59 | | - * The Command's Arguments |
60 | | - * |
61 | | - * @var array<string, string> |
62 | | - */ |
63 | | - protected $arguments = [ |
64 | | - 'name' => 'The controller class name.', |
65 | | - ]; |
| 68 | + if (! in_array($type, [null, 'controller', 'presenter'], true)) { |
| 69 | + CLI::error(lang('CLI.generator.invalidParentClass', [$type])); |
66 | 70 |
|
67 | | - /** |
68 | | - * The Command's Options |
69 | | - * |
70 | | - * @var array<string, string> |
71 | | - */ |
72 | | - protected $options = [ |
73 | | - '--bare' => 'Extends from CodeIgniter\Controller instead of BaseController.', |
74 | | - '--restful' => 'Extends from a RESTful resource, Options: [controller, presenter]. Default: "controller".', |
75 | | - '--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".', |
76 | | - '--suffix' => 'Append the component title to the class name (e.g. User => UserController).', |
77 | | - '--force' => 'Force overwrite existing file.', |
78 | | - ]; |
| 71 | + return EXIT_ERROR; |
| 72 | + } |
79 | 73 |
|
80 | | - /** |
81 | | - * Actually execute a command. |
82 | | - */ |
83 | | - public function run(array $params) |
| 74 | + return $this->generateClass(); |
| 75 | + } |
| 76 | + |
| 77 | + protected function getReplacements(string $class): array |
84 | 78 | { |
85 | | - $this->component = 'Controller'; |
86 | | - $this->directory = 'Controllers'; |
87 | | - $this->template = 'controller.tpl.php'; |
| 79 | + $parent = $this->getParentClass(); |
88 | 80 |
|
89 | | - $this->classNameLang = 'CLI.generator.className.controller'; |
90 | | - $this->generateClass($params); |
| 81 | + return ['{useStatement}' => $parent, '{extends}' => class_basename($parent)]; |
| 82 | + } |
| 83 | + |
| 84 | + protected function getTemplateData(string $class): array |
| 85 | + { |
| 86 | + return ['type' => $this->getValidatedOption('bare') === true ? null : $this->getResourceType()]; |
| 87 | + } |
91 | 88 |
|
92 | | - return EXIT_SUCCESS; |
| 89 | + private function getParentClass(): string |
| 90 | + { |
| 91 | + if ($this->getValidatedOption('bare') === true) { |
| 92 | + return Controller::class; |
| 93 | + } |
| 94 | + |
| 95 | + return match ($this->getResourceType()) { |
| 96 | + 'controller' => ResourceController::class, |
| 97 | + 'presenter' => ResourcePresenter::class, |
| 98 | + default => trim(APP_NAMESPACE, '\\') . '\\Controllers\\BaseController', |
| 99 | + }; |
93 | 100 | } |
94 | 101 |
|
95 | 102 | /** |
96 | | - * Prepare options and do the necessary replacements. |
| 103 | + * Returns the RESTful resource type, or `null` when `--restful` was not passed. |
97 | 104 | */ |
98 | | - protected function prepare(string $class): string |
| 105 | + private function getResourceType(): ?string |
99 | 106 | { |
100 | | - $bare = $this->getOption('bare'); |
101 | | - $rest = $this->getOption('restful'); |
102 | | - |
103 | | - $useStatement = trim(APP_NAMESPACE, '\\') . '\Controllers\BaseController'; |
104 | | - $extends = 'BaseController'; |
105 | | - |
106 | | - // Gets the appropriate parent class to extend. |
107 | | - if ($bare || $rest) { |
108 | | - if ($bare) { |
109 | | - $useStatement = Controller::class; |
110 | | - $extends = 'Controller'; |
111 | | - } elseif ($rest) { |
112 | | - $rest = is_string($rest) ? $rest : 'controller'; |
113 | | - |
114 | | - if (! in_array($rest, ['controller', 'presenter'], true)) { |
115 | | - // @codeCoverageIgnoreStart |
116 | | - $rest = CLI::prompt(lang('CLI.generator.parentClass'), ['controller', 'presenter'], 'required'); |
117 | | - CLI::newLine(); |
118 | | - // @codeCoverageIgnoreEnd |
119 | | - } |
120 | | - |
121 | | - if ($rest === 'controller') { |
122 | | - $useStatement = ResourceController::class; |
123 | | - $extends = 'ResourceController'; |
124 | | - } elseif ($rest === 'presenter') { |
125 | | - $useStatement = ResourcePresenter::class; |
126 | | - $extends = 'ResourcePresenter'; |
127 | | - } |
128 | | - } |
| 107 | + if (! $this->hasUnboundOption('restful')) { |
| 108 | + return null; |
129 | 109 | } |
130 | 110 |
|
131 | | - return $this->parseTemplate( |
132 | | - $class, |
133 | | - ['{useStatement}', '{extends}'], |
134 | | - [$useStatement, $extends], |
135 | | - ['type' => $rest], |
136 | | - ); |
| 111 | + $type = $this->getValidatedOption('restful'); |
| 112 | + |
| 113 | + return is_string($type) ? $type : 'controller'; |
137 | 114 | } |
138 | 115 | } |
0 commit comments