LinkStub.php 3.1 KB

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