/
FileStreamWrapper.php
602 lines (568 loc) · 15.6 KB
/
FileStreamWrapper.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
<?php
/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2019 Brad Kent
* @version v2.3
*/
namespace bdk\Debug;
/**
* Streamwrapper which injects `declare(ticks=1)`
*
* @see http://php.net/manual/en/class.streamwrapper.php
*
* phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
* phpcs:disable Generic.NamingConventions.CamelCapsFunctionName.ScopeNotCamelCaps
*/
class FileStreamWrapper
{
/**
* @var resource
*/
public $context;
private $bufferPrepend = '';
private $declaredTicks = false;
private $filepath;
/**
* @var resource
*/
private $handle;
/**
* @var array paths to exclude from adding tick declaration
*/
private static $pathsExclude = array();
public static $filesModified = array();
/**
* @var string
*/
const PROTOCOL = 'file';
/**
* Register this stream wrapper
*
* @param array $pathsExclude paths/directories to exclude
*
* @return void
*
* @throws \UnexpectedValueException
*/
public static function register($pathsExclude = array())
{
$result = \stream_wrapper_unregister(static::PROTOCOL);
if ($result === false) {
throw new \UnexpectedValueException('Failed to unregister');
}
if ($pathsExclude) {
self::$pathsExclude = $pathsExclude;
}
\stream_wrapper_register(static::PROTOCOL, \get_called_class());
/*
Disable OPcache
a) want to make sure we modify required files
b) don't want to cache modified files
*/
\ini_set('opcache.enable', 0);
}
/**
* Restore previous wrapper
*
* @return void
*
* @throws \UnexpectedValueException
*/
private static function restorePrev()
{
$result = \stream_wrapper_restore(static::PROTOCOL);
if ($result === false) {
throw new \UnexpectedValueException('Failed to restore');
}
}
/**
* Close the directory
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.dir-closedir.php
*/
public function dir_closedir()
{
if (!$this->handle) {
return false;
}
self::restorePrev();
\closedir($this->handle);
self::register();
$this->handle = null;
return true;
}
/**
* Opens a directory for reading
*
* @param string $path Specifies the URL that was passed to opendir().
* @param integer $options Whether or not to enforce safe_mode (0x04).
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.dir-opendir.php
*/
public function dir_opendir($path, $options = 0)
{
if ($this->handle) {
return false;
}
// "use" our function params so things don't complain
array($options);
self::restorePrev();
$this->handle = \opendir($path);
self::register();
return $this->handle !== false;
}
/**
* Read a single filename of a directory
*
* @return string|boolean
*
* @see http://php.net/manual/en/streamwrapper.dir-readdir.php
*/
public function dir_readdir()
{
if (!$this->handle) {
return false;
}
self::restorePrev();
$success = \readdir($this->handle);
self::register();
return $success;
}
/**
* Reset directory name pointer
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.dir-rewinddir.php
*/
public function dir_rewinddir()
{
if (!$this->handle) {
return false;
}
self::restorePrev();
\rewinddir($this->handle);
self::register();
return true;
}
/**
* Create a directory
*
* @param string $path Directory which should be created.
* @param integer $mode The value passed to mkdir().
* @param integer $options A bitwise mask of values, such as STREAM_MKDIR_RECURSIVE.
*
* @return boolean
*/
public function mkdir($path, $mode, $options = 0)
{
self::restorePrev();
$success = \mkdir($path, $mode, (bool) ($options & STREAM_MKDIR_RECURSIVE));
self::register();
return $success;
}
/**
* Rename a file
*
* @param string $pathFrom existing path
* @param string $pathTo The URL which the path_from should be renamed to.
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.rename.php
*/
public function rename($pathFrom, $pathTo)
{
self::restorePrev();
$success = \rename($pathFrom, $pathTo);
self::register();
return $success;
}
/**
* Remove a directory
*
* @param string $path directory to remove
* @param integer $options bitwise mask of values
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.rmdir.php
*/
public function rmdir($path, $options)
{
// "use" our function params so things don't complain
array($options);
self::restorePrev();
$success = \rmdir($path);
self::register();
return $success;
}
/**
* Retrieve the underlying resource
*
* @param integer $castAs STREAM_CAST_FOR_SELECT when stream_select() is calling stream_cast()
* STREAM_CAST_AS_STREAM when stream_cast() is called for other uses
*
* @return resource|boolean
*
* @see http://php.net/manual/en/streamwrapper.stream-cast.php
*/
public function stream_cast($castAs)
{
if ($this->handle && $castAs & STREAM_CAST_AS_STREAM) {
return $this->handle;
}
return false;
}
/**
* Close a file
*
* @see http://php.net/manual/en/streamwrapper.stream-close.php
*
* @return void
*/
public function stream_close()
{
if (!$this->handle) {
return;
}
self::restorePrev();
\fclose($this->handle);
$this->handle = null;
self::register();
}
/**
* Tests for end-of-file on a file pointer
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.stream-eof.php
*/
public function stream_eof()
{
if (!$this->handle) {
return false;
}
self::restorePrev();
$result = \feof($this->handle);
self::register();
return $result;
}
/**
* Flush the output
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.stream-flush.php
*/
public function stream_flush()
{
if (!$this->handle) {
return false;
}
self::restorePrev();
$success = \fflush($this->handle);
self::register();
return $success;
}
/**
* Advisory file locking
*
* @param integer $operation is one of the following:
* LOCK_SH to acquire a shared lock (reader).
* LOCK_EX to acquire an exclusive lock (writer).
* LOCK_UN to release a lock (shared or exclusive).
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.stream-lock.php
*/
public function stream_lock($operation)
{
if (!$this->handle) {
return false;
}
self::restorePrev();
$success = \flock($this->handle, $operation);
self::register();
return $success;
}
/**
* Change file options
*
* @param string $path filepath or URL
* @param integer $option What meta value is being set
* @param mixed $value Meta value
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.stream-metadata.php
*/
public function stream_metadata($path, $option, $value)
{
self::restorePrev();
switch ($option) {
case STREAM_META_TOUCH:
if (!empty($value)) {
$success = \touch($path, $value[0], $value[1]);
} else {
$success = \touch($path);
}
break;
case STREAM_META_OWNER_NAME:
// Fall through
case STREAM_META_OWNER:
$success = \chown($path, $value);
break;
case STREAM_META_GROUP_NAME:
// Fall through
case STREAM_META_GROUP:
$success = \chgrp($path, $value);
break;
case STREAM_META_ACCESS:
$success = \chmod($path, $value);
break;
default:
$success = false;
}
self::register();
return $success;
}
/**
* Opens file or URL
*
* @param string $path Specifies the file/URL that was passed to the original function.
* @param string $mode The mode used to open the file, as detailed for fopen().
* @param integers $options Holds additional flags set by the streams API. I
* @param string $openedPath the full path of the file/resource that was actually opened
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.stream-open.php
* @throws \UnexpectedValueException
*/
public function stream_open($path, $mode, $options, &$openedPath)
{
if ($this->handle) {
return false;
}
$useIncludePath = (bool) $options & STREAM_USE_PATH;
$context = $this->context;
if ($context === null) {
$context = \stream_context_get_default();
}
self::restorePrev();
if (\strpos($mode, 'r') !== false && !\file_exists($path)) {
return false;
} elseif (\strpos($mode, 'x') !== false && \file_exists($path)) {
return false;
}
$handle = \fopen($path, $mode, $useIncludePath, $context);
self::register();
if ($handle === false) {
return false;
}
/*
Determine opened path
*/
$meta = \stream_get_meta_data($handle);
if (!isset($meta['uri'])) {
throw new \UnexpectedValueException('Uri not in meta data');
}
$this->filepath = $openedPath = $meta['uri'];
$this->handle = $handle;
return true;
}
/**
* Read from stream
*
* @param integer $count How many bytes of data from the current position should be returned.
*
* @return string
*
* @see http://php.net/manual/en/streamwrapper.stream-read.php
*/
public function stream_read($count)
{
if (!$this->handle) {
return false;
}
self::restorePrev();
$buffer = \fread($this->handle, $count);
$bufferLen = \strlen($buffer);
$backtrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$isRequire = !\in_array($backtrace[1]['function'], array('file_get_contents'));
if (!$this->declaredTicks && $isRequire) {
foreach (self::$pathsExclude as $excludePath) {
if (\strpos($this->filepath, $excludePath . DIRECTORY_SEPARATOR) === 0) {
$this->declaredTicks = true;
}
}
}
if (!$this->declaredTicks && $isRequire) {
// insert declare(ticks=1); without adding any new lines
$buffer = \preg_replace(
'/^(<\?php\s*)$/m',
'$0 declare(ticks=1);',
$buffer,
1
);
$this->declaredTicks = true;
self::$filesModified[] = $this->filepath;
}
$buffer = $this->bufferPrepend . $buffer;
$bufferLenAfter = \strlen($buffer);
$diff = $bufferLenAfter - $bufferLen;
$this->bufferPrepend = '';
if ($diff) {
$this->bufferPrepend = \substr($buffer, $count);
$buffer = \substr($buffer, 0, $count);
}
self::register();
return $buffer;
}
/**
* Seek to specific location in a stream
*
* @param integer $offset The stream offset to seek to
* @param integer $whence [SEEK_SET] | SEEK_CUR | SEEK_END
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.stream-seek.php
*/
public function stream_seek($offset, $whence = SEEK_SET)
{
if (!$this->handle) {
return false;
}
self::restorePrev();
$result = \fseek($this->handle, $offset, $whence);
$success = $result !== -1;
self::register();
return $success;
}
/**
* Retrieve information about a file resource
*
* @return array
*
* @see http://php.net/manual/en/streamwrapper.stream-stat.php
*/
public function stream_stat()
{
if (!$this->handle) {
return false;
}
self::restorePrev();
$array = \fstat($this->handle);
self::register();
return $array;
}
/**
* Retrieve the current position of a stream
*
* @return integer
*
* @see http://php.net/manual/en/streamwrapper.stream-tell.php
*/
public function stream_tell()
{
if (!$this->handle) {
return false;
}
self::restorePrev();
$position = \ftell($this->handle);
self::register();
return $position;
}
/**
* Truncates a file to the given size
*
* @param integer $size Truncate to this size
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.stream-truncate.php
*/
public function stream_truncate($size)
{
if (!$this->handle) {
return false;
}
self::restorePrev();
$success = \ftruncate($this->handle, $size);
self::register();
return $success;
}
/**
* Write to stream
*
* @param string $data data to write
*
* @return integer
*
* @see http://php.net/manual/en/streamwrapper.stream-write.php
*/
public function stream_write($data)
{
if (!$this->handle) {
return false;
}
self::restorePrev();
$length = \fwrite($this->handle, $data);
self::register();
return $length;
}
/**
* Unlink a file
*
* @param string $path filepath
*
* @return boolean
*
* @see http://php.net/manual/en/streamwrapper.unlink.php
*/
public function unlink($path)
{
self::restorePrev();
$success = \unlink($path);
self::register();
return $success;
}
/**
* Retrieve information about a file
*
* @param string $path The file path or URL to stat
* @param integer $flags Holds additional flags set by the streams API.
*
* @return array
*
* @see http://php.net/manual/en/streamwrapper.url-stat.php
*/
public function url_stat($path, $flags)
{
self::restorePrev();
if (!\file_exists($path)) {
$info = false;
} elseif ($flags & STREAM_URL_STAT_LINK) {
$info = $flags & STREAM_URL_STAT_QUIET
? @\lstat($path)
: \lstat($path);
} else {
$info = $flags & STREAM_URL_STAT_QUIET
? @\stat($path)
: \stat($path);
}
self::register();
return $info;
}
}