Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileLinkFormatter.php 3.1KB

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