Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\VarDumper\Caster; Chris@0: Chris@0: /** Chris@0: * Represents a file or a URL. Chris@0: * Chris@0: * @author Nicolas Grekas Chris@0: */ Chris@0: class LinkStub extends ConstStub Chris@0: { Chris@12: public $inVendor = false; Chris@12: Chris@0: private static $vendorRoots; Chris@0: private static $composerRoots; Chris@0: Chris@0: public function __construct($label, $line = 0, $href = null) Chris@0: { Chris@0: $this->value = $label; Chris@0: Chris@0: if (null === $href) { Chris@0: $href = $label; Chris@0: } Chris@17: if (!\is_string($href)) { Chris@0: return; Chris@0: } Chris@0: if (0 === strpos($href, 'file://')) { Chris@0: if ($href === $label) { Chris@0: $label = substr($label, 7); Chris@0: } Chris@0: $href = substr($href, 7); Chris@0: } elseif (false !== strpos($href, '://')) { Chris@0: $this->attr['href'] = $href; Chris@0: Chris@0: return; Chris@0: } Chris@0: if (!file_exists($href)) { Chris@0: return; Chris@0: } Chris@0: if ($line) { Chris@0: $this->attr['line'] = $line; Chris@0: } Chris@0: if ($label !== $this->attr['file'] = realpath($href) ?: $href) { Chris@0: return; Chris@0: } Chris@12: if ($composerRoot = $this->getComposerRoot($href, $this->inVendor)) { Chris@17: $this->attr['ellipsis'] = \strlen($href) - \strlen($composerRoot) + 1; Chris@0: $this->attr['ellipsis-type'] = 'path'; Chris@17: $this->attr['ellipsis-tail'] = 1 + ($this->inVendor ? 2 + \strlen(implode('', \array_slice(explode(\DIRECTORY_SEPARATOR, substr($href, 1 - $this->attr['ellipsis'])), 0, 2))) : 0); Chris@17: } elseif (3 < \count($ellipsis = explode(\DIRECTORY_SEPARATOR, $href))) { Chris@17: $this->attr['ellipsis'] = 2 + \strlen(implode('', \array_slice($ellipsis, -2))); Chris@0: $this->attr['ellipsis-type'] = 'path'; Chris@0: $this->attr['ellipsis-tail'] = 1; Chris@0: } Chris@0: } Chris@0: Chris@0: private function getComposerRoot($file, &$inVendor) Chris@0: { Chris@0: if (null === self::$vendorRoots) { Chris@17: self::$vendorRoots = []; Chris@0: Chris@0: foreach (get_declared_classes() as $class) { Chris@0: if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) { Chris@0: $r = new \ReflectionClass($class); Chris@17: $v = \dirname(\dirname($r->getFileName())); Chris@0: if (file_exists($v.'/composer/installed.json')) { Chris@17: self::$vendorRoots[] = $v.\DIRECTORY_SEPARATOR; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: $inVendor = false; Chris@0: Chris@17: if (isset(self::$composerRoots[$dir = \dirname($file)])) { Chris@0: return self::$composerRoots[$dir]; Chris@0: } Chris@0: Chris@0: foreach (self::$vendorRoots as $root) { Chris@0: if ($inVendor = 0 === strpos($file, $root)) { Chris@0: return $root; Chris@0: } Chris@0: } Chris@0: Chris@0: $parent = $dir; Chris@0: while (!@file_exists($parent.'/composer.json')) { Chris@0: if (!@file_exists($parent)) { Chris@0: // open_basedir restriction in effect Chris@0: break; Chris@0: } Chris@17: if ($parent === \dirname($parent)) { Chris@0: return self::$composerRoots[$dir] = false; Chris@0: } Chris@0: Chris@17: $parent = \dirname($parent); Chris@0: } Chris@0: Chris@17: return self::$composerRoots[$dir] = $parent.\DIRECTORY_SEPARATOR; Chris@0: } Chris@0: }