Skip to content

Commit 5bc61cd

Browse files
committed
feat: use select() for the framework prompts that pick from fixed options
1 parent d0e419d commit 5bc61cd

20 files changed

Lines changed: 86 additions & 67 deletions

‎system/CLI/Console.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,13 @@ private function chooseAlternative(array $alternatives): ?string
285285
CLI::error(lang('CLI.commandNotFound', [$this->command]));
286286
CLI::newLine();
287287

288+
$prompt = CLI::getPrompt();
289+
288290
if (count($alternatives) === 1) {
289-
return CLI::prompt(lang('CLI.altCommandRun', [$alternatives[0]]), ['y', 'n']) === 'y' ? $alternatives[0] : null;
291+
return $prompt->ask(lang('CLI.altCommandRun', [$alternatives[0]]), ['y', 'n']) === 'y' ? $alternatives[0] : null;
290292
}
291293

292-
$chosen = (int) CLI::promptByKey(lang('CLI.altCommandSelect'), [...$alternatives, lang('CLI.altCommandNone')]);
294+
$chosen = $prompt->select(lang('CLI.altCommandSelect'), [...$alternatives, lang('CLI.altCommandNone')]);
293295

294296
return $alternatives[$chosen] ?? null;
295297
}

‎system/CLI/Prompt.php‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,12 @@ public function askByMultipleKeys(string $text, array $options): array
166166
/**
167167
* Lets the user pick one option with the arrow keys and returns its key, asking for its number where keys cannot be read.
168168
*
169-
* @param array<array-key, string> $options
170-
* @param int|string|null $default The key highlighted at the start, or `null` for the first one
169+
* @template TKey of array-key
170+
*
171+
* @param array<TKey, string> $options
172+
* @param TKey|null $default The key highlighted at the start, or `null` for the first one
173+
*
174+
* @return TKey
171175
*
172176
* @throws InvalidArgumentException
173177
*/
@@ -209,10 +213,12 @@ public function select(string $question, array $options, int|string|null $defaul
209213
/**
210214
* Lets the user tick options with the arrow keys and the space bar, asking for their numbers where keys cannot be read.
211215
*
212-
* @param array<array-key, string> $options
213-
* @param list<array-key> $default The keys ticked at the start
216+
* @template TKey of array-key
217+
*
218+
* @param array<TKey, string> $options
219+
* @param list<TKey> $default The keys ticked at the start
214220
*
215-
* @return array<array-key, string> The ticked options, in the order of `$options`
221+
* @return array<TKey, string> The ticked options, in the order of `$options`
216222
*
217223
* @throws CLIException When a required answer has no default on an output that cannot ask
218224
* @throws InvalidArgumentException

‎system/Commands/Database/CreateDatabase.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ protected function interact(array &$arguments, array &$options): void
5959
$ext = $this->getUnboundOption('ext', $options);
6060

6161
if (is_string($ext) && ! in_array($ext, self::VALID_EXTENSIONS, true)) {
62-
$options['ext'] = CLI::prompt('Please choose a valid file extension', self::VALID_EXTENSIONS, 'required');
62+
$options['ext'] = $this->getPrompt()->select(
63+
'Please choose a valid file extension',
64+
array_combine(self::VALID_EXTENSIONS, self::VALID_EXTENSIONS),
65+
);
6366
}
6467
}
6568

‎system/Commands/Database/ShowTableInfo.php‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,8 @@ protected function interact(array &$arguments, array &$options): void
108108
return;
109109
}
110110

111-
while (! in_array($arguments[0] ?? '', $tables, true)) {
112-
$tableKey = CLI::promptByKey(
113-
['Here is the list of your database tables:', 'Which table do you want to see?'],
114-
$tables,
115-
'required',
116-
);
117-
CLI::newLine();
118-
119-
$arguments[0] = $tables[$tableKey] ?? '';
111+
if (! in_array($arguments[0] ?? '', $tables, true)) {
112+
$arguments[0] = $tables[$this->getPrompt()->select('Which table do you want to see?', $tables)];
120113
}
121114
}
122115

‎system/Commands/Encryption/GenerateKey.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ protected function interact(array &$arguments, array &$options): void
6363
$prefix = $this->getUnboundOption('prefix', $options);
6464

6565
if (is_string($prefix) && ! in_array($prefix, self::VALID_PREFIXES, true)) {
66-
$options['prefix'] = CLI::prompt('Please provide a valid prefix to use.', self::VALID_PREFIXES, 'required');
66+
$options['prefix'] = $this->getPrompt()->select(
67+
'Please provide a valid prefix to use.',
68+
array_combine(self::VALID_PREFIXES, self::VALID_PREFIXES),
69+
);
6770
}
6871

6972
if ($this->hasUnboundOption('show', $options)) {

‎system/Commands/Encryption/RotateKey.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ protected function interact(array &$arguments, array &$options): void
6767
$prefix = $this->getUnboundOption('prefix', $options);
6868

6969
if (is_string($prefix) && ! in_array($prefix, self::VALID_PREFIXES, true)) {
70-
$options['prefix'] = CLI::prompt('Please provide a valid prefix to use.', self::VALID_PREFIXES, 'required');
70+
$options['prefix'] = $this->getPrompt()->select(
71+
'Please provide a valid prefix to use.',
72+
array_combine(self::VALID_PREFIXES, self::VALID_PREFIXES),
73+
);
7174
}
7275

7376
if ($this->hasUnboundOption('force', $options)) {

‎system/Commands/Generators/CommandGenerator.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ protected function interact(array &$arguments, array &$options): void
6464
return;
6565
}
6666

67-
$options['type'] = CLI::prompt(lang('CLI.generator.commandType'), ['basic', 'generator'], 'required');
67+
$options['type'] = $this->getPrompt()->select(
68+
lang('CLI.generator.commandType'),
69+
['basic' => 'basic', 'generator' => 'generator'],
70+
);
6871
}
6972

7073
protected function execute(array $arguments, array $options): int

‎system/Commands/Generators/ControllerGenerator.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ protected function interact(array &$arguments, array &$options): void
5858
return;
5959
}
6060

61-
$options['restful'] = CLI::prompt(lang('CLI.generator.parentClass'), ['controller', 'presenter'], 'required');
61+
$options['restful'] = $this->getPrompt()->select(
62+
lang('CLI.generator.parentClass'),
63+
['controller' => 'controller', 'presenter' => 'presenter'],
64+
);
6265
}
6366

6467
protected function execute(array $arguments, array $options): int

‎system/Commands/Generators/ModelGenerator.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ protected function interact(array &$arguments, array &$options): void
6767
return;
6868
}
6969

70-
$options['return'] = CLI::prompt(lang('CLI.generator.returnType'), self::RETURN_TYPES, 'required');
70+
$options['return'] = $this->getPrompt()->select(
71+
lang('CLI.generator.returnType'),
72+
array_combine(self::RETURN_TYPES, self::RETURN_TYPES),
73+
);
7174
}
7275

7376
protected function execute(array $arguments, array $options): int

‎tests/system/CLI/ConsoleTest.php‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public function testUnknownCommandDeclinedSuggestionExitsWithError(): void
270270
public function testUnknownCommandRunsSelectedSuggestion(): void
271271
{
272272
$this->initializeConsole('clear', '--no-header');
273-
$io = $this->useInputs('0');
273+
$io = $this->useInputs('1');
274274

275275
$console = new Console();
276276
$exitCode = $console->run();
@@ -283,12 +283,11 @@ public function testUnknownCommandRunsSelectedSuggestion(): void
283283
Command "clear" not found.
284284
285285
Select a command to run instead:
286-
[0] cache:clear
287-
[1] debugbar:clear
288-
[2] logs:clear
289-
[3] none of these
290-
291-
[0, 1, 2, 3]: 0
286+
[1] cache:clear
287+
[2] debugbar:clear
288+
[3] logs:clear
289+
[4] none of these
290+
Number [1]: 1
292291
293292
Cache cleared using the "file" driver.
294293

@@ -300,7 +299,7 @@ public function testUnknownCommandRunsSelectedSuggestion(): void
300299
public function testUnknownCommandSelectingNoneExitsWithError(): void
301300
{
302301
$this->initializeConsole('clear', '--no-header');
303-
$io = $this->useInputs('3');
302+
$io = $this->useInputs('4');
304303

305304
$console = new Console();
306305
$exitCode = $console->run();
@@ -313,12 +312,11 @@ public function testUnknownCommandSelectingNoneExitsWithError(): void
313312
Command "clear" not found.
314313
315314
Select a command to run instead:
316-
[0] cache:clear
317-
[1] debugbar:clear
318-
[2] logs:clear
319-
[3] none of these
320-
321-
[0, 1, 2, 3]: 3
315+
[1] cache:clear
316+
[2] debugbar:clear
317+
[3] logs:clear
318+
[4] none of these
319+
Number [1]: 4
322320

323321
EOT,
324322
$this->getUndecoratedIoOutput($io),

0 commit comments

Comments
 (0)