4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Caster;
15 * Represents a file or a URL.
17 * @author Nicolas Grekas <p@tchwork.com>
19 class LinkStub extends ConstStub
21 public $inVendor = false;
23 private static $vendorRoots;
24 private static $composerRoots;
26 public function __construct($label, $line = 0, $href = null)
28 $this->value = $label;
33 if (!\is_string($href)) {
36 if (0 === strpos($href, 'file://')) {
37 if ($href === $label) {
38 $label = substr($label, 7);
40 $href = substr($href, 7);
41 } elseif (false !== strpos($href, '://')) {
42 $this->attr['href'] = $href;
46 if (!file_exists($href)) {
50 $this->attr['line'] = $line;
52 if ($label !== $this->attr['file'] = realpath($href) ?: $href) {
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;
66 private function getComposerRoot($file, &$inVendor)
68 if (null === self::$vendorRoots) {
69 self::$vendorRoots = array();
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;
83 if (isset(self::$composerRoots[$dir = \dirname($file)])) {
84 return self::$composerRoots[$dir];
87 foreach (self::$vendorRoots as $root) {
88 if ($inVendor = 0 === strpos($file, $root)) {
94 while (!@file_exists($parent.'/composer.json')) {
95 if (!@file_exists($parent)) {
96 // open_basedir restriction in effect
99 if ($parent === \dirname($parent)) {
100 return self::$composerRoots[$dir] = false;
103 $parent = \dirname($parent);
106 return self::$composerRoots[$dir] = $parent.\DIRECTORY_SEPARATOR;