Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 
12
namespace Symfony\Component\HttpKernel\Debug;
13
 
14
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
 
19
/**
20
 * Formats debug file links.
21
 *
22
 * @author Jérémy Romey <jeremy@free-agent.fr>
23
 *
24
 * @final
25
 */
26
class FileLinkFormatter
27
{
28
    private array|false $fileLinkFormat;
29
    private ?RequestStack $requestStack = null;
30
    private ?string $baseDir = null;
31
    private \Closure|string|null $urlFormat;
32
 
33
    /**
34
     * @param string|\Closure $urlFormat the URL format, or a closure that returns it on-demand
35
     */
36
    public function __construct(string|array $fileLinkFormat = null, RequestStack $requestStack = null, string $baseDir = null, string|\Closure $urlFormat = null)
37
    {
38
        $fileLinkFormat ??= $_ENV['SYMFONY_IDE'] ?? $_SERVER['SYMFONY_IDE'] ?? '';
39
 
40
        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) {
41
            $i = strpos($f = $fileLinkFormat, '&', 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
    public function format(string $file, int $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
 
61
            return strtr($fmt[0], ['%f' => $file, '%l' => $line]);
62
        }
63
 
64
        return false;
65
    }
66
 
67
    /**
68
     * @internal
69
     */
70
    public function __sleep(): array
71
    {
72
        $this->fileLinkFormat = $this->getFileLinkFormat();
73
 
74
        return ['fileLinkFormat'];
75
    }
76
 
77
    /**
78
     * @internal
79
     */
80
    public static function generateUrlFormat(UrlGeneratorInterface $router, string $routeName, string $queryString): ?string
81
    {
82
        try {
83
            return $router->generate($routeName).$queryString;
84
        } catch (\Throwable) {
85
            return null;
86
        }
87
    }
88
 
89
    private function getFileLinkFormat(): array|false
90
    {
91
        if ($this->fileLinkFormat) {
92
            return $this->fileLinkFormat;
93
        }
94
 
95
        if ($this->requestStack && $this->baseDir && $this->urlFormat) {
96
            $request = $this->requestStack->getMainRequest();
97
 
98
            if ($request instanceof Request && (!$this->urlFormat instanceof \Closure || $this->urlFormat = ($this->urlFormat)())) {
99
                return [
100
                    $request->getSchemeAndHttpHost().$this->urlFormat,
101
                    $this->baseDir.\DIRECTORY_SEPARATOR, '',
102
                ];
103
            }
104
        }
105
 
106
        return false;
107
    }
108
}