FileLinkFormatter.php
91 lines
| 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 |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\HttpKernel\Debug; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Request; |
| 14 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\RequestStack; |
| 15 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 16 | /** |
| 17 | * Formats debug file links. |
| 18 | * |
| 19 | * @author Jérémy Romey <jeremy@free-agent.fr> |
| 20 | * |
| 21 | * @final |
| 22 | */ |
| 23 | class FileLinkFormatter |
| 24 | { |
| 25 | private const FORMATS = ['textmate' => 'txmt://open?url=file://%f&line=%l', 'macvim' => 'mvim://open?url=file://%f&line=%l', 'emacs' => 'emacs://open?url=file://%f&line=%l', 'sublime' => 'subl://open?url=file://%f&line=%l', 'phpstorm' => 'phpstorm://open?file=%f&line=%l', 'atom' => 'atom://core/open/file?filename=%f&line=%l', 'vscode' => 'vscode://file/%f:%l']; |
| 26 | private $fileLinkFormat; |
| 27 | private $requestStack; |
| 28 | private $baseDir; |
| 29 | private $urlFormat; |
| 30 | /** |
| 31 | * @param string|array|null $fileLinkFormat |
| 32 | * @param string|\Closure $urlFormat the URL format, or a closure that returns it on-demand |
| 33 | */ |
| 34 | public function __construct($fileLinkFormat = null, ?RequestStack $requestStack = null, ?string $baseDir = null, $urlFormat = null) |
| 35 | { |
| 36 | if (!\is_array($fileLinkFormat) && ($fileLinkFormat = (self::FORMATS[$fileLinkFormat] ?? $fileLinkFormat ?: \ini_get('xdebug.file_link_format')) ?: get_cfg_var('xdebug.file_link_format'))) { |
| 37 | $i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: \strlen($f); |
| 38 | $fileLinkFormat = [substr($f, 0, $i)] + preg_split('/&([^>]++)>/', substr($f, $i), -1, \PREG_SPLIT_DELIM_CAPTURE); |
| 39 | } |
| 40 | $this->fileLinkFormat = $fileLinkFormat; |
| 41 | $this->requestStack = $requestStack; |
| 42 | $this->baseDir = $baseDir; |
| 43 | $this->urlFormat = $urlFormat; |
| 44 | } |
| 45 | public function format(string $file, int $line) |
| 46 | { |
| 47 | if ($fmt = $this->getFileLinkFormat()) { |
| 48 | for ($i = 1; isset($fmt[$i]); ++$i) { |
| 49 | if (str_starts_with($file, $k = $fmt[$i++])) { |
| 50 | $file = substr_replace($file, $fmt[$i], 0, \strlen($k)); |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | return strtr($fmt[0], ['%f' => $file, '%l' => $line]); |
| 55 | } |
| 56 | return \false; |
| 57 | } |
| 58 | /** |
| 59 | * @internal |
| 60 | */ |
| 61 | public function __sleep() : array |
| 62 | { |
| 63 | $this->fileLinkFormat = $this->getFileLinkFormat(); |
| 64 | return ['fileLinkFormat']; |
| 65 | } |
| 66 | /** |
| 67 | * @internal |
| 68 | */ |
| 69 | public static function generateUrlFormat(UrlGeneratorInterface $router, string $routeName, string $queryString) : ?string |
| 70 | { |
| 71 | try { |
| 72 | return $router->generate($routeName) . $queryString; |
| 73 | } catch (\Throwable $e) { |
| 74 | return null; |
| 75 | } |
| 76 | } |
| 77 | private function getFileLinkFormat() |
| 78 | { |
| 79 | if ($this->fileLinkFormat) { |
| 80 | return $this->fileLinkFormat; |
| 81 | } |
| 82 | if ($this->requestStack && $this->baseDir && $this->urlFormat) { |
| 83 | $request = $this->requestStack->getMainRequest(); |
| 84 | if ($request instanceof Request && (!$this->urlFormat instanceof \Closure || ($this->urlFormat = ($this->urlFormat)()))) { |
| 85 | return [$request->getSchemeAndHttpHost() . $this->urlFormat, $this->baseDir . \DIRECTORY_SEPARATOR, '']; |
| 86 | } |
| 87 | } |
| 88 | return null; |
| 89 | } |
| 90 | } |
| 91 |