Skip to content

Commit e4f6a02

Browse files
authored
refactor: write the Database commands through $this->getOutput() (#10689)
1 parent 62a2ebe commit e4f6a02

7 files changed

Lines changed: 67 additions & 62 deletions

File tree

‎system/Commands/Database/CreateDatabase.php‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Argument;
2019
use CodeIgniter\CLI\Input\Option;
2120
use CodeIgniter\Config\Factories;
@@ -53,7 +52,7 @@ protected function configure(): void
5352
protected function interact(array &$arguments, array &$options): void
5453
{
5554
if ($arguments === []) {
56-
$arguments[] = CLI::prompt('Database name', null, 'required');
55+
$arguments[] = $this->getPrompt()->ask('Database name', null, 'required');
5756
}
5857

5958
$ext = $this->getUnboundOption('ext', $options);
@@ -71,6 +70,8 @@ protected function execute(array $arguments, array $options): int
7170
$name = $arguments['db_name'];
7271
assert(is_string($name));
7372

73+
$output = $this->getOutput();
74+
7475
try {
7576
$config = config(Database::class);
7677

@@ -86,7 +87,7 @@ protected function execute(array $arguments, array $options): int
8687
assert(is_string($ext));
8788

8889
if (! in_array($ext, self::VALID_EXTENSIONS, true)) {
89-
CLI::error(sprintf('Invalid file extension "%s". Use either `db` or `sqlite`.', $ext), 'light_gray', 'red');
90+
$output->error(sprintf('Invalid file extension "%s". Use either `db` or `sqlite`.', $ext), 'light_gray', 'red');
9091

9192
return EXIT_ERROR;
9293
}
@@ -102,7 +103,7 @@ protected function execute(array $arguments, array $options): int
102103
$dbName = str_contains($name, DIRECTORY_SEPARATOR) ? $name : WRITEPATH . $name;
103104

104105
if (is_file($dbName)) {
105-
CLI::error("Database \"{$dbName}\" already exists.", 'light_gray', 'red');
106+
$output->error("Database \"{$dbName}\" already exists.", 'light_gray', 'red');
106107

107108
return EXIT_ERROR;
108109
}
@@ -116,18 +117,18 @@ protected function execute(array $arguments, array $options): int
116117

117118
if (! is_file($db->getDatabase()) && $name !== ':memory:') {
118119
// @codeCoverageIgnoreStart
119-
CLI::error('Database creation failed.', 'light_gray', 'red');
120+
$output->error('Database creation failed.', 'light_gray', 'red');
120121

121122
return EXIT_ERROR;
122123
// @codeCoverageIgnoreEnd
123124
}
124125
} elseif (! Database::forge()->createDatabase($name)) {
125-
CLI::error('Database creation failed.', 'light_gray', 'red');
126+
$output->error('Database creation failed.', 'light_gray', 'red');
126127

127128
return EXIT_ERROR;
128129
}
129130

130-
CLI::write("Database \"{$name}\" successfully created.", 'green');
131+
$output->write("Database \"{$name}\" successfully created.", 'green');
131132

132133
return EXIT_SUCCESS;
133134
} catch (Throwable $e) {

‎system/Commands/Database/Migrate.php‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Option;
2019
use CodeIgniter\CLI\SignalTrait;
2120
use CodeIgniter\Database\MigrationRunner;
@@ -62,7 +61,9 @@ protected function execute(array $arguments, array $options): int
6261
$runner = service('migrations');
6362
$runner->clearCliMessages();
6463

65-
CLI::write(lang('Migrations.latest'), 'yellow');
64+
$output = $this->getOutput();
65+
66+
$output->write(lang('Migrations.latest'), 'yellow');
6667

6768
$namespace = $options['namespace'];
6869
assert(is_string($namespace));
@@ -79,19 +80,19 @@ protected function execute(array $arguments, array $options): int
7980
$runner->setNamespace($namespace);
8081
}
8182

82-
$this->withSignalsBlocked(static function () use ($runner, $group): void {
83+
$this->withSignalsBlocked(static function () use ($runner, $group, $output): void {
8384
if (! $runner->latest($group)) {
84-
CLI::error(lang('Migrations.generalFault'), 'light_gray', 'red');
85+
$output->error(lang('Migrations.generalFault'), 'light_gray', 'red');
8586
}
8687
});
8788

8889
$messages = $runner->getCliMessages();
8990

9091
foreach ($messages as $message) {
91-
CLI::write($message);
92+
$output->write($message);
9293
}
9394

94-
CLI::write(lang('Migrations.migrated'), 'green');
95+
$output->write(lang('Migrations.migrated'), 'green');
9596

9697
return EXIT_SUCCESS;
9798
} catch (Throwable $e) {

‎system/Commands/Database/MigrateRefresh.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Option;
2019
use CodeIgniter\CLI\SignalTrait;
2120

@@ -69,7 +68,7 @@ protected function interact(array &$arguments, array &$options): void
6968
return;
7069
}
7170

72-
if (CLI::prompt(lang('Migrations.refreshConfirm'), ['y', 'n']) === 'y') {
71+
if ($this->getPrompt()->ask(lang('Migrations.refreshConfirm'), ['y', 'n']) === 'y') {
7372
$options['force'] = null; // simulate the presence of the --force option
7473
}
7574
}

‎system/Commands/Database/MigrateRollback.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Option;
2019
use CodeIgniter\CLI\SignalTrait;
2120
use CodeIgniter\Database\MigrationRunner;
@@ -60,7 +59,7 @@ protected function interact(array &$arguments, array &$options): void
6059
return;
6160
}
6261

63-
if (CLI::prompt(lang('Migrations.rollBackConfirm'), ['y', 'n']) === 'y') {
62+
if ($this->getPrompt()->ask(lang('Migrations.rollBackConfirm'), ['y', 'n']) === 'y') {
6463
$options['force'] = null; // simulate the presence of the --force option
6564
}
6665
}
@@ -73,6 +72,7 @@ protected function execute(array $arguments, array $options): int
7372

7473
/** @var MigrationRunner $runner */
7574
$runner = service('migrations');
75+
$output = $this->getOutput();
7676

7777
try {
7878
$batch = $options['batch'];
@@ -81,18 +81,18 @@ protected function execute(array $arguments, array $options): int
8181
if ($batch === '') {
8282
$batch = $runner->getLastBatch() - 1;
8383
} elseif (! ctype_digit($batch)) {
84-
CLI::error('Invalid batch number: ' . $batch, 'light_gray', 'red');
84+
$output->error('Invalid batch number: ' . $batch, 'light_gray', 'red');
8585

8686
return EXIT_ERROR;
8787
} else {
8888
$batch = (int) $batch;
8989
}
9090

91-
CLI::write(lang('Migrations.rollingBack') . ' ' . $batch, 'yellow');
91+
$output->write(lang('Migrations.rollingBack') . ' ' . $batch, 'yellow');
9292

93-
$exit = $this->withSignalsBlocked(static function () use ($runner, $batch): int {
93+
$exit = $this->withSignalsBlocked(static function () use ($runner, $batch, $output): int {
9494
if (! $runner->regress($batch)) {
95-
CLI::error(lang('Migrations.generalFault'), 'light_gray', 'red');
95+
$output->error(lang('Migrations.generalFault'), 'light_gray', 'red');
9696

9797
return EXIT_ERROR;
9898
}
@@ -107,10 +107,10 @@ protected function execute(array $arguments, array $options): int
107107
$messages = $runner->getCliMessages();
108108

109109
foreach ($messages as $message) {
110-
CLI::write($message);
110+
$output->write($message);
111111
}
112112

113-
CLI::write('Done rolling back migrations.', 'green');
113+
$output->write('Done rolling back migrations.', 'green');
114114

115115
return EXIT_SUCCESS;
116116
} catch (Throwable $e) {

‎system/Commands/Database/MigrateStatus.php‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Option;
19+
use CodeIgniter\CLI\Table;
2020
use CodeIgniter\Database\MigrationRunner;
2121

2222
/**
@@ -118,22 +118,24 @@ protected function execute(array $arguments, array $options): int
118118
}
119119
}
120120

121+
$output = $this->getOutput();
122+
121123
if ($status === []) {
122-
CLI::error(lang('Migrations.noneFound'), 'light_gray', 'red');
124+
$output->error(lang('Migrations.noneFound'), 'light_gray', 'red');
123125

124126
return EXIT_ERROR;
125127
}
126128

127129
$headers = [
128-
CLI::color(lang('Migrations.namespace'), 'yellow'),
129-
CLI::color(lang('Migrations.version'), 'yellow'),
130-
CLI::color(lang('Migrations.filename'), 'yellow'),
131-
CLI::color(lang('Migrations.group'), 'yellow'),
132-
CLI::color(str_replace(': ', '', lang('Migrations.on')), 'yellow'),
133-
CLI::color(lang('Migrations.batch'), 'yellow'),
130+
$output->color(lang('Migrations.namespace'), 'yellow'),
131+
$output->color(lang('Migrations.version'), 'yellow'),
132+
$output->color(lang('Migrations.filename'), 'yellow'),
133+
$output->color(lang('Migrations.group'), 'yellow'),
134+
$output->color(str_replace(': ', '', lang('Migrations.on')), 'yellow'),
135+
$output->color(lang('Migrations.batch'), 'yellow'),
134136
];
135137

136-
CLI::table($status, $headers);
138+
$output->write((new Table($status, $headers))->render());
137139

138140
return EXIT_SUCCESS;
139141
}

‎system/Commands/Database/Seed.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Argument;
2019
use CodeIgniter\Database\Seeder;
2120
use Config\Database;
@@ -43,7 +42,7 @@ protected function configure(): void
4342
protected function interact(array &$arguments, array &$options): void
4443
{
4544
if ($arguments === []) {
46-
$arguments[] = CLI::prompt(lang('Migrations.migSeeder'), null, 'required');
45+
$arguments[] = $this->getPrompt()->ask(lang('Migrations.migSeeder'), null, 'required');
4746
}
4847
}
4948

0 commit comments

Comments
 (0)