FileLinkFormatter.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\HttpKernel\Debug;
  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 $fileLinkFormat;
  24. private $requestStack;
  25. private $baseDir;
  26. private $urlFormat;
  27. /**
  28. * @param string|\Closure $urlFormat the URL format, or a closure that returns it on-demand
  29. */
  30. public function __construct($fileLinkFormat = null, RequestStack $requestStack = null, string $baseDir = null, $urlFormat = null)
  31. {
  32. $fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
  33. if ($fileLinkFormat && !\is_array($fileLinkFormat)) {
  34. $i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: \strlen($f);
  35. $fileLinkFormat = [substr($f, 0, $i)] + preg_split('/&([^>]++)>/', substr($f, $i), -1, \PREG_SPLIT_DELIM_CAPTURE);
  36. }
  37. $this->fileLinkFormat = $fileLinkFormat;
  38. $this->requestStack = $requestStack;
  39. $this->baseDir = $baseDir;
  40. $this->urlFormat = $urlFormat;
  41. }
  42. public function format(string $file, int $line)
  43. {
  44. if ($fmt = $this->getFileLinkFormat()) {
  45. for ($i = 1; isset($fmt[$i]); ++$i) {
  46. if (0 === strpos($file, $k = $fmt[$i++])) {
  47. $file = substr_replace($file, $fmt[$i], 0, \strlen($k));
  48. break;
  49. }
  50. }
  51. return strtr($fmt[0], ['%f' => $file, '%l' => $line]);
  52. }
  53. return false;
  54. }
  55. /**
  56. * @internal
  57. */
  58. public function __sleep(): array
  59. {
  60. $this->fileLinkFormat = $this->getFileLinkFormat();
  61. return ['fileLinkFormat'];
  62. }
  63. /**
  64. * @internal
  65. */
  66. public static function generateUrlFormat(UrlGeneratorInterface $router, string $routeName, string $queryString): ?string
  67. {
  68. try {
  69. return $router->generate($routeName).$queryString;
  70. } catch (\Throwable $e) {
  71. return null;
  72. }
  73. }
  74. private function getFileLinkFormat()
  75. {
  76. if ($this->fileLinkFormat) {
  77. return $this->fileLinkFormat;
  78. }
  79. if ($this->requestStack && $this->baseDir && $this->urlFormat) {
  80. $request = $this->requestStack->getMasterRequest();
  81. if ($request instanceof Request && (!$this->urlFormat instanceof \Closure || $this->urlFormat = ($this->urlFormat)())) {
  82. return [
  83. $request->getSchemeAndHttpHost().$this->urlFormat,
  84. $this->baseDir.\DIRECTORY_SEPARATOR, '',
  85. ];
  86. }
  87. }
  88. return null;
  89. }
  90. }