vendor/symfony/var-dumper/Caster/LinkStub.php line 92

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\VarDumper\Caster;
  11. /**
  12.  * Represents a file or a URL.
  13.  *
  14.  * @author Nicolas Grekas <p@tchwork.com>
  15.  */
  16. class LinkStub extends ConstStub
  17. {
  18.     public $inVendor false;
  19.     private static array $vendorRoots;
  20.     private static array $composerRoots = [];
  21.     public function __construct(string $labelint $line 0string $href null)
  22.     {
  23.         $this->value $label;
  24.         if (!\is_string($href ??= $label)) {
  25.             return;
  26.         }
  27.         if (str_starts_with($href'file://')) {
  28.             if ($href === $label) {
  29.                 $label substr($label7);
  30.             }
  31.             $href substr($href7);
  32.         } elseif (str_contains($href'://')) {
  33.             $this->attr['href'] = $href;
  34.             return;
  35.         }
  36.         if (!is_file($href)) {
  37.             return;
  38.         }
  39.         if ($line) {
  40.             $this->attr['line'] = $line;
  41.         }
  42.         if ($label !== $this->attr['file'] = realpath($href) ?: $href) {
  43.             return;
  44.         }
  45.         if ($composerRoot $this->getComposerRoot($href$this->inVendor)) {
  46.             $this->attr['ellipsis'] = \strlen($href) - \strlen($composerRoot) + 1;
  47.             $this->attr['ellipsis-type'] = 'path';
  48.             $this->attr['ellipsis-tail'] = + ($this->inVendor \strlen(implode(''\array_slice(explode(\DIRECTORY_SEPARATORsubstr($href$this->attr['ellipsis'])), 02))) : 0);
  49.         } elseif (\count($ellipsis explode(\DIRECTORY_SEPARATOR$href))) {
  50.             $this->attr['ellipsis'] = \strlen(implode(''\array_slice($ellipsis, -2)));
  51.             $this->attr['ellipsis-type'] = 'path';
  52.             $this->attr['ellipsis-tail'] = 1;
  53.         }
  54.     }
  55.     private function getComposerRoot(string $filebool &$inVendor)
  56.     {
  57.         if (!isset(self::$vendorRoots)) {
  58.             self::$vendorRoots = [];
  59.             foreach (get_declared_classes() as $class) {
  60.                 if ('C' === $class[0] && str_starts_with($class'ComposerAutoloaderInit')) {
  61.                     $r = new \ReflectionClass($class);
  62.                     $v \dirname($r->getFileName(), 2);
  63.                     if (is_file($v.'/composer/installed.json')) {
  64.                         self::$vendorRoots[] = $v.\DIRECTORY_SEPARATOR;
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.         $inVendor false;
  70.         if (isset(self::$composerRoots[$dir \dirname($file)])) {
  71.             return self::$composerRoots[$dir];
  72.         }
  73.         foreach (self::$vendorRoots as $root) {
  74.             if ($inVendor str_starts_with($file$root)) {
  75.                 return $root;
  76.             }
  77.         }
  78.         $parent $dir;
  79.         while (!@is_file($parent.'/composer.json')) {
  80.             if (!@file_exists($parent)) {
  81.                 // open_basedir restriction in effect
  82.                 break;
  83.             }
  84.             if ($parent === \dirname($parent)) {
  85.                 return self::$composerRoots[$dir] = false;
  86.             }
  87.             $parent \dirname($parent);
  88.         }
  89.         return self::$composerRoots[$dir] = $parent.\DIRECTORY_SEPARATOR;
  90.     }
  91. }