|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE <
10000
/code> |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\ErrorHandler\ErrorRenderer; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 16 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Formats debug file links. |
| 20 | + * |
| 21 | + * @author Jérémy Romey <jeremy@free-agent.fr> |
| 22 | + * |
| 23 | + * @final |
| 24 | + */ |
| 25 | +class FileLinkFormatter |
| 26 | +{ |
| 27 | + private array|false $fileLinkFormat; |
| 28 | + private ?RequestStack $requestStack = null; |
| 29 | + private ?string $baseDir = null; |
| 30 | + private \Closure|string|null $urlFormat; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param string|\Closure $urlFormat the URL format, or a closure that returns it on-demand |
| 34 | + */ |
| 35 | + public function __construct(string|array $fileLinkFormat = null, RequestStack $requestStack = null, string $baseDir = null, string|\Closure $urlFormat = null) |
| 36 | + { |
| 37 | + $fileLinkFormat ??= $_ENV['SYMFONY_IDE'] ?? $_SERVER['SYMFONY_IDE'] ?? ''; |
| 38 | + |
| 39 | + if (!\is_array($f = $fileLinkFormat)) { |
| 40 | + $f = (ErrorRendererInterface::IDE_LINK_FORMATS[$f] ?? $f) ?: \ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: 'file://%f#L%l'; |
| 41 | + $i = strpos($f, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: \strlen($f); |
| 42 | + $fileLinkFormat = [substr($f, 0, $i)] + preg_split('/&([^>]++)>/', substr($f, $i), -1, \PREG_SPLIT_DELIM_CAPTURE); |
| 43 | + } |
| 44 | + |
| 45 | + $this->fileLinkFormat = $fileLinkFormat; |
| 46 | + $this->requestStack = $requestStack; |
| 47 | + $this->baseDir = $baseDir; |
| 48 | + $this->urlFormat = $urlFormat; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @return string|false |
| 53 | + */ |
| 54 | + public function format(string $file, int $line): string|bool |
| 55 | + { |
| 56 | + if ($fmt = $this->getFileLinkFormat()) { |
| 57 | + for ($i = 1; isset($fmt[$i]); ++$i) { |
| 58 | + if (str_starts_with($file, $k = $fmt[$i++])) { |
| 59 | + $file = substr_replace($file, $fmt[$i], 0, \strlen($k)); |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return strtr($fmt[0], ['%f' => $file, '%l' => $line]); |
| 65 | + } |
| 66 | + |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @internal |
| 72 | + */ |
| 73 | + public function __sleep(): array |
| 74 | + { |
| 75 | + $this->fileLinkFormat = $this->getFileLinkFormat(); |
| 76 | + |
| 77 | + return ['fileLinkFormat']; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @internal |
| 82 | + */ |
| 83 | + public static function generateUrlFormat(UrlGeneratorInterface $router, string $routeName, string $queryString): ?string |
| 84 | + { |
| 85 | + try { |
| 86 | + return $router->generate($routeName).$queryString; |
| 87 | + } catch (\Throwable) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private function getFileLinkFormat(): array|false |
| 93 | + { |
| 94 | + if ($this->fileLinkFormat) { |
| 95 | + return $this->fileLinkFormat; |
| 96 | + } |
| 97 | + |
| 98 | + if ($this->requestStack && $this->baseDir && $this->urlFormat) { |
| 99 | + $request = $this->requestStack->getMainRequest(); |
| 100 | + |
| 101 | + if ($request instanceof Request && (!$this->urlFormat instanceof \Closure || $this->urlFormat = ($this->urlFormat)())) { |
| 102 | + return [ |
| 103 | + $request->getSchemeAndHttpHost().$this->urlFormat, |
| 104 | + $this->baseDir.\DIRECTORY_SEPARATOR, '', |
| 105 | + ]; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return false; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +if (!class_exists(\Symfony\Component\HttpKernel\Debug\FileLinkFormatter::class, false)) { |
| 114 | + class_alias(FileLinkFormatter::class, \Symfony\Component\HttpKernel\Debug\FileLinkFormatter::class); |
| 115 | +} |
0 commit comments