1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dock\Cli\IO; |
4
|
|
|
|
5
|
|
|
use Dock\IO\UserInteraction; |
6
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
7
|
|
|
use Symfony\Component\Console\Helper\FormatterHelper; |
8
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Question\Question; |
12
|
|
|
|
13
|
|
|
class ConsoleUserInteraction implements UserInteraction |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var OutputInterface |
17
|
|
|
*/ |
18
|
|
|
private $output; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var InputInterface |
22
|
|
|
*/ |
23
|
|
|
private $input; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This method is called when a console command event is fired. |
27
|
|
|
* |
28
|
|
|
* @param ConsoleCommandEvent $event |
29
|
|
|
*/ |
30
|
|
|
public function onCommand(ConsoleCommandEvent $event) |
31
|
|
|
{ |
32
|
|
|
$this->input = $event->getInput(); |
33
|
|
|
$this->output = $event->getOutput(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function writeTitle($name) |
40
|
|
|
{ |
41
|
|
|
$formatter = new FormatterHelper(); |
42
|
|
|
$formattedBlock = $formatter->formatBlock([$name], 'info'); |
43
|
|
|
$this->getOutput()->writeln($formattedBlock); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function write($string) |
50
|
|
|
{ |
51
|
|
|
$this->getOutput()->writeln($string); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function ask(Question $question) |
58
|
|
|
{ |
59
|
|
|
$questionHelper = new QuestionHelper(); |
60
|
|
|
|
61
|
|
|
return $questionHelper->ask($this->getInput(), $this->getOutput(), $question); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @throws \RuntimeException |
66
|
|
|
* |
67
|
|
|
* @return InputInterface |
68
|
|
|
*/ |
69
|
|
|
private function getInput() |
70
|
|
|
{ |
71
|
|
|
if (null === $this->input) { |
72
|
|
|
throw new \RuntimeException('No user interaction context available.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->input; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @throws \RuntimeException |
80
|
|
|
* |
81
|
|
|
* @return OutputInterface |
82
|
|
|
*/ |
83
|
|
|
public function getOutput() |
84
|
|
|
{ |
85
|
|
|
if (null === $this->output) { |
86
|
|
|
throw new \RuntimeException('No user interaction context available.'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->output; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|