This repository has been archived by the owner on Feb 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessorIterator.php
329 lines (288 loc) · 8.29 KB
/
ProcessorIterator.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
namespace Box\Component\Processor;
use Box\Component\Processor\Exception\ProcessingException;
use Iterator;
use OuterIterator;
use SplFileInfo;
/**
* Processes files as an iterator is used.
*
* The `ProcessorIterator` is designed for use with `buildFromIterator()` from
* the `Phar` class. The requirements for key/value pairs provided by the given
* iterator are the same as those of `Phar::buildFromIterator()`. According to
* the documentation, the following pairs are supported:
*
* | Key | Value |
* |:--------------------|:--------------------------|
* | path inside archive | path outside archive |
* | path inside archive | file stream resource |
* | - | instance of `SplFileInfo` |
*
* The `ProcessorIterator` will always return the following pair:
*
* | Key | Value |
* |:--------------------|:---------------------|
* | path inside archive | file stream resource |
*
* The file stream resource that is returned as the value will contain the
* processed file contents. The stream is opened using `php://memory`, which
* means that a file does not actually exist for the processed contents. Since
* only a file stream resource is returned, it is necessary for the base path
* to be provided to this iterator as the `Phar::buildFromIterator()` method
* will not have an opportunity to process the returned archive path.
*
* @author Kevin Herrera <kevin@herrera.io>
*/
class ProcessorIterator implements Iterator, OuterIterator
{
/**
* The base directory path.
*
* @var string
*/
private $base;
/**
* The current value.
*
* @var resource
*/
private $current;
/**
* The iterator.
*
* @var Iterator
*/
private $iterator;
/**
* The key.
*
* @var string
*/
private $key;
/**
* The processor.
*
* @var ProcessorInterface
*/
private $processor;
/**
* Sets the iterator, processor, and base directory path.
*
* If the given iterator returns instance of `SplFileInfo` as the current
* value, the `$base` directory path argument is required. If a path is not
* provided, an exception will later be thrown.
*
* @param Iterator $iterator The iterator.
* @param ProcessorInterface $processor The processor.
* @param string $base The base directory path.
*/
public function __construct(
Iterator $iterator,
ProcessorInterface $processor,
$base = null
) {
if (null !== $base) {
$this->base = '/' . preg_quote($base, '/') . '/';
}
$this->iterator = $iterator;
$this->processor = $processor;
}
/**
* {@inheritdoc}
*/
public function current()
{
return $this->current;
}
/**
* {@inheritdoc}
*/
public function getInnerIterator()
{
return $this->iterator;
}
/**
* {@inheritdoc}
*/
public function key()
{
return $this->key;
}
/**
* {@inheritdoc}
*/
public function next()
{
$this->iterator->next();
}
/**
* {@inheritdoc}
*/
public function rewind()
{
$this->iterator->rewind();
}
/**
* {@inheritdoc}
*/
public function valid()
{
if ($this->iterator->valid()) {
if ($this->isSupported()) {
$this->key = $this->getKey();
$this->current = $this->getCurrent();
if ($this->processor->supports($this->key())) {
$this->current = $this->processContents($this->current);
}
} else {
$this->current = $this->iterator->current();
$this->key = $this->iterator->key();
}
return true;
}
return false;
}
/**
* Returns the processed value.
*
* @return resource The processed value.
*/
private function getCurrent()
{
$current = $this->iterator->current();
if ($current instanceof SplFileInfo) {
$current = $this->readInfo($current);
} elseif (is_string($current)
&& (false === strpos($current, "\n"))
&& file_exists($current)) {
$current = $this->readPath($current);
} elseif (is_resource($current)) {
$current = $this->readResource($current);
// @codeCoverageIgnoreStart
} else {
throw new ProcessingException(
sprintf(
'The iterator value "%s" is not compatible.',
is_object($current)
? 'object(' . get_class($current) . ')'
: gettype($current) . "($current)"
)
);
}
// @codeCoverageIgnoreEnd
return $current;
}
/**
* Returns the processed key.
*
* @return string The processed key.
*
* @throws ProcessingException If the base directory path is not set.
*/
private function getKey()
{
$key = $this->iterator->key();
if (null !== $this->base) {
$key = preg_replace($this->base, '', $key);
} elseif ($this->iterator->current() instanceof SplFileInfo) {
throw new ProcessingException(
'The base directory path is required to use SplFileInfo.'
);
}
return $key;
}
/**
* Checks if the current value is supported for processing.
*
* @return boolean Returns `true` if supported, `false` if not.
*/
private function isSupported()
{
$current = $this->iterator->current();
if ($current instanceof SplFileInfo) {
return $this->processor->supports($current->getPathname());
}
if (is_string($current)
&& (false === strpos($current, "\n"))) {
return $this->processor->supports($current);
}
if (is_resource($current)) {
return $this->processor->supports($this->iterator->key());
}
return false; // @codeCoverageIgnore
}
/**
* Processes the file contents and writes it to a stream.
*
* @param string $contents The file contents.
*
* @return resource The processed contents stream.
*
* @throws ProcessingException If a memory stream could not be opened.
*/
private function processContents($contents)
{
// @codeCoverageIgnoreStart
if (false === ($resource = fopen('php://memory', 'rb+'))) {
throw new ProcessingException(
'The memory stream could not be opened.'
);
}
// @codeCoverageIgnoreEnd
if ($this->processor->supports($this->key)) {
$contents = $this->processor->process($this->key, $contents);
}
fwrite($resource, $contents);
rewind($resource);
return $resource;
}
/**
* Processes the contents of a file managed by `SplFileInfo`.
*
* @param SplFileInfo $info The file information object.
*
* @return resource The processed file contents.
*/
private function readInfo(SplFileInfo $info)
{
return $this->readPath(
$info->getPathname()
);
}
/**
* Returns the contents of a file.
*
* @param string $path The path to the file.
*
* @return string The file contents.
*
* @throws ProcessingException If the file could not be opened.
*/
private function readPath($path)
{
// @codeCoverageIgnoreStart
if (false === ($handle = fopen($path, 'rb'))) {
throw new ProcessingException(
"The file \"$path\" could not be opened for reading."
);
}
// @codeCoverageIgnoreEnd
return $this->readResource($handle);
}
/**
* Returns the contents of a file stream.
*
* @param resource $resource The file resource.
*
* @return string The file contents.
*/
private function readResource($resource)
{
$contents = '';
do {
$contents .= fgets($resource);
} while (!feof($resource));
fclose($resource);
return $contents;
}
}