Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Http;
4
 
5
use Illuminate\Foundation\Concerns\ResolvesDumpSource;
6
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
7
use Symfony\Component\VarDumper\Cloner\Data;
8
use Symfony\Component\VarDumper\Cloner\VarCloner;
9
use Symfony\Component\VarDumper\Dumper\HtmlDumper as BaseHtmlDumper;
10
use Symfony\Component\VarDumper\VarDumper;
11
 
12
class HtmlDumper extends BaseHtmlDumper
13
{
14
    use ResolvesDumpSource;
15
 
16
    /**
17
     * Where the source should be placed on "expanded" kind of dumps.
18
     *
19
     * @var string
20
     */
21
    const EXPANDED_SEPARATOR = 'class=sf-dump-expanded>';
22
 
23
    /**
24
     * Where the source should be placed on "non expanded" kind of dumps.
25
     *
26
     * @var string
27
     */
28
    const NON_EXPANDED_SEPARATOR = "\n</pre><script>";
29
 
30
    /**
31
     * The base path of the application.
32
     *
33
     * @var string
34
     */
35
    protected $basePath;
36
 
37
    /**
38
     * The compiled view path of the application.
39
     *
40
     * @var string
41
     */
42
    protected $compiledViewPath;
43
 
44
    /**
45
     * If the dumper is currently dumping.
46
     *
47
     * @var bool
48
     */
49
    protected $dumping = false;
50
 
51
    /**
52
     * Create a new HTML dumper instance.
53
     *
54
     * @param  string  $basePath
55
     * @param  string  $compiledViewPath
56
     * @return void
57
     */
58
    public function __construct($basePath, $compiledViewPath)
59
    {
60
        parent::__construct();
61
 
62
        $this->basePath = $basePath;
63
        $this->compiledViewPath = $compiledViewPath;
64
    }
65
 
66
    /**
67
     * Create a new HTML dumper instance and register it as the default dumper.
68
     *
69
     * @param  string  $basePath
70
     * @param  string  $compiledViewPath
71
     * @return void
72
     */
73
    public static function register($basePath, $compiledViewPath)
74
    {
75
        $cloner = tap(new VarCloner())->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
76
 
77
        $dumper = new static($basePath, $compiledViewPath);
78
 
79
        VarDumper::setHandler(fn ($value) => $dumper->dumpWithSource($cloner->cloneVar($value)));
80
    }
81
 
82
    /**
83
     * Dump a variable with its source file / line.
84
     *
85
     * @param  \Symfony\Component\VarDumper\Cloner\Data  $data
86
     * @return void
87
     */
88
    public function dumpWithSource(Data $data)
89
    {
90
        if ($this->dumping) {
91
            $this->dump($data);
92
 
93
            return;
94
        }
95
 
96
        $this->dumping = true;
97
 
98
        $output = (string) $this->dump($data, true);
99
 
100
        $output = match (true) {
101
            str_contains($output, static::EXPANDED_SEPARATOR) => str_replace(
102
                static::EXPANDED_SEPARATOR,
103
                static::EXPANDED_SEPARATOR.$this->getDumpSourceContent(),
104
                $output,
105
            ),
106
            str_contains($output, static::NON_EXPANDED_SEPARATOR) => str_replace(
107
                static::NON_EXPANDED_SEPARATOR,
108
                $this->getDumpSourceContent().static::NON_EXPANDED_SEPARATOR,
109
                $output,
110
            ),
111
            default => $output,
112
        };
113
 
114
        fwrite($this->outputStream, $output);
115
 
116
        $this->dumping = false;
117
    }
118
 
119
    /**
120
     * Get the dump's source HTML content.
121
     *
122
     * @return string
123
     */
124
    protected function getDumpSourceContent()
125
    {
126
        if (is_null($dumpSource = $this->resolveDumpSource())) {
127
            return '';
128
        }
129
 
130
        [$file, $relativeFile, $line] = $dumpSource;
131
 
132
        $source = sprintf('%s%s', $relativeFile, is_null($line) ? '' : ":$line");
133
 
134
        if ($href = $this->resolveSourceHref($file, $line)) {
135
            $source = sprintf('<a href="%s">%s</a>', $href, $source);
136
        }
137
 
138
        return sprintf('<span style="color: #A0A0A0;"> // %s</span>', $source);
139
    }
140
}