|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Statamic\Cli\Concerns; |
| 4 | + |
| 5 | +use Laravel\Prompts\ConfirmPrompt; |
| 6 | +use Laravel\Prompts\Prompt; |
| 7 | +use Laravel\Prompts\SelectPrompt; |
| 8 | +use Laravel\Prompts\SuggestPrompt; |
| 9 | +use Laravel\Prompts\TextPrompt; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use Symfony\Component\Console\Question\Question; |
| 13 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 14 | + |
| 15 | +trait ConfiguresPrompts |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Configure the prompt fallbacks. |
| 19 | + * |
| 20 | + * @return void |
| 21 | + */ |
| 22 | + protected function configurePrompts(InputInterface $input, OutputInterface $output) |
| 23 | + { |
| 24 | + Prompt::fallbackWhen(! $input->isInteractive() || PHP_OS_FAMILY === 'Windows'); |
| 25 | + |
| 26 | + TextPrompt::fallbackUsing(fn (TextPrompt $prompt) => $this->promptUntilValid( |
| 27 | + fn () => (new SymfonyStyle($input, $output))->ask($prompt->label, $prompt->default ?: null) ?? '', |
| 28 | + $prompt->required, |
| 29 | + $prompt->validate, |
| 30 | + $output |
| 31 | + )); |
| 32 | + |
| 33 | + ConfirmPrompt::fallbackUsing(fn (ConfirmPrompt $prompt) => $this->promptUntilValid( |
| 34 | + fn () => (new SymfonyStyle($input, $output))->confirm($prompt->label, $prompt->default), |
| 35 | + $prompt->required, |
| 36 | + $prompt->validate, |
| 37 | + $output |
| 38 | + )); |
| 39 | + |
| 40 | + SelectPrompt::fallbackUsing(fn (SelectPrompt $prompt) => $this->promptUntilValid( |
| 41 | + fn () => (new SymfonyStyle($input, $output))->choice($prompt->label, $prompt->options, $prompt->default), |
| 42 | + false, |
| 43 | + $prompt->validate, |
| 44 | + $output |
| 45 | + )); |
| 46 | + |
| 47 | + SuggestPrompt::fallbackUsing(fn (SuggestPrompt $prompt) => $this->promptUntilValid( |
| 48 | + function () use ($prompt, $input, $output) { |
| 49 | + $question = new Question($prompt->label, $prompt->default); |
| 50 | + |
| 51 | + is_callable($prompt->options) |
| 52 | + ? $question->setAutocompleterCallback($prompt->options) |
| 53 | + : $question->setAutocompleterValues($prompt->options); |
| 54 | + |
| 55 | + return (new SymfonyStyle($input, $output))->askQuestion($question); |
| 56 | + }, |
| 57 | + $prompt->required, |
| 58 | + $prompt->validate, |
| 59 | + $output |
| 60 | + )); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Prompt the user until the given validation callback passes. |
| 65 | + * |
| 66 | + * @param \Closure $prompt |
| 67 | + * @param bool|string $required |
| 68 | + * @param \Closure|null $validate |
| 69 | + * @param \Symfony\Component\Console\Output\OutputInterface $output |
| 70 | + * @return mixed |
| 71 | + */ |
| 72 | + protected function promptUntilValid($prompt, $required, $validate, $output) |
| 73 | + { |
| 74 | + while (true) { |
| 75 | + $result = $prompt(); |
| 76 | + |
| 77 | + if ($required && ($result === '' || $result === [] || $result === false)) { |
| 78 | + $output->writeln('<error>'.(is_string($required) ? $required : 'Required.').'</error>'); |
| 79 | + |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + if ($validate) { |
| 84 | + $error = $validate($result); |
| 85 | + |
| 86 | + if (is_string($error) && strlen($error) > 0) { |
| 87 | + $output->writeln("<error>{$error}</error>"); |
| 88 | + |
| 89 | + continue; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return $result; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments