| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\VarDumper\Caster;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* Represents a file or a URL.
|
|
|
16 |
*
|
|
|
17 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
18 |
*/
|
|
|
19 |
class LinkStub extends ConstStub
|
|
|
20 |
{
|
|
|
21 |
public $inVendor = false;
|
|
|
22 |
|
|
|
23 |
private static array $vendorRoots;
|
|
|
24 |
private static array $composerRoots = [];
|
|
|
25 |
|
|
|
26 |
public function __construct(string $label, int $line = 0, string $href = null)
|
|
|
27 |
{
|
|
|
28 |
$this->value = $label;
|
|
|
29 |
|
|
|
30 |
if (!\is_string($href ??= $label)) {
|
|
|
31 |
return;
|
|
|
32 |
}
|
|
|
33 |
if (str_starts_with($href, 'file://')) {
|
|
|
34 |
if ($href === $label) {
|
|
|
35 |
$label = substr($label, 7);
|
|
|
36 |
}
|
|
|
37 |
$href = substr($href, 7);
|
|
|
38 |
} elseif (str_contains($href, '://')) {
|
|
|
39 |
$this->attr['href'] = $href;
|
|
|
40 |
|
|
|
41 |
return;
|
|
|
42 |
}
|
|
|
43 |
if (!is_file($href)) {
|
|
|
44 |
return;
|
|
|
45 |
}
|
|
|
46 |
if ($line) {
|
|
|
47 |
$this->attr['line'] = $line;
|
|
|
48 |
}
|
|
|
49 |
if ($label !== $this->attr['file'] = realpath($href) ?: $href) {
|
|
|
50 |
return;
|
|
|
51 |
}
|
|
|
52 |
if ($composerRoot = $this->getComposerRoot($href, $this->inVendor)) {
|
|
|
53 |
$this->attr['ellipsis'] = \strlen($href) - \strlen($composerRoot) + 1;
|
|
|
54 |
$this->attr['ellipsis-type'] = 'path';
|
|
|
55 |
$this->attr['ellipsis-tail'] = 1 + ($this->inVendor ? 2 + \strlen(implode('', \array_slice(explode(\DIRECTORY_SEPARATOR, substr($href, 1 - $this->attr['ellipsis'])), 0, 2))) : 0);
|
|
|
56 |
} elseif (3 < \count($ellipsis = explode(\DIRECTORY_SEPARATOR, $href))) {
|
|
|
57 |
$this->attr['ellipsis'] = 2 + \strlen(implode('', \array_slice($ellipsis, -2)));
|
|
|
58 |
$this->attr['ellipsis-type'] = 'path';
|
|
|
59 |
$this->attr['ellipsis-tail'] = 1;
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
private function getComposerRoot(string $file, bool &$inVendor)
|
|
|
64 |
{
|
|
|
65 |
if (!isset(self::$vendorRoots)) {
|
|
|
66 |
self::$vendorRoots = [];
|
|
|
67 |
|
|
|
68 |
foreach (get_declared_classes() as $class) {
|
|
|
69 |
if ('C' === $class[0] && str_starts_with($class, 'ComposerAutoloaderInit')) {
|
|
|
70 |
$r = new \ReflectionClass($class);
|
|
|
71 |
$v = \dirname($r->getFileName(), 2);
|
|
|
72 |
if (is_file($v.'/composer/installed.json')) {
|
|
|
73 |
self::$vendorRoots[] = $v.\DIRECTORY_SEPARATOR;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
$inVendor = false;
|
|
|
79 |
|
|
|
80 |
if (isset(self::$composerRoots[$dir = \dirname($file)])) {
|
|
|
81 |
return self::$composerRoots[$dir];
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
foreach (self::$vendorRoots as $root) {
|
|
|
85 |
if ($inVendor = str_starts_with($file, $root)) {
|
|
|
86 |
return $root;
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
$parent = $dir;
|
|
|
91 |
while (!@is_file($parent.'/composer.json')) {
|
|
|
92 |
if (!@file_exists($parent)) {
|
|
|
93 |
// open_basedir restriction in effect
|
|
|
94 |
break;
|
|
|
95 |
}
|
|
|
96 |
if ($parent === \dirname($parent)) {
|
|
|
97 |
return self::$composerRoots[$dir] = false;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
$parent = \dirname($parent);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
return self::$composerRoots[$dir] = $parent.\DIRECTORY_SEPARATOR;
|
|
|
104 |
}
|
|
|
105 |
}
|