-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStreamWrapper.php
More file actions
717 lines (577 loc) · 21.4 KB
/
Copy pathStreamWrapper.php
File metadata and controls
717 lines (577 loc) · 21.4 KB
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<?php
declare(strict_types=1);
namespace TypePHP\Internal;
require_once __DIR__ . '/PathMatcher.php';
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\ParserFactory;
use TypePHP\Resolver\SpecialTypeResolver;
/**
* Custom stream wrapper intercepting 'file://' inclusions to perform on-the-fly AST transformations.
*
* @internal
*/
final class StreamWrapper implements StreamWrapperInterface
{
/**
* Context resource provided by PHP stream subsystem.
*
* @var resource|null
*/
public $context;
/**
* @var resource|null
*/
private $handle = null;
/**
* @var resource|null
*/
private $dirHandle = null;
private static bool $isRegistered = false;
private static bool $cacheEnabled = true;
private static string $cacheDir = '';
/**
* In-memory cache for positive url_stat results.
*
* @var array<string, array<int|string, int>>
*/
private static array $statCache = [];
/**
* In-memory cache for static-path negative misses only (e.g. vendor).
* Never stores negative misses for dynamic paths (var/cache, storage).
*
* @var array<string, true>
*/
private static array $staticNegativeStatCache = [];
/**
* In-memory cache for isApplicationFile path decisions.
*
* @var array<string, bool>
*/
private static array $appFileDecisionCache = [];
/**
* Fast-lookup table for read-only source view functions.
*
* @var array<string, true>
*/
private const READ_ONLY_FUNCTIONS = [
'file_get_contents' => true,
'file' => true,
'readfile' => true,
'highlight_file' => true,
'show_source' => true,
'token_get_all' => true,
];
/**
* Resets all internal caches.
*/
public static function reset(): void
{
self::$statCache = [];
self::$staticNegativeStatCache = [];
self::$appFileDecisionCache = [];
PathMatcher::reset();
}
/**
* Registers the stream wrapper for the native 'file://' protocol.
*
* @param array<string, mixed> $config
*/
public static function register(array $config = []): void
{
$resolvedConfig = array_replace_recursive(Config::get(), $config);
self::$cacheEnabled = (bool) ($resolvedConfig['cache'] ?? true);
self::$cacheDir = CacheManager::getCacheDir();
if (! self::$isRegistered) {
stream_wrapper_unregister('file');
stream_wrapper_register('file', self::class);
self::$isRegistered = true;
}
}
public static function isRegistered(): bool
{
return self::$isRegistered;
}
public static function unregister(): void
{
if (self::$isRegistered) {
@stream_wrapper_restore('file');
self::$isRegistered = false;
}
}
/**
* Transforms PHP source code by parsing AST, extracting metadata, applying ContractVisitor, and formatting output.
* Preserves exact line numbers to guarantee zero line-drift in debug stack traces.
*/
public static function transformSource(string $source, string $filePath = ''): string
{
if (Config::isRespectIgnoreTagsEnabled() && (str_contains($source, '@typephp-ignore-file') || str_contains($source, '@typephp-disable-file'))) {
return $source;
}
$originalLineCount = substr_count($source, "\n");
$parser = (new ParserFactory())->createForNewestSupportedVersion();
try {
$oldStmts = $parser->parse($source);
if ($oldStmts === null) {
return $source;
}
} catch (\Throwable $e) {
return $source;
}
self::extractAndSeedFileMetadata($oldStmts, $filePath);
$oldTokens = $parser->getTokens();
$traverser1 = new NodeTraverser();
$traverser1->addVisitor(new CloningVisitor());
/** @var array<\PhpParser\Node\Stmt> $nodesToTraverse */
$nodesToTraverse = $oldStmts;
$newStmts = $traverser1->traverse($nodesToTraverse);
$traverser2 = new NodeTraverser();
$traverser2->addVisitor(new ContractVisitor());
$newStmts = $traverser2->traverse($newStmts);
$printer = new TypePHPPrinter();
$transformed = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
$transformed = preg_replace('/[ \t]*\r?\n[ \t]*\/\*__TYPEPHP_INJECTED_START__\*\//', ' /*__TYPEPHP_INJECTED_START__*/', $transformed) ?? $transformed;
$transformedLineCount = substr_count($transformed, "\n");
$drift = $transformedLineCount - $originalLineCount;
$count1 = 0;
$count2 = 0;
if ($drift > 0) {
$transformed = preg_replace('/[ \t]*\r?\n[ \t]*\{[ \t]*\/\*__TYPEPHP_INJECTED_START__\*\//', ' { /*__TYPEPHP_INJECTED_START__*/', $transformed, $drift, $count1) ?? $transformed;
$drift -= $count1;
}
if ($drift > 0) {
$transformed = preg_replace('/\/\*__TYPEPHP_INJECTED_END__\*\/[ \t]*\r?\n[ \t]*\}/', '/*__TYPEPHP_INJECTED_END__*/ }', $transformed, $drift, $count2) ?? $transformed;
$drift -= $count2;
}
if ($drift > 0) {
$transformed = preg_replace('/\/\*__TYPEPHP_INJECTED_END__\*\/[ \t]*\r?\n[ \t]*/', '/*__TYPEPHP_INJECTED_END__*/ ', $transformed, $drift) ?? $transformed;
}
return str_replace(['/*__TYPEPHP_INJECTED_START__*/', '/*__TYPEPHP_INJECTED_END__*/'], '', $transformed);
}
/**
* Opens a file stream, intercepting matching application files for AST transformation.
* Evaluates string fast-paths before checking file existence or unregistering the wrapper.
*/
public function stream_open(string $path, string $mode, int $options, ?string &$openedPath): bool
{
if ($mode !== 'r' && $mode !== 'rb' && $mode !== 'rt') {
return $this->openDirectHandle($path, $mode);
}
if (! str_ends_with(strtolower($path), '.php')) {
return $this->openDirectHandle($path, $mode);
}
if (! Config::isEnabled()) {
return $this->openDirectHandle($path, $mode);
}
$normalizedRaw = str_replace('\\', '/', $path);
if (! PathMatcher::mayPathBeIncluded($normalizedRaw)) {
return $this->openDirectHandle($path, $mode);
}
if (isset(self::$appFileDecisionCache[$normalizedRaw])) {
if (! self::$appFileDecisionCache[$normalizedRaw]) {
return $this->openDirectHandle($path, $mode);
}
}
self::unregister();
$exists = (bool) self::silent(fn () => file_exists($path));
$resolvedPath = $exists ? self::silent(fn () => realpath($path)) : false;
self::register();
if (! $exists || $resolvedPath === false) {
return $this->openDirectHandle($path, $mode);
}
$normalizedResolved = str_replace('\\', '/', $resolvedPath);
if (! self::isApplicationFile($path, $resolvedPath)) {
return $this->openDirectHandle($normalizedResolved, $mode);
}
if (self::isReadOnlyCall()) {
return $this->openDirectHandle($normalizedResolved, $mode);
}
self::unregister();
$success = self::$cacheEnabled
? $this->openCachedStream($resolvedPath, $mode)
: $this->openMemoryStream($resolvedPath);
self::register();
return $success;
}
/**
* Determines if the stream_open call is for reading raw file contents/snippets
* (e.g. error screen renderers) rather than PHP engine execution.
*/
private static function isReadOnlyCall(): bool
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
$caller1 = strtolower($trace[1]['function'] ?? '');
$caller2 = strtolower($trace[2]['function'] ?? '');
return isset(self::READ_ONLY_FUNCTIONS[$caller1]) || isset(self::READ_ONLY_FUNCTIONS[$caller2]);
}
/**
* Opens a raw file handle directly without stream interception.
*/
private function openDirectHandle(string $targetFile, string $mode): bool
{
self::unregister();
/** @var resource|false $handle */
$handle = self::silent(fn () => fopen($targetFile, $mode));
$this->handle = $handle !== false ? $handle : null;
self::register();
return $this->handle !== null;
}
public function stream_read(int $count): string
{
if ($this->handle === null || $count <= 0) {
return '';
}
$res = fread($this->handle, $count);
return $res !== false ? $res : '';
}
public function stream_write(string $data): int
{
if ($this->handle === null) {
return 0;
}
$res = fwrite($this->handle, $data);
return $res !== false ? $res : 0;
}
public function stream_lock(int $operation): bool
{
if ($this->handle === null) {
return false;
}
if ($operation < 1 || $operation > 15) {
return true;
}
// @phpstan-ignore argument.type
return @flock($this->handle, $operation);
}
public function stream_tell(): int
{
if ($this->handle === null) {
return 0;
}
$res = ftell($this->handle);
return $res !== false ? $res : 0;
}
public function stream_flush(): bool
{
if ($this->handle === null) {
return false;
}
return fflush($this->handle);
}
public function stream_truncate(int $new_size): bool
{
if ($this->handle === null || $new_size < 0) {
return false;
}
return ftruncate($this->handle, $new_size);
}
public function stream_eof(): bool
{
if ($this->handle === null) {
return true;
}
return feof($this->handle);
}
/**
* @return array<int|string, int>|false
*/
public function stream_stat(): array|false
{
if ($this->handle === null) {
return false;
}
return fstat($this->handle);
}
/**
* Extracts the underlying native file descriptor for C-level functions like proc_open() or stream_select().
*
* @return resource|false
*/
public function stream_cast(int $cast_as)
{
return $this->handle !== null ? $this->handle : false;
}
public function stream_seek(int $offset, int $whence = SEEK_SET): bool
{
if ($this->handle === null) {
return false;
}
return @fseek($this->handle, $offset, $whence) === 0;
}
public function stream_set_option(int $option, int $arg1, int $arg2): bool
{
return false;
}
public function stream_close(): void
{
if ($this->handle !== null) {
fclose($this->handle);
$this->handle = null;
}
}
/**
* High-speed stat resolution with dual-tier memoization caching:
*
* 1. Positive hit cache ($statCache): Caches stat arrays for existing files and directories.
* 2. Static negative cache ($staticNegativeStatCache): Caches false lookups strictly for static
* read-only paths (e.g. vendor directories), eliminating thousands of duplicate C-level stat calls.
* 3. Dynamic writable bypass: Never caches false for dynamic directories (var/cache, storage),
* ensuring framework cache warmers and runtime directory creation remain fully functional.
*
* @return array<int|string, int>|false
*/
public function url_stat(string $path, int $flags): array|false
{
$normalized = str_replace('\\', '/', $path);
if (isset(self::$statCache[$normalized])) {
return self::$statCache[$normalized];
}
if (isset(self::$staticNegativeStatCache[$normalized])) {
return false;
}
self::unregister();
/** @var array<int|string, int>|false $result */
$result = self::silent(fn () => stat($path));
self::register();
if ($result !== false) {
self::$statCache[$normalized] = $result;
} else {
if (! PathMatcher::isDynamicWritablePath($normalized)) {
self::$staticNegativeStatCache[$normalized] = true;
}
}
return $result;
}
public function stream_metadata(string $path, int $option, mixed $value): bool
{
$normalized = str_replace('\\', '/', $path);
unset(self::$statCache[$normalized], self::$staticNegativeStatCache[$normalized]);
self::unregister();
$result = false;
if ($option === STREAM_META_TOUCH) {
/** @var array{0?: int, 1?: int} $valueArray */
$valueArray = \is_array($value) ? $value : [];
$time = $valueArray[0] ?? time();
$atime = $valueArray[1] ?? $time;
$result = (bool) self::silent(fn () => touch($path, (int) $time, (int) $atime));
} elseif ($option === STREAM_META_ACCESS) {
/** @var int $mode */
$mode = \is_int($value) ? $value : 0777;
$result = (bool) self::silent(fn () => chmod($path, $mode));
}
self::register();
return $result;
}
public function dir_opendir(string $path, int $options): bool
{
self::unregister();
/** @var resource|false $dh */
$dh = self::silent(fn () => opendir($path));
$this->dirHandle = $dh !== false ? $dh : null;
self::register();
return $this->dirHandle !== null;
}
public function dir_readdir(): string|false
{
if ($this->dirHandle === null) {
return false;
}
return readdir($this->dirHandle);
}
public function dir_rewinddir(): bool
{
if ($this->dirHandle === null) {
return false;
}
rewinddir($this->dirHandle);
return true;
}
public function dir_closedir(): bool
{
if ($this->dirHandle !== null) {
closedir($this->dirHandle);
$this->dirHandle = null;
}
return true;
}
public function mkdir(string $path, int $mode, int $options): bool
{
$normalized = str_replace('\\', '/', $path);
unset(self::$statCache[$normalized], self::$staticNegativeStatCache[$normalized]);
self::unregister();
$result = (bool) self::silent(fn () => mkdir($path, $mode, ($options & STREAM_MKDIR_RECURSIVE) !== 0));
self::register();
return $result;
}
public function rmdir(string $path, int $options): bool
{
$normalized = str_replace('\\', '/', $path);
unset(self::$statCache[$normalized], self::$staticNegativeStatCache[$normalized]);
self::unregister();
$result = (bool) self::silent(fn () => rmdir($path));
self::register();
return $result;
}
public function unlink(string $path): bool
{
$normalized = str_replace('\\', '/', $path);
unset(self::$statCache[$normalized], self::$staticNegativeStatCache[$normalized]);
self::unregister();
$result = (bool) self::silent(fn () => unlink($path));
self::register();
return $result;
}
public function rename(string $pathFrom, string $pathTo): bool
{
$normFrom = str_replace('\\', '/', $pathFrom);
$normTo = str_replace('\\', '/', $pathTo);
unset(
self::$statCache[$normFrom],
self::$statCache[$normTo],
self::$staticNegativeStatCache[$normFrom],
self::$staticNegativeStatCache[$normTo]
);
self::unregister();
$result = (bool) self::silent(fn () => rename($pathFrom, $pathTo));
self::register();
return $result;
}
/**
* Executes a callback while temporarily suppressing PHP error and warning handlers.
*
* @template T
*
* @param callable(): T $callback
*
* @return T
*/
private static function silent(callable $callback): mixed
{
set_error_handler(fn () => true);
try {
return $callback();
} finally {
restore_error_handler();
}
}
/**
* Determines whether a target PHP file path should be intercepted with $O(1)$ caching.
*/
private static function isApplicationFile(string $path, string|false $resolvedPath): bool
{
if (! Config::isEnabled()) {
return false;
}
if (! str_ends_with($path, '.php') || $resolvedPath === false) {
return false;
}
$normalizedPath = PathMatcher::normalizePath($resolvedPath);
if (isset(self::$appFileDecisionCache[$normalizedPath])) {
return self::$appFileDecisionCache[$normalizedPath];
}
if (PathMatcher::isLibraryInternal($normalizedPath)) {
return self::$appFileDecisionCache[$normalizedPath] = false;
}
if (PathMatcher::isCachePath($normalizedPath)) {
return self::$appFileDecisionCache[$normalizedPath] = false;
}
$config = Config::get();
/** @var array<int, string> $includes */
$includes = \is_array($config['include'] ?? null) ? $config['include'] : ['**'];
/** @var array<int, string> $excludes */
$excludes = \is_array($config['exclude'] ?? null) ? $config['exclude'] : ['vendor/**', 'storage/**', 'var/**', 'cache/**'];
$isIncluded = PathMatcher::isPathIncluded($normalizedPath, $includes, $excludes, $path);
return self::$appFileDecisionCache[$normalizedPath] = $isIncluded;
}
private function openMemoryStream(string $resolvedPath): bool
{
$source = file_get_contents($resolvedPath);
if ($source === false) {
return false;
}
$transformed = self::transformSource($source, $resolvedPath);
$memHandle = fopen('php://memory', 'r+');
if ($memHandle !== false) {
fwrite($memHandle, $transformed);
rewind($memHandle);
$this->handle = $memHandle;
}
return $this->handle !== null;
}
/**
* Transforms and caches source code on disk before opening a file handle.
*/
private function openCachedStream(string $resolvedPath, string $mode): bool
{
$cacheDir = self::$cacheDir;
if (! is_dir($cacheDir)) {
self::silent(fn () => mkdir($cacheDir, 0777, true));
}
$cachedFile = CacheManager::getCachedFilePath($resolvedPath);
if (! file_exists($cachedFile)) {
$source = file_get_contents($resolvedPath);
if ($source !== false) {
$transformed = self::transformSource($source, $resolvedPath);
file_put_contents($cachedFile, $transformed);
}
}
$cacheHandle = fopen($cachedFile, $mode);
$this->handle = $cacheHandle !== false ? $cacheHandle : null;
return $this->handle !== null;
}
/**
* Scans top-level AST statements for namespace, use imports, and trait use declarations to seed SpecialTypeResolver.
*
* @param array<\PhpParser\Node\Stmt> $stmts
*/
private static function extractAndSeedFileMetadata(array $stmts, string $filePath): void
{
if ($filePath === '') {
return;
}
$namespace = '';
$imports = [];
$classTraitUseDocs = [];
$nodesToScan = $stmts;
foreach ($stmts as $stmt) {
if ($stmt instanceof \PhpParser\Node\Stmt\Namespace_) {
$namespace = $stmt->name !== null ? $stmt->name->toString() : '';
$nodesToScan = $stmt->stmts;
break;
}
}
foreach ($nodesToScan as $stmt) {
if ($stmt instanceof \PhpParser\Node\Stmt\Use_) {
if ($stmt->type !== \PhpParser\Node\Stmt\Use_::TYPE_NORMAL) {
continue;
}
foreach ($stmt->uses as $use) {
$fqcn = $use->name->toString();
$alias = $use->getAlias()->toString();
$imports[$alias] = $fqcn;
}
} elseif ($stmt instanceof \PhpParser\Node\Stmt\GroupUse) {
$prefix = $stmt->prefix->toString();
foreach ($stmt->uses as $use) {
if ($use->type !== \PhpParser\Node\Stmt\Use_::TYPE_NORMAL && $use->type !== \PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN && $stmt->type !== \PhpParser\Node\Stmt\Use_::TYPE_NORMAL) {
continue;
}
$fqcn = $prefix . '\\' . $use->name->toString();
$alias = $use->getAlias()->toString();
$imports[$alias] = $fqcn;
}
} elseif ($stmt instanceof \PhpParser\Node\Stmt\Class_ && $stmt->name !== null) {
$className = $namespace !== '' ? $namespace . '\\' . $stmt->name->toString() : $stmt->name->toString();
foreach ($stmt->stmts as $classStmt) {
if ($classStmt instanceof \PhpParser\Node\Stmt\TraitUse) {
$doc = $classStmt->getDocComment();
if ($doc !== null) {
$classTraitUseDocs[$className][] = $doc->getText();
}
}
}
}
}
SpecialTypeResolver::seedFileMetadata($filePath, $namespace, $imports, $classTraitUseDocs);
}
}