Skip to content

Commit 62a2ebe

Browse files
authored
refactor: write the top-level and Encryption commands through $this->getOutput() (#10688)
1 parent ebd25ec commit 62a2ebe

4 files changed

Lines changed: 62 additions & 61 deletions

File tree

‎system/Commands/Encryption/GenerateKey.php‎

Lines changed: 10 additions & 11 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\Config\DotEnv;
2120
use CodeIgniter\Encryption\Encryption;
@@ -81,25 +80,26 @@ protected function interact(array &$arguments, array &$options): void
8180
return;
8281
}
8382

84-
if (CLI::prompt('Overwrite existing key?', ['n', 'y']) === 'y') {
83+
if ($this->getPrompt()->ask('Overwrite existing key?', ['n', 'y']) === 'y') {
8584
$options['force'] = null; // simulate the presence of the --force option
8685
}
8786
}
8887

8988
protected function execute(array $arguments, array $options): int
9089
{
9190
$prefix = $options['prefix'];
91+
$output = $this->getOutput();
9292

9393
if (! in_array($prefix, self::VALID_PREFIXES, true)) {
94-
CLI::error(sprintf('Invalid prefix "%s". Use either "hex2bin" or "base64".', $prefix));
94+
$output->error(sprintf('Invalid prefix "%s". Use either "hex2bin" or "base64".', $prefix));
9595

9696
return EXIT_ERROR;
9797
}
9898

9999
$encodedKey = $this->generateRandomKey($prefix, (int) $options['length']);
100100

101101
if ($options['show'] === true) {
102-
CLI::write($encodedKey, 'yellow');
102+
$output->write($encodedKey, 'yellow');
103103

104104
return EXIT_SUCCESS;
105105
}
@@ -108,12 +108,12 @@ protected function execute(array $arguments, array $options): int
108108

109109
if ($currentKey !== '' && $options['force'] === false) {
110110
if ($this->isInteractive()) {
111-
CLI::write('Setting new encryption key cancelled.', 'yellow');
111+
$output->write('Setting new encryption key cancelled.', 'yellow');
112112

113113
return EXIT_SUCCESS;
114114
}
115115

116-
CLI::error('Setting new encryption key aborted: pass --force to overwrite the existing key in non-interactive mode.');
116+
$output->error('Setting new encryption key aborted: pass --force to overwrite the existing key in non-interactive mode.');
117117

118118
return EXIT_ERROR;
119119
}
@@ -122,14 +122,14 @@ protected function execute(array $arguments, array $options): int
122122
$baseEnv = ROOTPATH . 'env';
123123

124124
if (! is_file($envFile) && ! is_file($baseEnv)) {
125-
CLI::write('Both default shipped `env` file and custom `.env` are missing.', 'yellow');
126-
CLI::write(sprintf('Here\'s your new key instead: %s', CLI::color($encodedKey, 'yellow')));
125+
$output->write('Both default shipped `env` file and custom `.env` are missing.', 'yellow');
126+
$output->write(sprintf('Here\'s your new key instead: %s', $output->color($encodedKey, 'yellow')));
127127

128128
return EXIT_ERROR;
129129
}
130130

131131
if (! $this->writeNewEncryptionKeyToFile($encodedKey, $envFile, $baseEnv)) {
132-
CLI::error(sprintf('Failed to write new encryption key to %s.', clean_path($envFile)));
132+
$output->error(sprintf('Failed to write new encryption key to %s.', clean_path($envFile)));
133133

134134
return EXIT_ERROR;
135135
}
@@ -141,8 +141,7 @@ protected function execute(array $arguments, array $options): int
141141
$dotenv = new DotEnv((new Paths())->envDirectory ?? ROOTPATH); // @phpstan-ignore nullCoalesce.property
142142
$dotenv->load();
143143

144-
CLI::write(sprintf('New encryption key written to %s.', clean_path($envFile)), 'green');
145-
CLI::newLine();
144+
$output->write(sprintf('New encryption key written to %s.', clean_path($envFile)), 'green');
146145

147146
return EXIT_SUCCESS;
148147
}

