|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* @author Tom Klingenberg <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace N98\Util\Console\Helper; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
|
9
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
|
10
|
|
|
use Symfony\Component\Console\Helper\HelperInterface; |
|
11
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class IoHelper |
|
19
|
|
|
* |
|
20
|
|
|
* Helper named "io" providing (Input and) OutputInterface within the global helper-set |
|
21
|
|
|
* |
|
22
|
|
|
* Register itself on @see ConsoleEvents::COMMAND event to populate helper fields |
|
23
|
|
|
* |
|
24
|
|
|
* @package N98\Util\Console\Helper |
|
25
|
|
|
*/ |
|
26
|
|
|
class IoHelper implements HelperInterface, EventSubscriberInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var HelperSet |
|
30
|
|
|
*/ |
|
31
|
|
|
private $helperSet; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var OutputInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $output; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var InputInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $input; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @see getSubscribedEvents |
|
45
|
|
|
* |
|
46
|
|
|
* @param ConsoleCommandEvent $event |
|
47
|
|
|
*/ |
|
48
|
|
|
public function initializeEventIo(ConsoleCommandEvent $event) |
|
49
|
|
|
{ |
|
50
|
|
|
/** @var $helper IoHelper */ |
|
51
|
|
|
$helper = $event->getCommand()->getHelperSet()->get($this->getName()); |
|
52
|
|
|
$helper->initializeIo($event->getInput(), $event->getOutput()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param InputInterface $input |
|
57
|
|
|
* @param OutputInterface $output |
|
58
|
|
|
*/ |
|
59
|
|
|
public function initializeIo(InputInterface $input, OutputInterface $output) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->input = $input; |
|
62
|
|
|
$this->output = $output; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return OutputInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getOutput() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->output; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/* |
|
74
|
|
|
* HelperInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets the helper set associated with this helper. |
|
79
|
|
|
* |
|
80
|
|
|
* @param HelperSet $helperSet A HelperSet instance |
|
81
|
|
|
* |
|
82
|
|
|
* @api |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setHelperSet(HelperSet $helperSet = null) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->helperSet = $helperSet; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Gets the helper set associated with this helper. |
|
91
|
|
|
* |
|
92
|
|
|
* @return HelperSet A HelperSet instance |
|
93
|
|
|
* |
|
94
|
|
|
* @api |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getHelperSet() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->helperSet; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the canonical name of this helper. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string The canonical name |
|
105
|
|
|
* |
|
106
|
|
|
* @api |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getName() |
|
109
|
|
|
{ |
|
110
|
|
|
return 'io'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/* |
|
114
|
|
|
* EventSubscriberInterface |
|
115
|
|
|
*/ |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @inheritdoc |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function getSubscribedEvents() |
|
121
|
|
|
{ |
|
122
|
|
|
return array( |
|
123
|
|
|
ConsoleEvents::COMMAND => 'initializeEventIo', /** @see initializeEventIo */ |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|