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

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\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 const FORMATS = [
  24.         'textmate' => 'txmt://open?url=file://%f&line=%l',
  25.         'macvim' => 'mvim://open?url=file://%f&line=%l',
  26.         'emacs' => 'emacs://open?url=file://%f&line=%l',
  27.         'sublime' => 'subl://open?url=file://%f&line=%l',
  28.         'phpstorm' => 'phpstorm://open?file=%f&line=%l',
  29.         'atom' => 'atom://core/open/file?filename=%f&line=%l',
  30.         'vscode' => 'vscode://file/%f:%l',
  31.     ];
  32.     private $fileLinkFormat;
  33.     private $requestStack;
  34.     private $baseDir;
  35.     private $urlFormat;
  36.     /**
  37.      * @param string|array|null $fileLinkFormat
  38.      * @param string|\Closure   $urlFormat      the URL format, or a closure that returns it on-demand
  39.      */
  40.     public function __construct($fileLinkFormat nullRequestStack $requestStack nullstring $baseDir null$urlFormat null)
  41.     {
  42.         if (!\is_array($fileLinkFormat) && $fileLinkFormat = (self::FORMATS[$fileLinkFormat] ?? $fileLinkFormat) ?: \ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format')) {
  43.             $i strpos($f $fileLinkFormat'&'max(strrpos($f'%f'), strrpos($f'%l'))) ?: \strlen($f);
  44.             $fileLinkFormat = [substr($f0$i)] + preg_split('/&([^>]++)>/'substr($f$i), -1, \PREG_SPLIT_DELIM_CAPTURE);
  45.         }
  46.         $this->fileLinkFormat $fileLinkFormat;
  47.         $this->requestStack $requestStack;
  48.         $this->baseDir $baseDir;
  49.         $this->urlFormat $urlFormat;
  50.     }
  51.     public function format(string $fileint $line)
  52.     {
  53.         if ($fmt $this->getFileLinkFormat()) {
  54.             for ($i 1; isset($fmt[$i]); ++$i) {
  55.                 if (str_starts_with($file$k $fmt[$i++])) {
  56.                     $file substr_replace($file$fmt[$i], 0, \strlen($k));
  57.                     break;
  58.                 }
  59.             }
  60.             return strtr($fmt[0], ['%f' => $file'%l' => $line]);
  61.         }
  62.         return false;
  63.     }
  64.     /**
  65.      * @internal
  66.      */
  67.     public function __sleep(): array
  68.     {
  69.         $this->fileLinkFormat $this->getFileLinkFormat();
  70.         return ['fileLinkFormat'];
  71.     }
  72.     /**
  73.      * @internal
  74.      */
  75.     public static function generateUrlFormat(UrlGeneratorInterface $routerstring $routeNamestring $queryString): ?string
  76.     {
  77.         try {
  78.             return $router->generate($routeName).$queryString;
  79.         } catch (\Throwable $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->getMainRequest();
  90.             if ($request instanceof Request && (!$this->urlFormat instanceof \Closure || $this->urlFormat = ($this->urlFormat)())) {
  91.                 return [
  92.                     $request->getSchemeAndHttpHost().$this->urlFormat,
  93.                     $this->baseDir.\DIRECTORY_SEPARATOR'',
  94.                 ];
  95.             }
  96.         }
  97.         return null;
  98.     }
  99. }