Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions system/CLI/AbstractGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ protected function buildPath(string $class): string
$basePath = $this->getBasePath($namespace);

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

return '';
}
Expand Down Expand Up @@ -323,8 +323,10 @@ protected function getNamespace(): string
*/
private function generate(string $class): int
{
$output = $this->getOutput();

if (preg_match(self::CLASS_NAME_PATTERN, $class) !== 1) {
CLI::error(lang('CLI.generator.invalidClassName', [$class]));
$output->error(lang('CLI.generator.invalidClassName', [$class]));

return EXIT_ERROR;
}
Expand All @@ -335,23 +337,23 @@ private function generate(string $class): int
return EXIT_ERROR;
}

return $this->generateFile($target, $this->buildContent($class));
return $this->generateFile($output, $target, $this->buildContent($class));
}

/**
* Writes the generated file to disk with all the safety checks around that.
*/
private function generateFile(string $target, string $content): int
private function generateFile(Output $output, string $target, string $content): int
{
if (str_starts_with($target, SYSTEMPATH) && ! $this->confirmCodeIgniterNamespaceOnce()) {
if (str_starts_with($target, SYSTEMPATH) && ! $this->confirmCodeIgniterNamespaceOnce($output)) {
return EXIT_SUCCESS;
}

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

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

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

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

return EXIT_ERROR;
// @codeCoverageIgnoreEnd
}

if ($isFile) {
CLI::write(lang('CLI.generator.fileOverwrite', [clean_path($target)]), 'yellow');
$output->write(lang('CLI.generator.fileOverwrite', [clean_path($target)]), 'yellow');
} else {
CLI::write(lang('CLI.generator.fileCreate', [clean_path($target)]), 'green');
$output->write(lang('CLI.generator.fileCreate', [clean_path($target)]), 'green');
}

return EXIT_SUCCESS;
}

private function confirmCodeIgniterNamespaceOnce(): bool
private function confirmCodeIgniterNamespaceOnce(Output $output): bool
{
if ($this->confirmedCodeIgniterNamespace !== null) {
return $this->confirmedCodeIgniterNamespace;
}

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

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

return $this->confirmedCodeIgniterNamespace = false;
}
Expand Down
3 changes: 1 addition & 2 deletions system/Commands/Generators/CommandGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use CodeIgniter\CLI\AbstractGeneratorCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\Attributes\GeneratorCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Option;

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

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

return EXIT_ERROR;
}
Expand Down
3 changes: 1 addition & 2 deletions system/Commands/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use CodeIgniter\CLI\AbstractGeneratorCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\Attributes\GeneratorCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Option;
use CodeIgniter\Controller;
use CodeIgniter\RESTful\ResourceController;
Expand Down Expand Up @@ -69,7 +68,7 @@ protected function execute(array $arguments, array $options): int
$type = $this->getResourceType();

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

return EXIT_ERROR;
}
Expand Down
6 changes: 3 additions & 3 deletions system/Commands/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use CodeIgniter\CLI\AbstractGeneratorCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\Attributes\GeneratorCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Option;
use Config\Database;
use Config\Migrations;
Expand Down Expand Up @@ -78,15 +77,16 @@ protected function execute(array $arguments, array $options): int
if ($this->getValidatedOption('session') === true) {
$group = $this->getDatabaseGroup();
$driver = $this->getDatabaseDriver($group);
$output = $this->getOutput();

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

return EXIT_ERROR;
}

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

return EXIT_ERROR;
}
Expand Down
3 changes: 1 addition & 2 deletions system/Commands/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use CodeIgniter\CLI\AbstractGeneratorCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\Attributes\GeneratorCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Option;

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

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

return EXIT_ERROR;
}
Expand Down
Loading