Skip to content

Commit 9dbf715

Browse files
authored
refactor: write the generator commands through $this->getOutput() (#10690)
1 parent e4f6a02 commit 9dbf715

5 files changed

Lines changed: 20 additions & 21 deletions

File tree

‎system/CLI/AbstractGeneratorCommand.php‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ protected function buildPath(string $class): string
278278
$basePath = $this->getBasePath($namespace);
279279

280280
if ($basePath === null) {
281-
CLI::error(lang('CLI.namespaceNotDefined', [$namespace]));
281+
$this->getOutput()->error(lang('CLI.namespaceNotDefined', [$namespace]));
282282

283283
return '';
284284
}
@@ -323,8 +323,10 @@ protected function getNamespace(): string
323323
*/
324324
private function generate(string $class): int
325325
{
326+
$output = $this->getOutput();
327+
326328
if (preg_match(self::CLASS_NAME_PATTERN, $class) !== 1) {
327-
CLI::error(lang('CLI.generator.invalidClassName', [$class]));
329+
$output->error(lang('CLI.generator.invalidClassName', [$class]));
328330

329331
return EXIT_ERROR;
330332
}
@@ -335,23 +337,23 @@ private function generate(string $class): int
335337
return EXIT_ERROR;
336338
}
337339

338-
return $this->generateFile($target, $this->buildContent($class));
340+
return $this->generateFile($output, $target, $this->buildContent($class));
339341
}
340342

