Skip to content

Commit ccf5fe2

Browse files
authored
refactor: migrate the simple make:* generators to AbstractGeneratorCommand (#10528)
* refactor: migrate the simple `make:*` generators to `AbstractGeneratorCommand` * allow `BaseCommand::call()` to also invoke modern commands
1 parent bec4800 commit ccf5fe2

22 files changed

Lines changed: 350 additions & 568 deletions

app/Config/Generators.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ class Generators extends BaseConfig
3030
'class' => 'CodeIgniter\Commands\Generators\Views\cell.tpl.php',
3131
'view' => 'CodeIgniter\Commands\Generators\Views\cell_view.tpl.php',
3232
],
33-
'make:command' => 'CodeIgniter\Commands\Generators\Views\command.tpl.php',
34-
'make:config' => 'CodeIgniter\Commands\Generators\Views\config.tpl.php',
35-
'make:controller' => 'CodeIgniter\Commands\Generators\Views\controller.tpl.php',
36-
'make:entity' => 'CodeIgniter\Commands\Generators\Views\entity.tpl.php',
37-
'make:filter' => 'CodeIgniter\Commands\Generators\Views\filter.tpl.php',
38-
'make:migration' => 'CodeIgniter\Commands\Generators\Views\migration.tpl.php',
39-
'make:model' => 'CodeIgniter\Commands\Generators\Views\model.tpl.php',
40-
'make:seeder' => 'CodeIgniter\Commands\Generators\Views\seeder.tpl.php',
41-
'make:validation' => 'CodeIgniter\Commands\Generators\Views\validation.tpl.php',
33+
'make:command' => 'CodeIgniter\Commands\Generators\Views\command.tpl.php',
34+
'make:config' => 'CodeIgniter\Commands\Generators\Views\config.tpl.php',
35+
'make:controller' => 'CodeIgniter\Commands\Generators\Views\controller.tpl.php',
36+
'make:entity' => 'CodeIgniter\Commands\Generators\Views\entity.tpl.php',
37+
'make:filter' => 'CodeIgniter\Commands\Generators\Views\filter.tpl.php',
38+
'make:migration' => 'CodeIgniter\Commands\Generators\Views\migration.tpl.php',
39+
'make:model' => 'CodeIgniter\Commands\Generators\Views\model.tpl.php',
40+
'make:request' => 'CodeIgniter\Commands\Generators\Views\formrequest.tpl.php',
41+
'make:seeder' => 'CodeIgniter\Commands\Generators\Views\seeder.tpl.php',
42+
'make:test' => 'CodeIgniter\Commands\Generators\Views\test.tpl.php',
43+
'make:transformer' => 'CodeIgniter\Commands\Generators\Views\transformer.tpl.php',
44+
'make:validation' => 'CodeIgniter\Commands\Generators\Views\validation.tpl.php',
4245
];
4346
}

system/CLI/AbstractGeneratorCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected function configure(): void
7373
{
7474
$this->addArgument(new Argument(
7575
name: 'name',
76-
description: 'The name of the class to generate.',
76+
description: sprintf('The %s class name.', lcfirst($this->component)),
7777
required: true,
7878
));
7979
}

system/CLI/BaseCommand.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ abstract public function run(array $params);
108108
/**
109109
* Can be used by a command to run other commands.
110110
*
111+
* For a modern command, integer-keyed params are passed as arguments and string-keyed params as options.
112+
*
111113
* @param array<array-key, string|null> $params
112114
*
113115
* @return int|null
@@ -116,7 +118,23 @@ abstract public function run(array $params);
116118
*/
117119
protected function call(string $command, array $params = [])
118120
{
119-
return $this->commands->runLegacy($command, $params);
121+
if ($this->commands->hasLegacyCommand($command) || ! $this->commands->hasModernCommand($command)) {
122+
return $this->commands->runLegacy($command, $params);
123+
}
124+
125+
$arguments = [];
126+
$options = [];
127+
128+
foreach ($params as $key => $value) {
129+
if (is_int($key)) {
130+
assert(is_string($value));
131+
$arguments[] = $value;
132+
} else {
133+
$options[$key] = $value;
134+
}
135+
}
136+
137+
return $this->commands->runCommand($command, $arguments, $options);
120138
}
121139

122140
/**

system/Commands/Generators/EntityGenerator.php

Lines changed: 12 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,76 +13,17 @@
1313

1414
namespace CodeIgniter\Commands\Generators;
1515

16-
use CodeIgniter\CLI\BaseCommand;
17-
use CodeIgniter\CLI\GeneratorTrait;
18-
19-
/**
20-
* Generates a skeleton Entity file.
21-
*/
22-
class EntityGenerator extends BaseCommand
16+
use CodeIgniter\CLI\AbstractGeneratorCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
use CodeIgniter\CLI\Attributes\GeneratorCommand;
19+
20+
#[Command(name: 'make:entity', description: 'Generates a new entity file.', group: 'Generators')]
21+
#[GeneratorCommand(
22+
component: 'Entity',
23+
template: 'entity.tpl.php',
24+
directory: 'Entities',
25+
classNameLang: 'CLI.generator.className.entity',
26+
)]
27+
class EntityGenerator extends AbstractGeneratorCommand
2328
{
24-
use GeneratorTrait;
25-
26-
/**
27-
* The Command's Group
28-
*
29-
* @var string
30-
*/
31-
protected $group = 'Generators';
32-
33-
/**
34-
* The Command's Name
35-
*
36-
* @var string
37-
*/
38-
protected $name = 'make:entity';
39-
40-
/**
41-
* The Command's Description
42-
*
43-
* @var string
44-
*/
45-
protected $description = 'Generates a new entity file.';
46-
47-
/**
48-
* The Command's Usage
49-
*
50-
* @var string
51-
*/
52-
protected $usage = 'make:entity <name> [options]';
53-
54-
/**
55-
* The Command's Arguments
56-
*
57-
* @var array<string, string>
58-
*/
59-
protected $arguments = [
60-
'name' => 'The entity class name.',
61-
];
62-
63-
/**
64-
* The Command's Options
65-
*
66-
* @var array<string, string>
67-
*/
68-
protected $options = [
69-
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
70-
'--suffix' => 'Append the component title to the class name (e.g. User => UserEntity).',
71-
'--force' => 'Force overwrite existing file.',
72-
];
73-
74-
/**
75-
* Actually execute a command.
76-
*/
77-
public function run(array $params)
78-
{
79-
$this->component = 'Entity';
80-
$this->directory = 'Entities';
81-
$this->template = 'entity.tpl.php';
82-
83-
$this->classNameLang = 'CLI.generator.className.entity';
84-
$this->generateClass($params);
85-
86-
return EXIT_SUCCESS;
87-
}
8829
}

system/Commands/Generators/FilterGenerator.php

Lines changed: 12 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,76 +13,17 @@
1313

1414
namespace CodeIgniter\Commands\Generators;
1515

16-
use CodeIgniter\CLI\BaseCommand;
17-
use CodeIgniter\CLI\GeneratorTrait;
18-
19-
/**
20-
* Generates a skeleton Filter file.
21-
*/
22-
class FilterGenerator extends BaseCommand
16+
use CodeIgniter\CLI\AbstractGeneratorCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
use CodeIgniter\CLI\Attributes\GeneratorCommand;
19+
20+
#[Command(name: 'make:filter', description: 'Generates a new filter file.', group: 'Generators')]
21+
#[GeneratorCommand(
22+
component: 'Filter',
23+
template: 'filter.tpl.php',
24+
directory: 'Filters',
25+
classNameLang: 'CLI.generator.className.filter',
26+
)]
27+
class FilterGenerator extends AbstractGeneratorCommand
2328
{
24-
use GeneratorTrait;
25-
26-
/**
27-
* The Command's Group
28-
*
29-
* @var string
30-
*/
31-
protected $group = 'Generators';
32-
33-
/**
34-
* The Command's Name
35-
*
36-
* @var string
37-
*/
38-
protected $name = 'make:filter';
39-
40-
/**
41-
* The Command's Description
42-
*
43-
* @var string
44-
*/
45-
protected $description = 'Generates a new filter file.';
46-
47-
/**
48-
* The Command's Usage
49-
*
50-
* @var string
51-
*/
52-
protected $usage = 'make:filter <name> [options]';
53-
54-
/**
55-
* The Command's Arguments
56-
*
57-
* @var array<string, string>
58-
*/
59-
protected $arguments = [
60-
'name' => 'The filter class name.',
61-
];
62-
63-
/**
64-
* The Command's Options
65-
*
66-
* @var array<string, string>
67-
*/
68-
protected $options = [
69-
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
70-
'--suffix' => 'Append the component title to the class name (e.g. User => UserFilter).',
71-
'--force' => 'Force overwrite existing file.',
72-
];
73-
74-
/**
75-
* Actually execute a command.
76-
*/
77-
public function run(array $params)
78-
{
79-
$this->component = 'Filter';
80-
$this->directory = 'Filters';
81-
$this->template = 'filter.tpl.php';
82-
83-
$this->classNameLang = 'CLI.generator.className.filter';
84-
$this->generateClass($params);
85-
86-
return EXIT_SUCCESS;
87-
}
8829
}

