comparison vendor/symfony/var-dumper/Caster/LinkStub.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 private static $vendorRoots;
22 private static $composerRoots;
23
24 public function __construct($label, $line = 0, $href = null)
25 {
26 $this->value = $label;
27
28 if (null === $href) {
29 $href = $label;
30 }
31 if (!is_string($href)) {
32 return;
33 }
34 if (0 === strpos($href, 'file://')) {
35 if ($href === $label) {
36 $label = substr($label, 7);
37 }
38 $href = substr($href, 7);
39 } elseif (false !== strpos($href, '://')) {
40 $this->attr['href'] = $href;
41
42 return;
43 }
44 if (!file_exists($href)) {
45 return;
46 }
47 if ($line) {
48 $this->attr['line'] = $line;
49 }
50 if ($label !== $this->attr['file'] = realpath($href) ?: $href) {
51 return;
52 }
53 if ($composerRoot = $this->getComposerRoot($href, $inVendor)) {
54 $this->attr['ellipsis'] = strlen($href) - strlen($composerRoot) + 1;
55 $this->attr['ellipsis-type'] = 'path';
56 $this->attr['ellipsis-tail'] = 1 + ($inVendor ? 2 + strlen(implode(array_slice(explode(DIRECTORY_SEPARATOR, substr($href, 1 - $this->attr['ellipsis'])), 0, 2))) : 0);
57 } elseif (3 < count($ellipsis = explode(DIRECTORY_SEPARATOR, $href))) {
58 $this->attr['ellipsis'] = 2 + strlen(implode(array_slice($ellipsis, -2)));
59 $this->attr['ellipsis-type'] = 'path';
60 $this->attr['ellipsis-tail'] = 1;
61 }
62 }
63
64 private function getComposerRoot($file, &$inVendor)
65 {
66 if (null === self::$vendorRoots) {
67 self::$vendorRoots = array();
68
69 foreach (get_declared_classes() as $class) {
70 if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
71 $r = new \ReflectionClass($class);
72 $v = dirname(dirname($r->getFileName()));
73 if (file_exists($v.'/composer/installed.json')) {
74 self::$vendorRoots[] = $v.DIRECTORY_SEPARATOR;
75 }
76 }
77 }
78 }
79 $inVendor = false;
80
81 if (isset(self::$composerRoots[$dir = dirname($file)])) {
82 return self::$composerRoots[$dir];
83 }
84
85 foreach (self::$vendorRoots as $root) {
86 if ($inVendor = 0 === strpos($file, $root)) {
87 return $root;
88 }
89 }
90
91 $parent = $dir;
92 while (!@file_exists($parent.'/composer.json')) {
93 if (!@file_exists($parent)) {
94 // open_basedir restriction in effect
95 break;
96 }
97 if ($parent === dirname($parent)) {
98 return self::$composerRoots[$dir] = false;
99 }
100
101 $parent = dirname($parent);
102 }
103
104 return self::$composerRoots[$dir] = $parent.DIRECTORY_SEPARATOR;
105 }
106 }