‎system/Commands/Encryption/RotateKey.php‎

Lines changed: 13 additions & 13 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 Config\Paths;
2120

@@ -81,53 +80,54 @@ protected function interact(array &$arguments, array &$options): void
8180
return;
8281
}
8382

84-
if (CLI::prompt('Rotate encryption key? The current key will be moved to `previousKeys`.', ['n', 'y']) === 'y') {
83+
if ($this->getPrompt()->ask('Rotate encryption key? The current key will be moved to `previousKeys`.', ['n', 'y']) === 'y') {
8584
$options['force'] = null; // simulate the presence of the --force option
8685
}
8786
}
8887

8988
protected function execute(array $arguments, array $options): int
9089
{
9190
$prefix = $options['prefix'];
91+
$output = $this->getOutput();
9292

9393
if (! in_array($prefix, self::VALID_PREFIXES, true)) {
94-
CLI::error(sprintf('Invalid prefix "%s". Use either "hex2bin" or "base64".', $prefix));
94+
$output->error(sprintf('Invalid prefix "%s". Use either "hex2bin" or "base64".', $prefix));
9595

9696
return EXIT_ERROR;
9797
}
9898

9999
$currentKey = env('encryption.key', '');
100100

101101
if ($currentKey === '') {
102-
CLI::error('No existing `encryption.key` to rotate. Run `spark key:generate` first.');
102+
$output->error('No existing `encryption.key` to rotate. Run `spark key:generate` first.');
103103

104104
return EXIT_ERROR;
105105
}
106106

107107
if ($options['force'] === false) {
108108
if ($this->isInteractive()) {
109-
CLI::write('Key rotation cancelled.', 'yellow');
109+
$output->write('Key rotation cancelled.', 'yellow');
110110

111111
return EXIT_SUCCESS;
112112
}
113113

114-
CLI::error('Key rotation aborted: pass --force to rotate the encryption key in non-interactive mode.');
114+
$output->error('Key rotation aborted: pass --force to rotate the encryption key in non-interactive mode.');
115115

116116
return EXIT_ERROR;
117117
}
118118

119119
$keep = $options['keep'];
120120

121121
if (! is_string($keep) || ! ctype_digit($keep)) {
122-
CLI::error('The --keep option must be a non-negative integer.');
122+
$output->error('The --keep option must be a non-negative integer.');
123123

124124
return EXIT_ERROR;
125125
}
126126

127127
$length = $options['length'];
128128

129129
if (! is_string($length) || ! ctype_digit($length) || (int) $length < 1) {
130-
CLI::error('The --length option must be a positive integer.');
130+
$output->error('The --length option must be a positive integer.');
131131

132132
return EXIT_ERROR;
133133
}
@@ -140,20 +140,20 @@ protected function execute(array $arguments, array $options): int
140140
$envFile = ((new Paths())->envDirectory ?? ROOTPATH) . '.env'; // @phpstan-ignore nullCoalesce.property
141141

142142
if (! is_file($envFile)) {
143-
CLI::error(sprintf('Cannot rotate: `.env` file not found at %s.', clean_path($envFile)));
143+
$output->error(sprintf('Cannot rotate: `.env` file not found at %s.', clean_path($envFile)));
144144

145145
return EXIT_ERROR;
146146
}
147147

148148
if (! is_writable($envFile)) {
149-
CLI::error(sprintf('Cannot rotate: `.env` file at %s is not writable.', clean_path($envFile)));
149+
$output->error(sprintf('Cannot rotate: `.env` file at %s is not writable.', clean_path($envFile)));
150150

151151
return EXIT_ERROR;
152152
}
153153

154154
if (! $this->writePreviousKeys($previousKeys, $envFile)) {
155155
// @codeCoverageIgnoreStart
156-
CLI::error(sprintf('Failed to write `encryption.previousKeys` to %s.', clean_path($envFile)));
156+
$output->error(sprintf('Failed to write `encryption.previousKeys` to %s.', clean_path($envFile)));
157157

158158
return EXIT_ERROR;
159159
// @codeCoverageIgnoreEnd
@@ -178,12 +178,12 @@ protected function execute(array $arguments, array $options): int
178178

179179
$count = count($previousKeys);
180180

181-
CLI::write(sprintf(
181+
$output->write(sprintf(
182182
'Encryption key rotated. %d %s retained for decryption fallback.',
183183
$count,
184184
$count === 1 ? 'previous key' : 'previous keys',
185185
), 'green');
186-
CLI::write('Re-encrypt existing data with the new key when ready.', 'yellow');
186+
$output->write('Re-encrypt existing data with the new key when ready.', 'yellow');
187187

188188
return EXIT_SUCCESS;
189189
}

‎system/Commands/Help.php‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
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;
20+
use CodeIgniter\CLI\Output;
2121

2222
/**
2323
* Displays the basic usage information for a given command.
@@ -51,57 +51,57 @@ protected function execute(array $arguments, array $options): int
5151
return EXIT_ERROR;
5252
}
5353

54-
$this->describeHelp($commands->getCommand($command, legacy: false));
54+
$this->describeHelp($commands->getCommand($command, legacy: false), $this->getOutput());
5555

5656
return EXIT_SUCCESS;
5757
}
5858

59-
private function describeHelp(AbstractCommand $command): void
59+
private function describeHelp(AbstractCommand $command, Output $output): void
6060
{
61-
CLI::write(lang('CLI.helpUsage'), 'yellow');
61+
$output->write(lang('CLI.helpUsage'), 'yellow');
6262

6363
foreach ($command->getUsages() as $usage) {
64-
CLI::write($this->addPadding($usage));
64+
$output->write($this->addPadding($usage));
6565
}
6666

6767
if ($command->getDescription() !== '') {
68-
CLI::newLine();
69-
CLI::write(lang('CLI.helpDescription'), 'yellow');
70-
CLI::write($this->addPadding($command->getDescription()));
68+
$output->newLine();
69+
$output->write(lang('CLI.helpDescription'), 'yellow');
70+
$output->write($this->addPadding($command->getDescription()));
7171
}
7272

7373
if ($command->getAliases() !== []) {
74-
CLI::newLine();
75-
CLI::write(lang('CLI.helpAliases'), 'yellow');
74+
$output->newLine();
75+
$output->write(lang('CLI.helpAliases'), 'yellow');
7676

7777
foreach ($command->getAliases() as $alias) {
78-
CLI::write($this->addPadding($alias));
78+
$output->write($this->addPadding($alias));
7979
}
8080
}
8181

8282
$maxPadding = $this->getMaxPadding($command);
8383

8484
if ($command->getArgumentsDefinition() !== []) {
85-
CLI::newLine();
86-
CLI::write(lang('CLI.helpArguments'), 'yellow');
85+
$output->newLine();
86+
$output->write(lang('CLI.helpArguments'), 'yellow');
8787

8888
foreach ($command->getArgumentsDefinition() as $argument => $definition) {
8989
$default = ! $definition->required && $this->shouldShowDefault($definition->default)
9090
? sprintf(' [default: %s]', $this->formatDefaultValue($definition->default))
9191
: '';
9292

93-
CLI::write(sprintf(
93+
$output->write(sprintf(
9494
'%s%s%s',
95-
CLI::color($this->addPadding($argument, 2, $maxPadding), 'green'),
95+
$output->color($this->addPadding($argument, 2, $maxPadding), 'green'),
9696
$definition->description,
97-
CLI::color($default, 'yellow'),
97+
$output->color($default, 'yellow'),
9898
));
9999
}
100100
}
101101

102102
if ($command->getOptionsDefinition() !== []) {
103-
CLI::newLine();
104-
CLI::write(lang('CLI.helpOptions'), 'yellow');
103+
$output->newLine();
104+
$output->write(lang('CLI.helpOptions'), 'yellow');
105105

106106
$hasShortcuts = $command->getShortcuts() !== [];
107107

@@ -130,12 +130,12 @@ private function describeHelp(AbstractCommand $command): void
130130
? sprintf(' [default: %s]', $this->formatDefaultValue($definition->default))
131131
: '';
132132

133-
CLI::write(sprintf(
133+
$output->write(sprintf(
134134
'%s%s%s%s',
135-
CLI::color($this->addPadding($optionString, 2, $maxPadding), 'green'),
135+
$output->color($this->addPadding($optionString, 2, $maxPadding), 'green'),
136136
$definition->description,
137-
CLI::color($default, 'yellow'),
138-
$definition->isArray ? CLI::color(' (multiple values allowed)', 'yellow') : '',
137+
$output->color($default, 'yellow'),
138+
$definition->isArray ? $output->color(' (multiple values allowed)', 'yellow') : '',
139139
));
140140
}
141141
}

‎system/Commands/ListCommands.php‎

Lines changed: 17 additions & 15 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\Output;
2020

2121
/**
2222
* Lists the available commands.
@@ -34,14 +34,16 @@ protected function configure(): void
3434

3535
protected function execute(array $arguments, array $options): int
3636
{
37+
$output = $this->getOutput();
38+
3739
if ($options['simple'] === true) {
38-
return $this->describeCommandsSimple();
40+
return $this->describeCommandsSimple($output);
3941
}
4042

41-
return $this->describeCommandsDetailed();
43+
return $this->describeCommandsDetailed($output);
4244
}
4345

44-
private function describeCommandsSimple(): int
46+
private function describeCommandsSimple(Output $output): int
4547
{
4648
// Legacy takes precedence on key collision so the listing reflects the
4749
// command that would actually be invoked.
@@ -53,16 +55,16 @@ private function describeCommandsSimple(): int
5355
sort($commands);
5456

5557
foreach ($commands as $command) {
56-
CLI::write($command);
58+
$output->write($command);
5759
}
5860

5961
return EXIT_SUCCESS;
6062
}
6163

62-
private function describeCommandsDetailed(): int
64+
private function describeCommandsDetailed(Output $output): int
6365
{
64-
CLI::write(lang('CLI.helpUsage'), 'yellow');
65-
CLI::write($this->addPadding('command [options] [--] [arguments]'));
66+
$output->write(lang('CLI.helpUsage'), 'yellow');
67+
$output->write($this->addPadding('command [options] [--] [arguments]'));
6668

6769
$entries = [];
6870
$maxPad = 0;
@@ -110,23 +112,23 @@ private function describeCommandsDetailed(): int
110112
$groups[$group][] = [$command, $description];
111113
}
112114

113-
CLI::newLine();
114-
CLI::write(lang('CLI.helpAvailableCommands'), 'yellow');
115+
$output->newLine();
116+
$output->write(lang('CLI.helpAvailableCommands'), 'yellow');
115117

116118
$firstGroup = array_key_first($groups);
117119

118120
foreach ($groups as $group => $commands) {
119121
if ($group !== $firstGroup) {
120-
CLI::newLine();
122+
$output->newLine();
121123
}
122124

123-
CLI::write($group, 'yellow');
125+
$output->write($group, 'yellow');
124126

125127
foreach ($commands as $command) {
126-
CLI::write(sprintf(
128+
$output->write(sprintf(
127129
'%s%s',
128-
CLI::color($this->addPadding($command[0], 2, $maxPad), 'green'),
129-
CLI::wrap($command[1], 0, $maxPad),
130+
$output->color($this->addPadding($command[0], 2, $maxPad), 'green'),
131+
$output->wrap($command[1], 0, $maxPad),
130132
));
131133
}
132134
}

0 commit comments

Comments
 (0)