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
/
Copy pathBuilder.php
406 lines (348 loc) · 11.5 KB
/
Builder.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
namespace Box\Component\Builder;
use BadMethodCallException;
use Box\Component\Builder\Event\PostAddEmptyDirEvent;
use Box\Component\Builder\Event\PostAddFileEvent;
use Box\Component\Builder\Event\PostAddFromStringEvent;
use Box\Component\Builder\Event\PostBuildFromDirectoryEvent;
use Box\Component\Builder\Event\PostBuildFromIteratorEvent;
use Box\Component\Builder\Event\PostSetStubEvent;
use Box\Component\Builder\Event\PreAddEmptyDirEvent;
use Box\Component\Builder\Event\PreAddFileEvent;
use Box\Component\Builder\Event\PreAddFromStringEvent;
use Box\Component\Builder\Event\PreBuildFromDirectoryEvent;
use Box\Component\Builder\Event\PreBuildFromIteratorEvent;
use Box\Component\Builder\Event\PreSetStubEvent;
use Box\Component\Builder\Exception\BuilderException;
use Box\Component\Builder\Iterator\RegexIterator;
use Iterator;
use KHerGe\File\File;
use Phar;
use PharFileInfo;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Creates and modifies PHP archives with support for event dispatching.
*
* When an event dispatcher is registered with the builder, a series of events
* become observable. These events are all listed in the `Events` class and can
* be used by listeners and subscribers for registration. If an event dispatcher
* is not set, the builder's behavior is identical to that of `Phar`.
*
* @author Kevin Herrera <[email protected]>
*/
class Builder extends Phar
{
/**
* The event dispatcher.
*
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* Adds an empty directory to the archive.
*
* @param string $local The path to the directory in the archive.
*/
public function addEmptyDir($local = null)
{
if (null === $this->dispatcher) {
parent::addEmptyDir($local);
return;
}
$event = new PreAddEmptyDirEvent($this, $local);
$this->dispatcher->dispatch(
Events::PRE_ADD_EMPTY_DIR,
$event
);
if (!$event->isSkipped()) {
parent::addEmptyDir($event->getLocal());
$event = new PostAddEmptyDirEvent($this, $event->getLocal());
$this->dispatcher->dispatch(
Events::POST_ADD_EMPTY_DIR,
$event
);
}
}
/**
* Adds a file to the archive.
*
* @param string $file The path to the file.
* @param string $local The path to the file in the archive.
*/
public function addFile($file, $local = null)
{
if (null === $this->dispatcher) {
// @codeCoverageIgnoreStart
if (null === $local) {
parent::addFile($file);
} else {
parent::addFile($file, $local);
}
// @codeCoverageIgnoreEnd
return;
}
$event = new PreAddFileEvent($this, $file, $local);
$this->dispatcher->dispatch(
Events::PRE_ADD_FILE,
$event
);
if (!$event->isSkipped()) {
/*
* By using `Phar::addFile()` directly, it eliminates any
* possibility of making changes to the contents of the file
* before it is added to the archive. To work around this issue,
* `Builder::addFromString()` is used to add the file after its
* contents have been read.
*/
$this->addFromString(
(null === $event->getLocal())
? $event->getFile()
: $event->getLocal(),
$this->getFileContents($event->getFile())
);
$event = new PostAddFileEvent(
$this,
$event->getFile(),
$event->getLocal()
);
$this->dispatcher->dispatch(
Events::POST_ADD_FILE,
$event
);
}
}
/**
* Adds a file from a string to the archive.
*
* @param string $local The path to the file in the archive.
* @param string $contents The contents of the file.
*/
public function addFromString($local, $contents = null)
{
if (null === $this->dispatcher) {
parent::addFromString($local, $contents);
return;
}
$event = new PreAddFromStringEvent($this, $local, $contents);
$this->dispatcher->dispatch(
Events::PRE_ADD_FROM_STRING,
$event
);
if (!$event->isSkipped()) {
parent::addFromString($event->getLocal(), $event->getContents());
$event = new PostAddFromStringEvent(
$this,
$event->getLocal(),
$event->getContents()
);
$this->dispatcher->dispatch(
Events::POST_ADD_FROM_STRING,
$event
);
}
}
/**
* Recursively adds files from a directory to the archive.
*
* The `$regex` is used as a whitelist filter. Any path in the directory
* that matches the regular expression will be added to the archive. Any
* non-matching paths will be excluded.
*
* @param string $path The path to the directory.
* @param string $filter The regular expression filter.
*
* @return array An associative array mapping archive paths to file system paths.
*/
public function buildFromDirectory($path, $filter = null)
{
if (null === $this->dispatcher) {
return parent::buildFromDirectory($path, $filter);
}
$event = new PreBuildFromDirectoryEvent($this, $path, $filter);
$this->dispatcher->dispatch(
Events::PRE_BUILD_FROM_DIRECTORY,
$event
);
$map = array();
if (!$event->isSkipped()) {
/*
* By using `Phar::buildFromDirectory()` directly, it eliminates
* any possibility of making changes to the files as they are
* passed to the archive. To work around this issue, the data is
* used to create an iterator achieving the same purpose, but using
* `Builder::buildFromIterator()` instead.
*/
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$event->getPath(),
RecursiveDirectoryIterator::CURRENT_AS_FILEINFO
| RecursiveDirectoryIterator::KEY_AS_PATHNAME
| RecursiveDirectoryIterator::SKIP_DOTS
)
);
if (null !== $filter) {
$iterator = new RegexIterator($iterator, $event->getFilter());
}
$map = $this->buildFromIterator($iterator, $event->getPath());
$event = new PostBuildFromDirectoryEvent(
$this,
$event->getPath(),
$event->getFilter()
);
$this->dispatcher->dispatch(
Events::POST_BUILD_FROM_DIRECTORY,
$event
);
}
return $map;
}
/**
* Adds files returned by an iterator.
*
* The base directory path is used to trim convert absolute paths into
* relative file inside the archive. The path `/path/to/my/file.php`
* would then become `my/file.php` if `/path/to/` is provided as the
* base path. It is important to note that the trailing slash in the base
* directory path is necessary.
*
* @param Iterator $iterator The iterator.
* @param string $base The base directory path.
*
* @return array An associative array mapping archive paths to file system paths.
*/
public function buildFromIterator($iterator, $base = null)
{
if (null === $this->dispatcher) {
return parent::buildFromIterator($iterator, $base);
}
$event = new PreBuildFromIteratorEvent($this, $iterator, $base);
$this->dispatcher->dispatch(
Events::PRE_BUILD_FROM_ITERATOR,
$event
);
$map = array();
if (!$event->isSkipped()) {
$map = parent::buildFromIterator(
$event->getIterator(),
$event->getBase()
);
$event = new PostBuildFromIteratorEvent(
$this,
$event->getIterator(),
$event->getBase()
);
$this->dispatcher->dispatch(
Events::POST_BUILD_FROM_ITERATOR,
$event
);
}
return $map;
}
/**
* {@inheritdoc}
*
* @throws BuilderException If the archive could not be compressed.
*
* @codeCoverageIgnore
*/
public function compressFiles($algorithm)
{
try {
parent::compressFiles($algorithm);
} catch (BadMethodCallException $exception) {
if ('unable to create temporary file' !== $exception->getMessage()) {
throw $exception;
}
throw new BuilderException(
preg_replace(
'/\n+/',
' ',
<<<MESSAGE
There is a known bug in the phar extension with regards to the compression
of archives that contain a large number of files. It is recommended that you
increase the maximum number of open files allowed and try again. (This archive
contains {$this->count()} files.) On Linux, the command you need to use to
increase the limit is `ulimit -n \$NUM`.
MESSAGE
)
);
}
}
/**
* Sets the event dispatcher.
*
* @param EventDispatcherInterface $dispatcher The event dispatcher.
*/
public function setEventDispatcher(
EventDispatcherInterface $dispatcher = null
) {
$this->dispatcher = $dispatcher;
}
/**
* Resolves the path to its `PharFileInfo` object.
*
* @param string $local The path to the file or directory in the archive.
*
* @return PharFileInfo The path object.
*/
public function resolvePath($local)
{
$local = sprintf(
'phar://%s/%s',
$this->getAlias(),
ltrim($local, '\\/')
);
if (file_exists($local)) {
return new PharFileInfo($local);
}
return null;
}
/**
* Sets the stub in the archive.
*
* @param string $stub The stub.
*
* @return boolean Returns `true` if successful, `false` if not.
*/
public function setStub($stub, $_ = -1)
{
if (null === $this->dispatcher) {
return parent::setStub($stub);
}
$event = new PreSetStubEvent($this, $stub);
$this->dispatcher->dispatch(
Events::PRE_SET_STUB,
$event
);
$result = false;
if (!$event->isSkipped()) {
$result = parent::setStub($event->getStub());
$this->dispatcher->dispatch(
Events::POST_SET_STUB,
new PostSetStubEvent(
$this,
$event->getStub()
)
);
}
return $result;
}
/**
* Returns the contents of a file.
*
* @param string $path The path to the file.
*
* @return string The contents of the file.
*/
private function getFileContents($path)
{
$contents = '';
$file = File::create($path);
do {
$contents .= $file->fgets();
} while (!$file->eof());
return $contents;
}
}