vendor/symfony/http-kernel/Debug/FileLinkFormatter.php line 97

Open in your IDE?
  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\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  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
  21.  */
  22. class FileLinkFormatter
  23. {
  24.     private array|false $fileLinkFormat;
  25.     private ?RequestStack $requestStack null;
  26.     private ?string $baseDir null;
  27.     private \Closure|string|null $urlFormat;
  28.     /**
  29.      * @param string|\Closure $urlFormat the URL format, or a closure that returns it on-demand
  30.      */
  31.     public function __construct(string|array $fileLinkFormat nullRequestStack $requestStack nullstring $baseDir nullstring|\Closure $urlFormat null)
  32.     {
  33.         $fileLinkFormat ??= $_SERVER['SYMFONY_IDE'] ?? '';
  34.         if (!\is_array($fileLinkFormat) && $fileLinkFormat = (ErrorRendererInterface::IDE_LINK_FORMATS[$fileLinkFormat] ?? $fileLinkFormat) ?: \ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: false) {
  35.             $i strpos($f $fileLinkFormat'&'max(strrpos($f'%f'), strrpos($f'%l'))) ?: \strlen($f);
  36.             $fileLinkFormat = [substr($f0$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(string $fileint $line)
  44.     {
  45.         if ($fmt $this->getFileLinkFormat()) {
  46.             for ($i 1; isset($fmt[$i]); ++$i) {
  47.                 if (str_starts_with($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 $routerstring $routeNamestring $queryString): ?string
  68.     {
  69.         try {
  70.             return $router->generate($routeName).$queryString;
  71.         } catch (\Throwable) {
  72.             return null;
  73.         }
  74.     }
  75.     private function getFileLinkFormat(): array|false
  76.     {
  77.         if ($this->fileLinkFormat) {
  78.             return $this->fileLinkFormat;
  79.         }
  80.         if ($this->requestStack && $this->baseDir && $this->urlFormat) {
  81.             $request $this->requestStack->getMainRequest();
  82.             if ($request instanceof Request && (!$this->urlFormat instanceof \Closure || $this->urlFormat = ($this->urlFormat)())) {
  83.                 return [
  84.                     $request->getSchemeAndHttpHost().$this->urlFormat,
  85.                     $this->baseDir.\DIRECTORY_SEPARATOR'',
  86.                 ];
  87.             }
  88.         }
  89.         return false;
  90.     }
  91. }