-
Notifications
You must be signed in to change notification settings - Fork 37
/
StdinHandler.php
247 lines (228 loc) · 6.8 KB
/
StdinHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
namespace Consolidation\AnnotatedCommand\Input;
use Symfony\Component\Console\Input\StreamableInputInterface;
use Symfony\Component\Console\Input\InputInterface;
/**
* StdinHandler is a thin wrapper around php://stdin. It provides
* methods for redirecting input from a file, possibly conditionally
* under the control of an Input object.
*
* Example trivial usage (always reads from stdin):
*
* class Example implements StdinAwareInterface
* {
* /**
* * @command cat
* * @param string $file
* * @default $file -
* * /
* public function cat()
* {
* print($this->stdin()->contents());
* }
* }
*
* Command that reads from stdin or file via an option:
*
* /**
* * @command cat
* * @param string $file
* * @default $file -
* * /
* public function cat(InputInterface $input)
* {
* $data = $this->stdin()->select($input, 'file')->contents();
* }
*
* Command that reads from stdin or file via an option:
*
* /**
* * @command cat
* * @option string $file
* * @default $file -
* * /
* public function cat(InputInterface $input)
* {
* $data = $this->stdin()->select($input, 'file')->contents();
* }
*
* It is also possible to inject the selected stream into the input object,
* e.g. if you want the contents of the source file to be fed to any Question
* helper et. al. that the $input object is used with.
*
* /**
* * @command example
* * @option string $file
* * @default $file -
* * /
* public function example(InputInterface $input)
* {
* $this->stdin()->setStream($input, 'file');
* }
*
*
* Inject an alternate source for standard input in tests. Presumes that
* the object under test gets a reference to the StdinHandler via dependency
* injection from the container.
*
* $container->get('stdinHandler')->redirect($pathToTestStdinFileFixture);
*
* You may also inject your stdin file fixture stream into the $input object
* as usual, and then use it with 'select()' or 'setStream()' as shown above.
*
* Finally, this class may also be used in absence of a dependency injection
* container by using the static 'selectStream()' method:
*
* /**
* * @command example
* * @option string $file
* * @default $file -
* * /
* public function example(InputInterface $input)
* {
* $data = StdinHandler::selectStream($input, 'file')->contents();
* }
*
* To test a method that uses this technique, simply inject your stdin
* fixture into the $input object in your test:
*
* $input->setStream(fopen($pathToFixture, 'r'));
*/
class StdinHandler
{
protected $path;
protected $stream;
public static function selectStream(InputInterface $input, $optionOrArg)
{
$handler = new self();
return $handler->setStream($input, $optionOrArg);
}
/**
* hasPath returns 'true' if the stdin handler has a path to a file.
*
* @return bool
*/
public function hasPath()
{
// Once the stream has been opened, we mask the existence of the path.
return !$this->hasStream() && !empty($this->path);
}
/**
* hasStream returns 'true' if the stdin handler has opened a stream.
*
* @return bool
*/
public function hasStream()
{
return !empty($this->stream);
}
/**
* path returns the path to any file that was set as a redirection
* source, or `php://stdin` if none have been.
*
* @return string
*/
public function path()
{
return $this->path ?: 'php://stdin';
}
/**
* close closes the input stream if it was opened.
*/
public function close()
{
if ($this->hasStream()) {
fclose($this->stream);
$this->stream = null;
}
return $this;
}
/**
* redirect specifies a path to a file that should serve as the
* source to read from. If the input path is '-' or empty,
* then output will be taken from php://stdin (or whichever source
* was provided via the 'redirect' method).
*
* @return $this
*/
public function redirect($path)
{
if ($this->pathProvided($path)) {
$this->path = $path;
}
return $this;
}
/**
* select chooses the source of the input stream based on whether or
* not the user provided the specified option or argument on the commandline.
* Stdin is selected if there is no user selection.
*
* @param InputInterface $input
* @param string $optionOrArg
* @return $this
*/
public function select(InputInterface $input, $optionOrArg)
{
$this->redirect($this->getOptionOrArg($input, $optionOrArg));
if (!$this->hasPath() && ($input instanceof StreamableInputInterface)) {
$this->stream = $input->getStream();
}
return $this;
}
/**
* getStream opens and returns the stdin stream (or redirect file).
*/
public function getStream()
{
if (!$this->hasStream()) {
$this->stream = fopen($this->path(), 'r');
}
return $this->stream;
}
/**
* setStream functions like 'select', and also sets up the $input
* object to read from the selected input stream e.g. when used
* with a question helper.
*/
public function setStream(InputInterface $input, $optionOrArg)
{
$this->select($input, $optionOrArg);
if ($input instanceof StreamableInputInterface) {
$stream = $this->getStream();
$input->setStream($stream);
}
return $this;
}
/**
* contents reads the entire contents of the standard input stream.
*
* @return string
*/
public function contents()
{
// Optimization: use file_get_contents if we have a path to a file
// and the stream has not been opened yet.
if (!$this->hasStream()) {
return file_get_contents($this->path());
}
$stream = $this->getStream();
stream_set_blocking($stream, false); // TODO: We did this in backend invoke. Necessary here?
$contents = stream_get_contents($stream);
$this->close();
return $contents;
}
/**
* Returns 'true' if a path was specfied, and that path was not '-'.
*/
protected function pathProvided($path)
{
return !empty($path) && ($path != '-');
}
protected function getOptionOrArg(InputInterface $input, $optionOrArg)
{
if ($input->hasOption($optionOrArg)) {
return $input->getOption($optionOrArg);
}
return $input->getArgument($optionOrArg);
}
}