341343
/**
342344
* Writes the generated file to disk with all the safety checks around that.
343345
*/
344-
private function generateFile(string $target, string $content): int
346+
private function generateFile(Output $output, string $target, string $content): int
345347
{
346-
if (str_starts_with($target, SYSTEMPATH) && ! $this->confirmCodeIgniterNamespaceOnce()) {
348+
if (str_starts_with($target, SYSTEMPATH) && ! $this->confirmCodeIgniterNamespaceOnce($output)) {
347349
return EXIT_SUCCESS;
348350
}
349351

350352
$isFile = is_file($target);
351353
$force = $this->hasOption('force') && $this->getValidatedOption('force') === true;
352354

353355
if (! $force && $isFile) {
354-
CLI::error(lang('CLI.generator.fileExist', [clean_path($target)]));
356+
$output->error(lang('CLI.generator.fileExist', [clean_path($target)]));
355357

356358
return EXIT_ERROR;
357359
}
@@ -366,31 +368,31 @@ private function generateFile(string $target, string $content): int
366368

367369
if (! write_file($target, $content)) {
368370
// @codeCoverageIgnoreStart
369-
CLI::error(lang('CLI.generator.fileError', [clean_path($target)]));
371+
$output->error(lang('CLI.generator.fileError', [clean_path($target)]));
370372

371373
return EXIT_ERROR;
372374
// @codeCoverageIgnoreEnd
373375
}
374376

375377
if ($isFile) {
376-
CLI::write(lang('CLI.generator.fileOverwrite', [clean_path($target)]), 'yellow');
378+
$output->write(lang('CLI.generator.fileOverwrite', [clean_path($target)]), 'yellow');
377379
} else {
378-
CLI::write(lang('CLI.generator.fileCreate', [clean_path($target)]), 'green');
380+
$output->write(lang('CLI.generator.fileCreate', [clean_path($target)]), 'green');
379381
}
380382

381383
return EXIT_SUCCESS;
382384
}
383385

384-
private function confirmCodeIgniterNamespaceOnce(): bool
386+
private function confirmCodeIgniterNamespaceOnce(Output $output): bool
385387
{
386388
if ($this->confirmedCodeIgniterNamespace !== null) {
387389
return $this->confirmedCodeIgniterNamespace;
388390
}
389391

390-
CLI::write(lang('CLI.generator.usingCINamespace'), 'yellow');
392+
$output->write(lang('CLI.generator.usingCINamespace'), 'yellow');
391393

392394
if ($this->isInteractive() && $this->getPrompt()->ask(lang('CLI.generator.confirmContinue'), ['y', 'n'], 'required') === 'n') {
393-
CLI::write(lang('CLI.generator.cancelOperation'), 'yellow');
395+
$output->write(lang('CLI.generator.cancelOperation'), 'yellow');
394396

395397
return $this->confirmedCodeIgniterNamespace = false;
396398
}

‎system/Commands/Generators/CommandGenerator.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\CLI\AbstractGeneratorCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
1818
use CodeIgniter\CLI\Attributes\GeneratorCommand;
19-
use CodeIgniter\CLI\CLI;
2019
use CodeIgniter\CLI\Input\Option;
2120

2221
#[Command(name: 'make:command', description: 'Generates a new spark command.', group: 'Generators')]
@@ -75,7 +74,7 @@ protected function execute(array $arguments, array $options): int
7574
$type = $this->getValidatedOption('type');
7675

7776
if ($type !== 'basic' && $type !== 'generator') {
78-
CLI::error(lang('CLI.generator.invalidCommandType', [$type]));
77+
$this->getOutput()->error(lang('CLI.generator.invalidCommandType', [$type]));
7978

8079
return EXIT_ERROR;
8180
}

‎system/Commands/Generators/ControllerGenerator.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\CLI\AbstractGeneratorCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
1818
use CodeIgniter\CLI\Attributes\GeneratorCommand;
19-
use CodeIgniter\CLI\CLI;
2019
use CodeIgniter\CLI\Input\Option;
2120
use CodeIgniter\Controller;
2221
use CodeIgniter\RESTful\ResourceController;
@@ -69,7 +68,7 @@ protected function execute(array $arguments, array $options): int
6968
$type = $this->getResourceType();
7069

7170
if (! in_array($type, [null, 'controller', 'presenter'], true)) {
72-
CLI::error(lang('CLI.generator.invalidParentClass', [$type]));
71+
$this->getOutput()->error(lang('CLI.generator.invalidParentClass', [$type]));
7372

7473
return EXIT_ERROR;
7574
}

‎system/Commands/Generators/MigrationGenerator.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\CLI\AbstractGeneratorCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
1818
use CodeIgniter\CLI\Attributes\GeneratorCommand;
19-
use CodeIgniter\CLI\CLI;
2019
use CodeIgniter\CLI\Input\Option;
2120
use Config\Database;
2221
use Config\Migrations;
@@ -78,15 +77,16 @@ protected function execute(array $arguments, array $options): int
7877
if ($this->getValidatedOption('session') === true) {
7978
$group = $this->getDatabaseGroup();
8079
$driver = $this->getDatabaseDriver($group);
80+
$output = $this->getOutput();
8181

8282
if ($driver === null) {
83-
CLI::error(lang('CLI.generator.undefinedDatabaseGroup', [$group]));
83+
$output->error(lang('CLI.generator.undefinedDatabaseGroup', [$group]));
8484

8585
return EXIT_ERROR;
8686
}
8787

8888
if ($driver !== 'MySQLi' && $driver !== 'Postgre') {
89-
CLI::error(lang('CLI.generator.unsupportedSessionDriver', [$group, $driver]));
89+
$output->error(lang('CLI.generator.unsupportedSessionDriver', [$group, $driver]));
9090

9191
return EXIT_ERROR;
9292
}

‎system/Commands/Generators/ModelGenerator.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\CLI\AbstractGeneratorCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
1818
use CodeIgniter\CLI\Attributes\GeneratorCommand;
19-
use CodeIgniter\CLI\CLI;
2019
use CodeIgniter\CLI\Input\Option;
2120

2221
#[Command(name: 'make:model', description: 'Generates a new model file.', group: 'Generators')]
@@ -78,7 +77,7 @@ protected function execute(array $arguments, array $options): int
7877
$return = $this->getValidatedOption('return');
7978

8079
if (! in_array($return, self::RETURN_TYPES, true)) {
81-
CLI::error(lang('CLI.generator.invalidReturnType', [$return]));
80+
$this->getOutput()->error(lang('CLI.generator.invalidReturnType', [$return]));
8281

8382
return EXIT_ERROR;
8483
}

0 commit comments

Comments
 (0)