-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathQuestionHelper.php
More file actions
72 lines (60 loc) · 2.66 KB
/
Copy pathQuestionHelper.php
File metadata and controls
72 lines (60 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/*
* Copyright (c) Default Value LLC.
* This source file is subject to the License https://github.com/DefaultValue/dockerizer_for_php/LICENSE.txt
* Do not change this file if you want to upgrade the tool to the newer versions in the future
* Please, contact us at https://default-value.com/#contact if you wish to customize this tool
* according to you business needs
*/
declare(strict_types=1);
namespace DefaultValue\Dockerizer\Console\Helper;
use DefaultValue\Dockerizer\Console\Question\ChoiceQuestionWithRecommendation;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
/**
* Extends Symfony's question helper to render a post-choices message between the choices list and the ` > ` prompt.
* Only activates for {@see ChoiceQuestionWithRecommendation}; every other question falls through to the parent.
*/
class QuestionHelper extends SymfonyQuestionHelper
{
protected function writePrompt(OutputInterface $output, Question $question): void
{
if (!$question instanceof ChoiceQuestionWithRecommendation || $question->getPostChoicesMessage() === '') {
parent::writePrompt($output, $question);
return;
}
$text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
$default = $question->getDefault();
$choices = $question->getChoices();
if ($default === null) {
$header = sprintf(' <info>%s</info>:', $text);
} else {
$defaultString = (string) $default;
if ($question->isMultiselect()) {
$labels = [];
foreach (explode(',', $defaultString) as $value) {
$key = trim($value);
$labels[] = (string) ($choices[$key] ?? $key);
}
$header = sprintf(
' <info>%s</info> [<comment>%s</comment>]:',
$text,
OutputFormatter::escape(implode(', ', $labels))
);
} else {
$label = (string) ($choices[$defaultString] ?? $defaultString);
$header = sprintf(
' <info>%s</info> [<comment>%s</comment>]:',
$text,
OutputFormatter::escape($label)
);
}
}
$output->writeln($header);
$output->writeln($this->formatChoiceQuestionChoices($question, 'comment'));
$output->writeln($question->getPostChoicesMessage());
$output->write($question->getPrompt());
}
}