Skip to content

Commit ca87305

Browse files
authored
refactor: migrate make:config to AbstractGeneratorCommand (#10535)
1 parent ccf5fe2 commit ca87305

3 files changed

Lines changed: 85 additions & 89 deletions

File tree

‎system/Commands/Generators/ConfigGenerator.php‎

Lines changed: 20 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,90 +13,31 @@
1313

1414
namespace CodeIgniter\Commands\Generators;
1515

16-
use CodeIgniter\CLI\BaseCommand;
17-
use CodeIgniter\CLI\GeneratorTrait;
18-
19-
/**
20-
* Generates a skeleton config file.
21-
*/
22-
class ConfigGenerator extends BaseCommand
16+
use CodeIgniter\CLI\AbstractGeneratorCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
use CodeIgniter\CLI\Attributes\GeneratorCommand;
19+
20+
#[Command(name: 'make:config', description: 'Generates a new config file.', group: 'Generators')]
21+
#[GeneratorCommand(
22+
component: 'Config',
23+
template: 'config.tpl.php',
24+
directory: 'Config',
25+
classNameLang: 'CLI.generator.className.config',
26+
)]
27+
class ConfigGenerator 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:config';
39-
40-
/**
41-
* The Command's Description
42-
*
43-
* @var string
44-
*/
45-
protected $description = 'Generates a new config file.';
46-
47-
/**
48-
* The Command's Usage
49-
*
50-
* @var string
51-
*/
52-
protected $usage = 'make:config <name> [options]';
53-
54-
/**
55-
* The Command's Arguments
56-
*
57-
* @var array<string, string>
58-
*/
59-
protected $arguments = [
60-
'name' => 'The config 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 => UserConfig).',
71-
'--force' => 'Force overwrite existing file.',
72-
];
73-
74-
/**
75-
* Actually execute a command.
76-
*/
77-
public function run(array $params)
29+
protected function getReplacements(string $class): array
7830
{
79-
$this->component = 'Config';
80-
$this->directory = 'Config';
81-
$this->template = 'config.tpl.php';
82-
83-
$this->classNameLang = 'CLI.generator.className.config';
84-
$this->generateClass($params);
31+
$segments = explode('\\', $class);
32+
array_pop($segments);
8533

86-
return EXIT_SUCCESS;
87-
}
88-
89-
/**
90-
* Prepare options and do the necessary replacements.
91-
*/
92-
protected function prepare(string $class): string
93-
{
94-
$namespace = $this->getOption('namespace') ?? APP_NAMESPACE;
34+
$namespace = implode('\\', $segments);
35+
$prefix = APP_NAMESPACE . '\\';
9536

96-
if ($namespace === APP_NAMESPACE) {
97-
$class = substr($class, strlen($namespace . '\\'));
37+
if (! str_starts_with($namespace, $prefix)) {
38+
return [];
9839
}
9940

100-
return $this->parseTemplate($class);
41+
return ['{namespace}' => substr($namespace, strlen($prefix))];
10142
}
10243
}

‎tests/system/Commands/Generators/ConfigGeneratorTest.php‎

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Commands\Generators;
1515

16+
use CodeIgniter\CLI\CLI;
1617
use CodeIgniter\Test\CIUnitTestCase;
1718
use CodeIgniter\Test\StreamFilterTrait;
1819
use PHPUnit\Framework\Attributes\Group;
@@ -25,24 +26,78 @@ final class ConfigGeneratorTest extends CIUnitTestCase
2526
{
2627
use StreamFilterTrait;
2728

29+
protected function setUp(): void
30+
{
31+
parent::setUp();
32+
33+
CLI::reset();
34+
}
35+
2836
protected function tearDown(): void
2937
{
30-
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
31-
$file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
32-
if (is_file($file)) {
33-
unlink($file);
38+
parent::tearDown();
39+
40+
CLI::reset();
41+
42+
foreach (['Auth.php', 'AuthConfig.php'] as $file) {
43+
if (is_file(APPPATH . 'Config/' . $file)) {
44+
unlink(APPPATH . 'Config/' . $file);
45+
}
3446
}
47+
48+
if (is_dir(APPPATH . 'Config/Sub')) {
49+
helper('filesystem');
50+
delete_files(APPPATH . 'Config/Sub', true);
51+
rmdir(APPPATH . 'Config/Sub');
52+
}
53+
54+
if (is_file(SUPPORTPATH . 'Config/Auth.php')) {
55+
unlink(SUPPORTPATH . 'Config/Auth.php');
56+
}
57+
}
58+
59+
private function getUndecoratedBuffer(): string
60+
{
61+
return preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()) ?? '';
3562
}
3663

3764
public function testGenerateConfig(): void
3865
{
3966
command('make:config auth');
40-
$this->assertFileExists(APPPATH . 'Config/Auth.php');
67+
68+
$this->assertSame(
69+
PHP_EOL . 'File created: ' . clean_path(APPPATH . 'Config/Auth.php') . PHP_EOL,
70+
$this->getUndecoratedBuffer(),
71+
);
72+
73+
$content = file_get_contents(APPPATH . 'Config/Auth.php');
74+
$this->assertIsString($content);
75+
$this->assertStringContainsString('namespace Config;', $content);
76+
$this->assertStringContainsString('class Auth extends BaseConfig', $content);
77+
}
78+
79+
public function testGenerateConfigInSubdirectory(): void
80+
{
81+
command('make:config sub/auth');
82+
83+
$content = file_get_contents(APPPATH . 'Config/Sub/Auth.php');
84+
$this->assertIsString($content);
85+
$this->assertStringContainsString('namespace Config\\Sub;', $content);
86+
}
87+
88+
public function testGenerateConfigWithOtherNamespaceKeepsFullNamespace(): void
89+
{
90+
command('make:config auth --namespace Tests\\\\Support');
91+
92+
$content = file_get_contents(SUPPORTPATH . 'Config/Auth.php');
93+
$this->assertIsString($content);
94+
$this->assertStringContainsString('namespace Tests\\Support\\Config;', $content);
4195
}
4296

43-
public function testGenerateConfigWithOptionSuffix(): void
97+
public function testGenerateConfigWithSuffix(): void
4498
{
45-
command('make:config auth -suffix');
99+
command('make:config auth --suffix');
100+
46101
$this->assertFileExists(APPPATH . 'Config/AuthConfig.php');
47102
}
48103
}

‎user_guide_src/source/cli/cli_generators.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ Argument:
108108

109109
Options:
110110
========
111-
* ``--namespace``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``.
112-
* ``--suffix``: Append the component suffix to the generated class name.
113-
* ``--force``: Set this flag to overwrite existing files on destination.
111+
* ``--namespace`` (``-n``): Set the root namespace. Defaults to value of ``APP_NAMESPACE``.
112+
* ``--suffix`` (``-s``): Append the component suffix to the generated class name.
113+
* ``--force`` (``-f``): Set this flag to overwrite existing files on destination.
114114

115115
make:controller
116116
---------------

0 commit comments

Comments
 (0)