|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of byrokrat\giroapp. |
|
4
|
|
|
* |
|
5
|
|
|
* byrokrat\giroapp is free software: you can redistribute it and/or |
|
6
|
|
|
* modify it under the terms of the GNU General Public License as published |
|
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* byrokrat\giroapp is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
* |
|
18
|
|
|
* Copyright 2016-20 Hannes Forsgård |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
|
22
|
|
|
|
|
23
|
|
|
namespace byrokrat\giroapp\Console\Helper; |
|
24
|
|
|
|
|
25
|
|
|
use byrokrat\giroapp\Validator\ValidatorInterface; |
|
26
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
28
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
29
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
30
|
|
|
use Symfony\Component\Console\Question\Question; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Read option or fallback to interactive question |
|
34
|
|
|
*/ |
|
35
|
|
|
class InputReader |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var InputInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $input; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var OutputInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $output; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var QuestionHelper |
|
49
|
|
|
*/ |
|
50
|
|
|
private $questionHelper; |
|
51
|
|
|
|
|
52
|
|
|
public function __construct(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->input = $input; |
|
55
|
|
|
$this->output = $output; |
|
56
|
|
|
$this->questionHelper = $questionHelper; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function readInput(string $key, Question $question, ValidatorInterface $validator): string |
|
60
|
|
|
{ |
|
61
|
|
|
$value = null; |
|
62
|
|
|
|
|
63
|
|
|
if ($this->input->hasOption($key)) { |
|
64
|
|
|
$value = $this->input->getOption($key); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (is_string($value)) { |
|
68
|
|
|
return $validator->validate($key, $value); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$value = (string)$this->questionHelper->ask( |
|
72
|
|
|
$this->input, |
|
73
|
|
|
$this->output, |
|
74
|
|
|
$question->setValidator(function ($answer) use ($key, $validator) { |
|
75
|
|
|
return $validator->validate($key, (string)$answer); |
|
76
|
|
|
}) |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
return $this->input->isInteractive() ? $value : $validator->validate($key, $value); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function readOptionalInput(string $key, string $default, ValidatorInterface $validator): string |
|
83
|
|
|
{ |
|
84
|
|
|
$value = null; |
|
85
|
|
|
|
|
86
|
|
|
if ($this->input->hasOption($key)) { |
|
87
|
|
|
$value = $this->input->getOption($key); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (is_string($value)) { |
|
91
|
|
|
return $validator->validate($key, $value); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Ask if user wants to edit |
|
95
|
|
|
$requestEdit = $this->questionHelper->ask( |
|
96
|
|
|
$this->input, |
|
97
|
|
|
$this->output, |
|
98
|
|
|
new ConfirmationQuestion("$key: <comment>$default</comment>\nEdit [<info>y/N</info>]? ", false) |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
// Return default if user does not want to edit |
|
102
|
|
|
if (!$requestEdit) { |
|
103
|
|
|
return $default; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$value = (string)$this->questionHelper->ask( |
|
107
|
|
|
$this->input, |
|
108
|
|
|
$this->output, |
|
109
|
|
|
(new Question("New $key: "))->setValidator(function ($answer) use ($key, $validator) { |
|
110
|
|
|
return $validator->validate($key, (string)$answer); |
|
111
|
|
|
}) |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
return $this->input->isInteractive() ? $value : $validator->validate($key, $value); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|