Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 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 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 23 | private static array $vendorRoots; |
| 24 | private static array $composerRoots = []; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 25 | |
| 26 | public function __construct(string $label, int $line = 0, string $href = null) |
| 27 | { |
| 28 | $this->value = $label; |
| 29 | |
| 30 | if (null === $href) { |
| 31 | $href = $label; |
| 32 | } |
| 33 | if (!\is_string($href)) { |
| 34 | return; |
| 35 | } |
| 36 | if (str_starts_with($href, 'file://')) { |
| 37 | if ($href === $label) { |
| 38 | $label = substr($label, 7); |
| 39 | } |
| 40 | $href = substr($href, 7); |
| 41 | } elseif (str_contains($href, '://')) { |
| 42 | $this->attr['href'] = $href; |
| 43 | |
| 44 | return; |
| 45 | } |
| 46 | if (!is_file($href)) { |
| 47 | return; |
| 48 | } |
| 49 | if ($line) { |
| 50 | $this->attr['line'] = $line; |
| 51 | } |
| 52 | if ($label !== $this->attr['file'] = realpath($href) ?: $href) { |
| 53 | return; |
| 54 | } |
| 55 | if ($composerRoot = $this->getComposerRoot($href, $this->inVendor)) { |
| 56 | $this->attr['ellipsis'] = \strlen($href) - \strlen($composerRoot) + 1; |
| 57 | $this->attr['ellipsis-type'] = 'path'; |
| 58 | $this->attr['ellipsis-tail'] = 1 + ($this->inVendor ? 2 + \strlen(implode('', \array_slice(explode(\DIRECTORY_SEPARATOR, substr($href, 1 - $this->attr['ellipsis'])), 0, 2))) : 0); |
| 59 | } elseif (3 < \count($ellipsis = explode(\DIRECTORY_SEPARATOR, $href))) { |
| 60 | $this->attr['ellipsis'] = 2 + \strlen(implode('', \array_slice($ellipsis, -2))); |
| 61 | $this->attr['ellipsis-type'] = 'path'; |
| 62 | $this->attr['ellipsis-tail'] = 1; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private function getComposerRoot(string $file, bool &$inVendor) |
| 67 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 68 | if (!isset(self::$vendorRoots)) { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 69 | self::$vendorRoots = []; |
| 70 | |
| 71 | foreach (get_declared_classes() as $class) { |
| 72 | if ('C' === $class[0] && str_starts_with($class, 'ComposerAutoloaderInit')) { |
| 73 | $r = new \ReflectionClass($class); |
| 74 | $v = \dirname($r->getFileName(), 2); |
| 75 | if (is_file($v.'/composer/installed.json')) { |
| 76 | self::$vendorRoots[] = $v.\DIRECTORY_SEPARATOR; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | $inVendor = false; |
| 82 | |
| 83 | if (isset(self::$composerRoots[$dir = \dirname($file)])) { |
| 84 | return self::$composerRoots[$dir]; |
| 85 | } |
| 86 | |
| 87 | foreach (self::$vendorRoots as $root) { |
| 88 | if ($inVendor = str_starts_with($file, $root)) { |
| 89 | return $root; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | $parent = $dir; |
| 94 | while (!@is_file($parent.'/composer.json')) { |
| 95 | if (!@file_exists($parent)) { |
| 96 | // open_basedir restriction in effect |
| 97 | break; |
| 98 | } |
| 99 | if ($parent === \dirname($parent)) { |
| 100 | return self::$composerRoots[$dir] = false; |
| 101 | } |
| 102 | |
| 103 | $parent = \dirname($parent); |
| 104 | } |
| 105 | |
| 106 | return self::$composerRoots[$dir] = $parent.\DIRECTORY_SEPARATOR; |
| 107 | } |
| 108 | } |