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