|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ptondereau\PackMe\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
|
6
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Question\Question; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class BaseCommand. |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractBaseCommand |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var InputInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $input; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var OutputInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $output; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var HelperSet |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $helperSet; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* BaseCommand constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param HelperSet $helperSet |
|
36
|
|
|
*/ |
|
37
|
9 |
|
public function __construct(HelperSet $helperSet) |
|
38
|
|
|
{ |
|
39
|
9 |
|
$this->helperSet = $helperSet; |
|
40
|
9 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $question |
|
44
|
|
|
* @param \Closure $validator |
|
45
|
|
|
* @param null|int $attempts |
|
46
|
|
|
* @param null|string $default |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function askAndValidate($question, $validator, $attempts = null, $default = null) |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var QuestionHelper $helper */ |
|
53
|
|
|
$helper = $this->helperSet->get('question'); |
|
54
|
|
|
$question = new Question($question, $default); |
|
55
|
|
|
$question->setValidator($validator); |
|
56
|
|
|
$question->setMaxAttempts($attempts); |
|
57
|
|
|
|
|
58
|
|
|
return $helper->ask($this->input, $this->getErrorOutput(), $question); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $question |
|
63
|
|
|
* @param null|string $default |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function ask($question, $default = null) |
|
68
|
|
|
{ |
|
69
|
|
|
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */ |
|
70
|
|
|
$helper = $this->helperSet->get('question'); |
|
71
|
|
|
$question = new Question($question, $default); |
|
72
|
|
|
|
|
73
|
|
|
return $helper->ask($this->input, $this->getErrorOutput(), $question); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return OutputInterface |
|
78
|
|
|
*/ |
|
79
|
|
|
private function getErrorOutput() |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->output instanceof ConsoleOutputInterface) { |
|
82
|
|
|
return $this->output->getErrorOutput(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this->output; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|