blob: 36e0d3cb99578094bbca06ec1cd62052cfa6bb84 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\VarDumper\Caster;
13
14/**
15 * Represents a file or a URL.
16 *
17 * @author Nicolas Grekas <p@tchwork.com>
18 */
19class LinkStub extends ConstStub
20{
21 public $inVendor = false;
22
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010023 private static array $vendorRoots;
24 private static array $composerRoots = [];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020025
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 Benkard1ba53812022-12-27 17:32:58 +010068 if (!isset(self::$vendorRoots)) {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020069 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}