1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace EddIriarte\Console\Helpers; |
|
|
|
|
3
|
|
|
|
4
|
|
|
use EddIriarte\Console\Handlers\SelectHandler; |
5
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
6
|
|
|
use Symfony\Component\Console\Helper\HelperInterface; |
7
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use EddIriarte\Console\Inputs\Interfaces\SelectInput; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SelectionHelper |
14
|
|
|
* |
15
|
|
|
* Its used for registration to symfonys output helpers. |
16
|
|
|
* |
17
|
|
|
* @package EddIriarte\Console\Helpers |
|
|
|
|
18
|
|
|
* @author Eduardo Iriarte <eddiriarte[at]gmail[dot]com> |
|
|
|
|
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
class SelectionHelper implements HelperInterface |
21
|
|
|
{ |
|
|
|
|
22
|
|
|
use StreamableInput; |
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @var InputInterface |
26
|
|
|
*/ |
27
|
|
|
protected $input; |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @var OutputInterface |
31
|
|
|
*/ |
32
|
|
|
protected $output; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* @var null |
36
|
|
|
*/ |
37
|
|
|
protected $helperSet = null; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* SelectionHelper constructor. |
41
|
|
|
* @param InputInterface $input |
|
|
|
|
42
|
|
|
* @param OutputInterface $output |
|
|
|
|
43
|
|
|
*/ |
44
|
5 |
|
public function __construct(InputInterface $input, OutputInterface $output) |
|
|
|
|
45
|
|
|
{ |
|
|
|
|
46
|
5 |
|
$this->input = $input; |
|
|
|
|
47
|
5 |
|
$this->output = $output; |
48
|
|
|
|
49
|
5 |
|
$this->checkAnsiSupport(); |
50
|
5 |
|
$this->setOutputStyles(); |
51
|
5 |
|
} |
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
|
|
|
|
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
|
|
|
|
56
|
2 |
|
public function setHelperSet(HelperSet $helperSet = null) |
|
|
|
|
57
|
|
|
{ |
|
|
|
|
58
|
2 |
|
$this->helperSet = $helperSet; |
|
|
|
|
59
|
2 |
|
} |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
|
|
|
|
64
|
1 |
|
public function getHelperSet() |
65
|
|
|
{ |
|
|
|
|
66
|
1 |
|
return $this->helperSet; |
67
|
|
|
} |
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
|
|
|
|
72
|
2 |
|
public function getName() |
73
|
|
|
{ |
|
|
|
|
74
|
2 |
|
return 'selection'; |
75
|
|
|
} |
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Allow multiple item selections to user. |
79
|
|
|
* |
80
|
|
|
* @param SelectInput $question |
|
|
|
|
81
|
|
|
* @return array |
|
|
|
|
82
|
|
|
*/ |
83
|
1 |
|
public function select(SelectInput $question) |
84
|
|
|
{ |
|
|
|
|
85
|
1 |
|
$select = new SelectHandler($question, $this->output, $this->getInputStream()); |
86
|
|
|
|
87
|
1 |
|
$responses = $select->handle(); |
88
|
|
|
// TODO: validate responses ??? |
|
|
|
|
89
|
|
|
|
90
|
1 |
|
return $responses; |
91
|
|
|
} |
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
|
|
|
|
94
|
|
|
* |
95
|
|
|
*/ |
|
|
|
|
96
|
5 |
|
protected function checkAnsiSupport(): void |
97
|
|
|
{ |
|
|
|
|
98
|
5 |
|
if ($this->output->isDecorated()) { |
99
|
3 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// // disable overwrite when output does not support ANSI codes. |
103
|
|
|
// $this->overwrite = false; |
|
|
|
|
104
|
|
|
// // set a reasonable redraw frequency so output isn't flooded |
105
|
|
|
// $this->setRedrawFrequency(10); |
|
|
|
|
106
|
2 |
|
} |
|
|
|
|
107
|
|
|
|
108
|
5 |
|
protected function setOutputStyles() |
|
|
|
|
109
|
|
|
{ |
|
|
|
|
110
|
5 |
|
if (!$this->output->getFormatter()->hasStyle('hl')) { |
|
|
|
|
111
|
3 |
|
$style = new OutputFormatterStyle('black', 'white'); |
112
|
3 |
|
$this->output->getFormatter()->setStyle('hl', $style); |
113
|
|
|
} |
114
|
5 |
|
} |
|
|
|
|
115
|
|
|
} |
|
|
|
|
116
|
|
|
|