Repository navigation
Expand file tree
/
Copy pathTrace.php
More file actions
170 lines (143 loc) · 5.75 KB
/
Copy pathTrace.php
File metadata and controls
170 lines (143 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types=1);
namespace PHPForge\Debug\Helper;
use Closure;
use PHPForge\Debug\Panel\Request\RequestDiagnosticValueRenderer;
use function rtrim;
use function str_replace;
use function str_starts_with;
use function strlen;
use function strtr;
use function substr;
/**
* Renders captured argument-free source frames as configurable, escaped source links.
*/
final class Trace
{
/**
* Default template: an `ide://` deep link IDE extensions resolve into "open file at line".
*/
public const string DEFAULT_TEMPLATE = '<a href="ide://open?url=file://{file}&line={line}">{text}</a>';
/**
* @param (Closure(array<string, mixed>): mixed)|string|false $template Renderer applied to every frame.
* @param array<string, string> $pathMappings Normalized source-path prefixes mapped to local prefixes.
*/
private function __construct(private Closure|string|false $template, private array $pathMappings) {}
/**
* Creates a renderer emitting {@see DEFAULT_TEMPLATE} links without path rewriting.
*
* @return self Renderer using the default template and no path mappings.
*/
public static function create(): self
{
return new self(self::DEFAULT_TEMPLATE, []);
}
/**
* Renders one source frame, or an escaped dump when `file` or `line` is missing.
*
* Internal PHP functions such as {@see call_user_func()} may produce frames without those keys, see
* {@link https://www.php.net/manual/en/function.debug-backtrace.php#59713}.
*
* @param array<string, mixed> $frame Source frame; consumes `file`, `line`, and the optional `text` label.
*
* @return string Rendered source line.
*/
public function render(array $frame): string
{
$file = Coerce::stringOrNull($frame['file'] ?? null);
$line = Coerce::stringOrNull($frame['line'] ?? null);
if ($file === null || $line === null) {
return RequestDiagnosticValueRenderer::escape(Dump::asString($frame));
}
$text = "{$file}:{$line}";
if (isset($frame['text'])) {
$text = Coerce::stringOrNull($frame['text']) ?? Dump::asString($frame['text']);
}
$file = $this->mapPath(str_replace('\\', '/', $file));
$template = $this->template;
if ($template === false) {
return RequestDiagnosticValueRenderer::escape($text);
}
if ($template instanceof Closure) {
$frame['file'] = $file;
$frame['line'] = $line;
$frame['text'] = $text;
$rendered = $template($frame);
$template = Coerce::stringOrNull($rendered);
if ($template === null) {
return RequestDiagnosticValueRenderer::escape(Dump::asString($rendered));
}
}
return strtr(
$template,
[
'{file}' => RequestDiagnosticValueRenderer::escape($file),
'{line}' => RequestDiagnosticValueRenderer::escape($line),
'{text}' => RequestDiagnosticValueRenderer::escape($text),
],
);
}
/**
* Returns a copy rewriting containerized or remote source paths to their local counterparts.
*
* Entries whose value does not coerce to a string are dropped; both sides are normalized to forward slashes and a
* single trailing slash, and only the first matching prefix is applied.
*
* @param array<array-key, mixed> $pathMappings Remote source-path prefixes mapped to local prefixes.
*
* @return self New instance rewriting frame paths through the normalized mappings.
*/
public function withPathMappings(array $pathMappings): self
{
$normalized = [];
foreach ($pathMappings as $remote => $local) {
$target = Coerce::stringOrNull($local);
if ($target === null) {
continue;
}
$normalized[self::normalizePrefix((string) $remote)] = self::normalizePrefix($target);
}
return new self(template: $this->template, pathMappings: $normalized);
}
/**
* Returns a copy rendering every frame with the given template.
*
* @param (Closure(array<string, mixed>): mixed)|string|false $template Placeholder template resolving `{file}`,
* `{line}`, and `{text}` against the escaped frame values, `false` to emit escaped plain text without a link, or a
* closure receiving the frame with the normalized `file`, `line`, and `text` keys; a `string` returned by the
* closure resolves the same placeholders, any other result renders as an escaped dump.
*
* @return self New instance rendering every frame with the given template.
*/
public function withTemplate(Closure|string|false $template): self
{
return new self(template: $template, pathMappings: $this->pathMappings);
}
/**
* Rewrites a captured file path through the first matching source-path mapping.
*
* @param string $file Captured source path.
*
* @return string Locally reachable path, or the captured path when no mapping matches.
*/
private function mapPath(string $file): string
{
foreach ($this->pathMappings as $remote => $local) {
if (str_starts_with($file, $remote)) {
return $local . substr($file, strlen($remote));
}
}
return $file;
}
/**
* Normalizes a path prefix to forward slashes and exactly one trailing separator.
*
* @param string $path Raw configured prefix.
*
* @return string Prefix using `/` separators and ending in a single `/`.
*/
private static function normalizePrefix(string $path): string
{
return rtrim(str_replace('\\', '/', $path), '/') . '/';
}
}