1
|
|
|
<?php |
2
|
|
|
namespace Shlinkio\Shlink\CLI\Install\Plugin; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
5
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Question\Question; |
9
|
|
|
|
10
|
|
|
abstract class AbstractConfigCustomizerPlugin implements ConfigCustomizerPluginInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var QuestionHelper |
14
|
|
|
*/ |
15
|
|
|
protected $questionHelper; |
16
|
|
|
|
17
|
14 |
|
public function __construct(QuestionHelper $questionHelper) |
18
|
|
|
{ |
19
|
14 |
|
$this->questionHelper = $questionHelper; |
20
|
14 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param InputInterface $input |
24
|
|
|
* @param OutputInterface $output |
25
|
|
|
* @param string $text |
26
|
|
|
* @param string|null $default |
27
|
|
|
* @param bool $allowEmpty |
28
|
|
|
* @return string |
29
|
|
|
* @throws RuntimeException |
30
|
|
|
*/ |
31
|
6 |
|
protected function ask(InputInterface $input, OutputInterface $output, $text, $default = null, $allowEmpty = false) |
32
|
|
|
{ |
33
|
6 |
|
if ($default !== null) { |
34
|
2 |
|
$text .= ' (defaults to ' . $default . ')'; |
35
|
2 |
|
} |
36
|
|
|
do { |
37
|
6 |
|
$value = $this->questionHelper->ask($input, $output, new Question( |
38
|
6 |
|
'<question>' . $text . ':</question> ', |
39
|
|
|
$default |
40
|
6 |
|
)); |
41
|
6 |
|
if (empty($value) && ! $allowEmpty) { |
42
|
|
|
$output->writeln('<error>Value can\'t be empty</error>'); |
43
|
|
|
} |
44
|
6 |
|
} while (empty($value) && $default === null && ! $allowEmpty); |
45
|
|
|
|
46
|
6 |
|
return $value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param OutputInterface $output |
51
|
|
|
* @param string $text |
52
|
|
|
*/ |
53
|
13 |
|
protected function printTitle(OutputInterface $output, $text) |
54
|
|
|
{ |
55
|
13 |
|
$text = trim($text); |
56
|
13 |
|
$length = strlen($text) + 4; |
57
|
13 |
|
$header = str_repeat('*', $length); |
58
|
|
|
|
59
|
13 |
|
$output->writeln([ |
60
|
13 |
|
'', |
61
|
13 |
|
'<info>' . $header . '</info>', |
62
|
13 |
|
'<info>* ' . strtoupper($text) . ' *</info>', |
63
|
13 |
|
'<info>' . $header . '</info>', |
64
|
13 |
|
]); |
65
|
13 |
|
} |
66
|
|
|
} |
67
|
|
|
|