diff --git a/system/Commands/Database/CreateDatabase.php b/system/Commands/Database/CreateDatabase.php index d5129989a5ff..5f6dec8c74da 100644 --- a/system/Commands/Database/CreateDatabase.php +++ b/system/Commands/Database/CreateDatabase.php @@ -15,7 +15,6 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Argument; use CodeIgniter\CLI\Input\Option; use CodeIgniter\Config\Factories; @@ -53,7 +52,7 @@ protected function configure(): void protected function interact(array &$arguments, array &$options): void { if ($arguments === []) { - $arguments[] = CLI::prompt('Database name', null, 'required'); + $arguments[] = $this->getPrompt()->ask('Database name', null, 'required'); } $ext = $this->getUnboundOption('ext', $options); @@ -71,6 +70,8 @@ protected function execute(array $arguments, array $options): int $name = $arguments['db_name']; assert(is_string($name)); + $output = $this->getOutput(); + try { $config = config(Database::class); @@ -86,7 +87,7 @@ protected function execute(array $arguments, array $options): int assert(is_string($ext)); if (! in_array($ext, self::VALID_EXTENSIONS, true)) { - CLI::error(sprintf('Invalid file extension "%s". Use either `db` or `sqlite`.', $ext), 'light_gray', 'red'); + $output->error(sprintf('Invalid file extension "%s". Use either `db` or `sqlite`.', $ext), 'light_gray', 'red'); return EXIT_ERROR; } @@ -102,7 +103,7 @@ protected function execute(array $arguments, array $options): int $dbName = str_contains($name, DIRECTORY_SEPARATOR) ? $name : WRITEPATH . $name; if (is_file($dbName)) { - CLI::error("Database \"{$dbName}\" already exists.", 'light_gray', 'red'); + $output->error("Database \"{$dbName}\" already exists.", 'light_gray', 'red'); return EXIT_ERROR; } @@ -116,18 +117,18 @@ protected function execute(array $arguments, array $options): int if (! is_file($db->getDatabase()) && $name !== ':memory:') { // @codeCoverageIgnoreStart - CLI::error('Database creation failed.', 'light_gray', 'red'); + $output->error('Database creation failed.', 'light_gray', 'red'); return EXIT_ERROR; // @codeCoverageIgnoreEnd } } elseif (! Database::forge()->createDatabase($name)) { - CLI::error('Database creation failed.', 'light_gray', 'red'); + $output->error('Database creation failed.', 'light_gray', 'red'); return EXIT_ERROR; } - CLI::write("Database \"{$name}\" successfully created.", 'green'); + $output->write("Database \"{$name}\" successfully created.", 'green'); return EXIT_SUCCESS; } catch (Throwable $e) { diff --git a/system/Commands/Database/Migrate.php b/system/Commands/Database/Migrate.php index e91aa972ef9f..864404f30add 100644 --- a/system/Commands/Database/Migrate.php +++ b/system/Commands/Database/Migrate.php @@ -15,7 +15,6 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; use CodeIgniter\CLI\SignalTrait; use CodeIgniter\Database\MigrationRunner; @@ -62,7 +61,9 @@ protected function execute(array $arguments, array $options): int $runner = service('migrations'); $runner->clearCliMessages(); - CLI::write(lang('Migrations.latest'), 'yellow'); + $output = $this->getOutput(); + + $output->write(lang('Migrations.latest'), 'yellow'); $namespace = $options['namespace']; assert(is_string($namespace)); @@ -79,19 +80,19 @@ protected function execute(array $arguments, array $options): int $runner->setNamespace($namespace); } - $this->withSignalsBlocked(static function () use ($runner, $group): void { + $this->withSignalsBlocked(static function () use ($runner, $group, $output): void { if (! $runner->latest($group)) { - CLI::error(lang('Migrations.generalFault'), 'light_gray', 'red'); + $output->error(lang('Migrations.generalFault'), 'light_gray', 'red'); } }); $messages = $runner->getCliMessages(); foreach ($messages as $message) { - CLI::write($message); + $output->write($message); } - CLI::write(lang('Migrations.migrated'), 'green'); + $output->write(lang('Migrations.migrated'), 'green'); return EXIT_SUCCESS; } catch (Throwable $e) { diff --git a/system/Commands/Database/MigrateRefresh.php b/system/Commands/Database/MigrateRefresh.php index c8d867059108..4a20f3c24b8e 100644 --- a/system/Commands/Database/MigrateRefresh.php +++ b/system/Commands/Database/MigrateRefresh.php @@ -15,7 +15,6 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; use CodeIgniter\CLI\SignalTrait; @@ -69,7 +68,7 @@ protected function interact(array &$arguments, array &$options): void return; } - if (CLI::prompt(lang('Migrations.refreshConfirm'), ['y', 'n']) === 'y') { + if ($this->getPrompt()->ask(lang('Migrations.refreshConfirm'), ['y', 'n']) === 'y') { $options['force'] = null; // simulate the presence of the --force option } } diff --git a/system/Commands/Database/MigrateRollback.php b/system/Commands/Database/MigrateRollback.php index f11e3a30e6a4..9fa65e143257 100644 --- a/system/Commands/Database/MigrateRollback.php +++ b/system/Commands/Database/MigrateRollback.php @@ -15,7 +15,6 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; use CodeIgniter\CLI\SignalTrait; use CodeIgniter\Database\MigrationRunner; @@ -60,7 +59,7 @@ protected function interact(array &$arguments, array &$options): void return; } - if (CLI::prompt(lang('Migrations.rollBackConfirm'), ['y', 'n']) === 'y') { + if ($this->getPrompt()->ask(lang('Migrations.rollBackConfirm'), ['y', 'n']) === 'y') { $options['force'] = null; // simulate the presence of the --force option } } @@ -73,6 +72,7 @@ protected function execute(array $arguments, array $options): int /** @var MigrationRunner $runner */ $runner = service('migrations'); + $output = $this->getOutput(); try { $batch = $options['batch']; @@ -81,18 +81,18 @@ protected function execute(array $arguments, array $options): int if ($batch === '') { $batch = $runner->getLastBatch() - 1; } elseif (! ctype_digit($batch)) { - CLI::error('Invalid batch number: ' . $batch, 'light_gray', 'red'); + $output->error('Invalid batch number: ' . $batch, 'light_gray', 'red'); return EXIT_ERROR; } else { $batch = (int) $batch; } - CLI::write(lang('Migrations.rollingBack') . ' ' . $batch, 'yellow'); + $output->write(lang('Migrations.rollingBack') . ' ' . $batch, 'yellow'); - $exit = $this->withSignalsBlocked(static function () use ($runner, $batch): int { + $exit = $this->withSignalsBlocked(static function () use ($runner, $batch, $output): int { if (! $runner->regress($batch)) { - CLI::error(lang('Migrations.generalFault'), 'light_gray', 'red'); + $output->error(lang('Migrations.generalFault'), 'light_gray', 'red'); return EXIT_ERROR; } @@ -107,10 +107,10 @@ protected function execute(array $arguments, array $options): int $messages = $runner->getCliMessages(); foreach ($messages as $message) { - CLI::write($message); + $output->write($message); } - CLI::write('Done rolling back migrations.', 'green'); + $output->write('Done rolling back migrations.', 'green'); return EXIT_SUCCESS; } catch (Throwable $e) { diff --git a/system/Commands/Database/MigrateStatus.php b/system/Commands/Database/MigrateStatus.php index d1f6054d6bd8..068c64ed2928 100644 --- a/system/Commands/Database/MigrateStatus.php +++ b/system/Commands/Database/MigrateStatus.php @@ -15,8 +15,8 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; +use CodeIgniter\CLI\Table; use CodeIgniter\Database\MigrationRunner; /** @@ -118,22 +118,24 @@ protected function execute(array $arguments, array $options): int } } + $output = $this->getOutput(); + if ($status === []) { - CLI::error(lang('Migrations.noneFound'), 'light_gray', 'red'); + $output->error(lang('Migrations.noneFound'), 'light_gray', 'red'); return EXIT_ERROR; } $headers = [ - CLI::color(lang('Migrations.namespace'), 'yellow'), - CLI::color(lang('Migrations.version'), 'yellow'), - CLI::color(lang('Migrations.filename'), 'yellow'), - CLI::color(lang('Migrations.group'), 'yellow'), - CLI::color(str_replace(': ', '', lang('Migrations.on')), 'yellow'), - CLI::color(lang('Migrations.batch'), 'yellow'), + $output->color(lang('Migrations.namespace'), 'yellow'), + $output->color(lang('Migrations.version'), 'yellow'), + $output->color(lang('Migrations.filename'), 'yellow'), + $output->color(lang('Migrations.group'), 'yellow'), + $output->color(str_replace(': ', '', lang('Migrations.on')), 'yellow'), + $output->color(lang('Migrations.batch'), 'yellow'), ]; - CLI::table($status, $headers); + $output->write((new Table($status, $headers))->render()); return EXIT_SUCCESS; } diff --git a/system/Commands/Database/Seed.php b/system/Commands/Database/Seed.php index 5b102dfb284e..9e1351c5b341 100644 --- a/system/Commands/Database/Seed.php +++ b/system/Commands/Database/Seed.php @@ -15,7 +15,6 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Argument; use CodeIgniter\Database\Seeder; use Config\Database; @@ -43,7 +42,7 @@ protected function configure(): void protected function interact(array &$arguments, array &$options): void { if ($arguments === []) { - $arguments[] = CLI::prompt(lang('Migrations.migSeeder'), null, 'required'); + $arguments[] = $this->getPrompt()->ask(lang('Migrations.migSeeder'), null, 'required'); } } diff --git a/system/Commands/Database/ShowTableInfo.php b/system/Commands/Database/ShowTableInfo.php index f718b26b72a9..835048c83737 100644 --- a/system/Commands/Database/ShowTableInfo.php +++ b/system/Commands/Database/ShowTableInfo.php @@ -15,9 +15,10 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Argument; use CodeIgniter\CLI\Input\Option; +use CodeIgniter\CLI\Output; +use CodeIgniter\CLI\Table; use CodeIgniter\Database\BaseConnection; use CodeIgniter\Database\TableName; use CodeIgniter\Exceptions\InvalidArgumentException; @@ -115,30 +116,32 @@ protected function interact(array &$arguments, array &$options): void protected function execute(array $arguments, array $options): int { + $output = $this->getOutput(); + try { $this->db = Database::connect($this->resolveDbGroup($options['dbgroup'])); } catch (InvalidArgumentException $e) { - CLI::error($e->getMessage()); + $output->error($e->getMessage()); return EXIT_ERROR; } $this->dbPrefix = $this->db->getPrefix(); - $this->showDbConfig(); + $this->showDbConfig($output); $tables = $this->db->listTables(); $this->sortOrder = $options['desc'] === true ? 'DESC' : 'ASC'; if ($tables === false || $tables === []) { - CLI::error('Database has no tables!', 'light_gray', 'red'); + $output->error('Database has no tables!', 'light_gray', 'red'); return EXIT_ERROR; } if ($options['show'] === true) { - $this->showAllTables($tables); + $this->showAllTables($output, $tables); return EXIT_SUCCESS; } @@ -147,7 +150,7 @@ protected function execute(array $arguments, array $options): int assert(is_string($tableName)); if (! in_array($tableName, $tables, true)) { - CLI::error( + $output->error( $tableName === '' ? 'No table name was specified.' : sprintf('Table "%s" was not found in the database.', $tableName), @@ -159,7 +162,7 @@ protected function execute(array $arguments, array $options): int } if ($options['metadata'] === true) { - $this->showFieldMetaData($tableName); + $this->showFieldMetaData($output, $tableName); return EXIT_SUCCESS; } @@ -168,7 +171,7 @@ protected function execute(array $arguments, array $options): int $limitFieldValue = $options['limit-field-value']; assert(is_string($limitRows) && is_string($limitFieldValue)); - $this->showDataOfTable($tableName, (int) $limitRows, (int) $limitFieldValue); + $this->showDataOfTable($output, $tableName, (int) $limitRows, (int) $limitFieldValue); return EXIT_SUCCESS; } @@ -178,16 +181,16 @@ private function resolveDbGroup(mixed $group): ?string return is_string($group) && $group !== '' ? $group : null; } - private function showDbConfig(): void + private function showDbConfig(Output $output): void { - CLI::table([[ + $output->write((new Table([[ $this->db->hostname, $this->db->getDatabase(), $this->db->username, $this->db->getPlatform(), $this->dbPrefix, $this->db->port, - ]], ['Hostname', 'Database', 'Username', 'DB Driver', 'DB Prefix', 'Port']); + ]], ['Hostname', 'Database', 'Username', 'DB Driver', 'DB Prefix', 'Port']))->render()); } private function removeDbPrefix(): void @@ -200,10 +203,10 @@ private function restoreDbPrefix(): void $this->db->setPrefix($this->dbPrefix); } - private function showDataOfTable(string $tableName, int $limitRows, int $limitFieldValue): void + private function showDataOfTable(Output $output, string $tableName, int $limitRows, int $limitFieldValue): void { - CLI::write(sprintf('Data of "%s" table:', $tableName), 'black', 'yellow'); - CLI::newLine(); + $output->write(sprintf('Data of "%s" table:', $tableName), 'black', 'yellow'); + $output->newLine(); $this->removeDbPrefix(); @@ -240,16 +243,16 @@ private function showDataOfTable(string $tableName, int $limitRows, int $limitFi $tbody = array_reverse($tbody); } - CLI::table($tbody, $thead); + $output->write((new Table($tbody, $thead))->render()); } /** * @param list $tables */ - private function showAllTables(array $tables): void + private function showAllTables(Output $output, array $tables): void { - CLI::write('The following is a list of the names of all database tables:', 'black', 'yellow'); - CLI::newLine(); + $output->write('The following is a list of the names of all database tables:', 'black', 'yellow'); + $output->newLine(); $this->removeDbPrefix(); @@ -268,13 +271,13 @@ private function showAllTables(array $tables): void $thead = ['Id', 'Table Name', 'Num of Rows', 'Num of Fields']; - CLI::table($this->sortOrder === 'DESC' ? array_reverse($tbody) : $tbody, $thead); + $output->write((new Table($this->sortOrder === 'DESC' ? array_reverse($tbody) : $tbody, $thead))->render()); } - private function showFieldMetaData(string $tableName): void + private function showFieldMetaData(Output $output, string $tableName): void { - CLI::write(sprintf('List of metadata information in "%s" table:', $tableName), 'black', 'yellow'); - CLI::newLine(); + $output->write(sprintf('List of metadata information in "%s" table:', $tableName), 'black', 'yellow'); + $output->newLine(); $thead = ['Field Name', 'Type', 'Max Length', 'Nullable?', 'Default', 'Primary Key?']; @@ -289,19 +292,19 @@ private function showFieldMetaData(string $tableName): void $row->name, $row->type, $row->max_length, - isset($row->nullable) ? $this->setYesOrNo($row->nullable) : 'n/a', + isset($row->nullable) ? $this->setYesOrNo($output, $row->nullable) : 'n/a', $row->default, - isset($row->primary_key) ? $this->setYesOrNo($row->primary_key) : 'n/a', + isset($row->primary_key) ? $this->setYesOrNo($output, $row->primary_key) : 'n/a', ]; } - CLI::table($this->sortOrder === 'DESC' ? array_reverse($tbody) : $tbody, $thead); + $output->write((new Table($this->sortOrder === 'DESC' ? array_reverse($tbody) : $tbody, $thead))->render()); } - private function setYesOrNo(mixed $fieldValue): string + private function setYesOrNo(Output $output, mixed $fieldValue): string { return filter_var($fieldValue, FILTER_VALIDATE_BOOL) - ? CLI::color('Yes', 'green') - : CLI::color('No', 'red'); + ? $output->color('Yes', 'green') + : $output->color('No', 'red'); } }