system/Commands/Generators/FormRequestGenerator.php

Lines changed: 12 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,76 +13,17 @@
1313

1414
namespace CodeIgniter\Commands\Generators;
1515

16-
use CodeIgniter\CLI\BaseCommand;
17-
use CodeIgniter\CLI\GeneratorTrait;
18-
19-
/**
20-
* Generates a skeleton FormRequest file.
21-
*/
22-
class FormRequestGenerator extends BaseCommand
16+
use CodeIgniter\CLI\AbstractGeneratorCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
use CodeIgniter\CLI\Attributes\GeneratorCommand;
19+
20+
#[Command(name: 'make:request', description: 'Generates a new FormRequest file.', group: 'Generators')]
21+
#[GeneratorCommand(
22+
component: 'Request',
23+
template: 'formrequest.tpl.php',
24+
directory: 'Requests',
25+
classNameLang: 'CLI.generator.className.request',
26+
)]
27+
class FormRequestGenerator extends AbstractGeneratorCommand
2328
{
24-
use GeneratorTrait;
25-
26-
/**
27-
* The Command's Group
28-
*
29-
* @var string
30-
*/
31-
protected $group = 'Generators';
32-
33-
/**
34-
* The Command's Name
35-
*
36-
* @var string
37-
*/
38-
protected $name = 'make:request';
39-
40-
/**
41-
* The Command's Description
42-
*
43-
* @var string
44-
*/
45-
protected $description = 'Generates a new FormRequest file.';
46-
47-
/**
48-
* The Command's Usage
49-
*
50-
* @var string
51-
*/
52-
protected $usage = 'make:request <name> [options]';
53-
54-
/**
55-
* The Command's Arguments
56-
*
57-
* @var array<string, string>
58-
*/
59-
protected $arguments = [
60-
'name' => 'The FormRequest class name.',
61-
];
62-
63-
/**
64-
* The Command's Options
65-
*
66-
* @var array<string, string>
67-
*/
68-
protected $options = [
69-
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
70-
'--suffix' => 'Append the component title to the class name (e.g. User => UserRequest).',
71-
'--force' => 'Force overwrite existing file.',
72-
];
73-
74-
/**
75-
* Actually execute a command.
76-
*/
77-
public function run(array $params)
78-
{
79-
$this->component = 'Request';
80-
$this->directory = 'Requests';
81-
$this->template = 'formrequest.tpl.php';
82-
83-
$this->classNameLang = 'CLI.generator.className.request';
84-
$this->generateClass($params);
85-
86-
return EXIT_SUCCESS;
87-
}
8829
}

system/Commands/Generators/ModelGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ protected function prepare(string $class): string
130130

131131
// Call the entity generator with the fully-qualified class name so
132132
// it ends up under the correct sub-namespace/folder (eg. Admin).
133-
$this->call('make:entity', array_merge([trim($entityClass, '\\')], $this->params));
133+
$entityOptions = array_intersect_key($this->params, array_flip(['namespace', 'suffix', 'force']));
134+
135+
$this->call('make:entity', array_merge([trim($entityClass, '\\')], $entityOptions));
134136

135137
$return = '\\' . trim($entityClass, '\\') . '::class';
136138
} else {

0 commit comments

Comments
 (0)