FileLinkFormatter.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\ErrorHandler\ErrorRenderer;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. /**
  15. * Formats debug file links.
  16. *
  17. * @author Jérémy Romey <jeremy@free-agent.fr>
  18. *
  19. * @final
  20. */
  21. class FileLinkFormatter
  22. {
  23. private array|false $fileLinkFormat;
  24. private ?RequestStack $requestStack = null;
  25. private ?string $baseDir = null;
  26. private \Closure|string|null $urlFormat;
  27. /**
  28. * @param string|\Closure $urlFormat The URL format, or a closure that returns it on-demand
  29. */
  30. public function __construct(string|array|null $fileLinkFormat = null, ?RequestStack $requestStack = null, ?string $baseDir = null, string|\Closure|null $urlFormat = null)
  31. {
  32. $fileLinkFormat ??= $_ENV['SYMFONY_IDE'] ?? $_SERVER['SYMFONY_IDE'] ?? '';
  33. if (!\is_array($f = $fileLinkFormat)) {
  34. $f = (ErrorRendererInterface::IDE_LINK_FORMATS[$f] ?? $f) ?: \ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: 'file://%f#L%l';
  35. $i = strpos($f, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: \strlen($f);
  36. $fileLinkFormat = [substr($f, 0, $i)] + preg_split('/&([^>]++)>/', substr($f, $i), -1, \PREG_SPLIT_DELIM_CAPTURE);
  37. }
  38. $this->fileLinkFormat = $fileLinkFormat;
  39. $this->requestStack = $requestStack;
  40. $this->baseDir = $baseDir;
  41. $this->urlFormat = $urlFormat;
  42. }
  43. /**
  44. * @return string|false
  45. */
  46. public function format(string $file, int $line): string|bool
  47. {
  48. if ($fmt = $this->getFileLinkFormat()) {
  49. for ($i = 1; isset($fmt[$i]); ++$i) {
  50. if (str_starts_with($file, $k = $fmt[$i++])) {
  51. $file = substr_replace($file, $fmt[$i], 0, \strlen($k));
  52. break;
  53. }
  54. }
  55. return strtr($fmt[0], ['%f' => $file, '%l' => $line]);
  56. }
  57. return false;
  58. }
  59. /**
  60. * @internal
  61. */
  62. public function __sleep(): array
  63. {
  64. $this->fileLinkFormat = $this->getFileLinkFormat();
  65. return ['fileLinkFormat'];
  66. }
  67. /**
  68. * @internal
  69. */
  70. public static function generateUrlFormat(UrlGeneratorInterface $router, string $routeName, string $queryString): ?string
  71. {
  72. try {
  73. return $router->generate($routeName).$queryString;
  74. } catch (\Throwable) {
  75. return null;
  76. }
  77. }
  78. private function getFileLinkFormat(): array|false
  79. {
  80. if ($this->fileLinkFormat) {
  81. return $this->fileLinkFormat;
  82. }
  83. if ($this->requestStack && $this->baseDir && $this->urlFormat) {
  84. $request = $this->requestStack->getMainRequest();
  85. if ($request instanceof Request && (!$this->urlFormat instanceof \Closure || $this->urlFormat = ($this->urlFormat)())) {
  86. return [
  87. $request->getSchemeAndHttpHost().$this->urlFormat,
  88. $this->baseDir.\DIRECTORY_SEPARATOR, '',
  89. ];
  90. }
  91. }
  92. return false;
  93. }
  94. }
  95. if (!class_exists(\Symfony\Component\HttpKernel\Debug\FileLinkFormatter::class, false)) {
  96. class_alias(FileLinkFormatter::class, \Symfony\Component\HttpKernel\Debug\FileLinkFormatter::class);
  97. }