24f360b8f5553db21282c7fd53dc3aada8e2a7e9
[yaffs-website] / vendor / symfony / var-dumper / Caster / LinkStub.php
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 $vendorRoots;
24     private static $composerRoots;
25
26     public function __construct($label, $line = 0, $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 (0 === strpos($href, 'file://')) {
37             if ($href === $label) {
38                 $label = substr($label, 7);
39             }
40             $href = substr($href, 7);
41         } elseif (false !== strpos($href, '://')) {
42             $this->attr['href'] = $href;
43
44             return;
45         }
46         if (!file_exists($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($file, &$inVendor)
67     {
68         if (null === self::$vendorRoots) {
69             self::$vendorRoots = array();
70
71             foreach (get_declared_classes() as $class) {
72                 if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
73                     $r = new \ReflectionClass($class);
74                     $v = dirname(dirname($r->getFileName()));
75                     if (file_exists($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 = 0 === strpos($file, $root)) {
89                 return $root;
90             }
91         }
92
93         $parent = $dir;
94         while (!@file_exists($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 }