LinkStub.php 3.3 KB

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