Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
339 lines (290 loc) · 9.91 KB

File metadata and controls

339 lines (290 loc) · 9.91 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
declare(strict_types=1);
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar\DataCollector;
use DebugBar\DataCollector\Message\LinkMessage;
use DebugBar\DataCollector\Message\MessageInterface;
use DebugBar\DataFormatter\HasXdebugLinks;
use Psr\Log\AbstractLogger;
use DebugBar\DataFormatter\HasDataFormatter;
/**
* Provides a way to log messages
*/
class MessagesCollector extends AbstractLogger implements DataCollectorInterface, MessagesAggregateInterface, Renderable, Resettable
{
use HasDataFormatter;
use HasXdebugLinks;
use HasTimeDataCollector;
protected string $name;
protected array $messages = [];
/** @var array<MessagesAggregateInterface> */
protected array $aggregates = [];
protected bool $compactDumps = false;
protected bool $collectFile = false;
protected int $backtraceLimit = 15;
/** @var array<string> */
protected array $backtraceExcludePaths = ['/vendor/'];
public function __construct(string $name = 'messages')
{
$this->name = $name;
}
public function reset(): void
{
$this->messages = [];
foreach ($this->aggregates as $collector) {
if ($collector instanceof Resettable) {
$collector->reset();
}
}
}
public function compactDumps(bool $enabled = true): void
{
$this->compactDumps = $enabled;
}
public function collectFileTrace(bool $enabled = true): void
{
$this->collectFile = $enabled;
}
public function limitBacktrace(int $limit): void
{
$this->backtraceLimit = $limit;
}
/**
* Set paths to exclude from the backtrace
*/
public function addBacktraceExcludePaths(array $excludePaths): void
{
$this->backtraceExcludePaths = array_merge($this->backtraceExcludePaths, $excludePaths);
}
/**
* Check if the given file is to be excluded from analysis
*/
protected function fileIsInExcludedPath(string $file): bool
{
$normalizedPath = str_replace('\\', '/', $file);
foreach ($this->backtraceExcludePaths as $excludedPath) {
if (str_contains($normalizedPath, $excludedPath)) {
return true;
}
}
return false;
}
/**
* Returns a simple plain-text representation of a variable for search/display fallback.
*
*/
protected function getPlainTextFromVar(mixed $var): string
{
if (is_null($var)) {
return 'null';
}
if (is_bool($var)) {
return $var ? 'true' : 'false';
}
if (is_scalar($var)) {
return (string) $var;
}
if (is_array($var)) {
return 'array(' . count($var) . ')';
}
if (is_object($var)) {
return $this->getDataFormatter()->formatClassName($var);
}
return gettype($var);
}
protected function compactMessageDump(?string $messageHtml): ?string
{
$pos = strpos((string) $messageHtml, 'sf-dump-expanded');
if ($pos !== false) {
$messageHtml = substr_replace($messageHtml, 'sf-dump-compact', $pos, 16);
}
return $messageHtml;
}
protected function getStackTraceItem(array $stacktrace): array
{
foreach ($stacktrace as $trace) {
if (!isset($trace['file']) || $this->fileIsInExcludedPath($trace['file'])) {
continue;
}
return $trace;
}
return $stacktrace[0];
}
/**
* Adds a message
*
* A message can be anything from an object to a string
*/
public function addMessage(mixed $message, string $label = 'info', array $context = []): void
{
// For string messages, interpolate the context following PSR-3
if (is_string($message) && $context) {
$message = $this->interpolate($message, $context);
}
$isString = is_string($message);
$formattedMessage = $this->getDataFormatter()->formatVar($message);
$messageText = null;
$messageHtml = null;
$messageJson = null;
if ($isString) {
$messageText = $formattedMessage;
} else {
if ($message instanceof MessageInterface) {
$messageText = $message->getText();
$messageHtml = $message->getHtml();
} elseif ($this->isJsonVarDumperUsed()) {
$messageJson = $formattedMessage;
} elseif ($this->isHtmlVarDumperUsed()) {
$messageHtml = $formattedMessage;
if ($this->compactDumps) {
$messageHtml = $this->compactMessageDump($messageHtml);
}
} else {
$messageText = $formattedMessage;
}
}
$contextJson = null;
if ($context) {
foreach ($context as $key => $value) {
$formatted = $this->getDataFormatter()->formatVar($value);
if ($this->isJsonVarDumperUsed()) {
$contextJson[$key] = $formatted;
$context[$key] = null;
} else {
$context[$key] = $formatted;
}
}
} else {
$context = null;
}
$stackItem = [];
if ($this->collectFile) {
$stackItem = $this->getStackTraceItem(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $this->backtraceLimit));
}
$this->messages[] = [
'message' => $messageText,
'message_html' => $messageHtml,
'message_json' => $messageJson,
'is_string' => $isString,
'context' => $context,
'context_json' => $contextJson,
'label' => $label,
'time' => microtime(true),
'xdebug_link' => $stackItem ? $this->getXdebugLink($stackItem['file'], $stackItem['line'] ?? null) : null,
];
if ($this->hasTimeDataCollector()) {
$this->addTimeMeasure("[{$label}]: " . substr($isString ? $message : $this->getPlainTextFromVar($message), 0, 100), microtime(true));
}
}
public function addLink(string $text, string $url, string $label = 'info', array $context = []): void
{
$message = new LinkMessage($text, $url);
$this->addMessage($message, $label, $context);
}
/**
* Aggregates messages from other collectors
*/
public function aggregate(MessagesAggregateInterface $messages): void
{
if ($this->collectFile && method_exists($messages, 'collectFileTrace')) {
$messages->collectFileTrace();
}
$this->aggregates[] = $messages;
}
public function getMessages(): array
{
$messages = $this->messages;
foreach ($this->aggregates as $collector) {
$msgs = array_map(function ($m) use ($collector): array {
$m['collector'] = $collector->getName();
return $m;
}, $collector->getMessages());
$messages = array_merge($messages, $msgs);
}
// sort messages by their timestamp
usort($messages, function ($a, $b): int {
if ($a['time'] === $b['time']) {
return 0;
}
return $a['time'] < $b['time'] ? -1 : 1;
});
return $messages;
}
public function log(mixed $level, mixed $message, array $context = []): void
{
$this->addMessage($message, $level, $context);
}
/**
* Interpolates context values into the message placeholders.
*/
public function interpolate(string $message, array $context = []): string
{
// build a replacement array with braces around the context keys
$replace = [];
foreach ($context as $key => $val) {
$placeholder = '{' . $key . '}';
if (!str_contains($message, $placeholder)) {
continue;
}
// check that the value can be cast to string
if (null === $val || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) {
$replace[$placeholder] = $val;
} elseif ($val instanceof \DateTimeInterface) {
$replace[$placeholder] = $val->format("Y-m-d\TH:i:s.uP");
} elseif ($val instanceof \UnitEnum) {
$replace[$placeholder] = $val instanceof \BackedEnum ? $val->value : $val->name;
} elseif (is_object($val)) {
$replace[$placeholder] = '[object ' . $this->getDataFormatter()->formatClassName($val) . ']';
} elseif (is_array($val)) {
$json = @json_encode($val);
$replace[$placeholder] = false === $json ? 'null' : 'array' . $json;
} else {
$replace[$placeholder] = '[' . gettype($val) . ']';
}
}
// interpolate replacement values into the message and return
return strtr($message, $replace);
}
/**
* Deletes all messages
*/
public function clear(): void
{
$this->messages = [];
}
public function collect(): array
{
$messages = $this->getMessages();
return [
'count' => count($messages),
'messages' => $messages,
];
}
public function getName(): string
{
return $this->name;
}
public function getWidgets(): array
{
$name = $this->getName();
return [
"$name" => [
'icon' => 'logs',
"widget" => "PhpDebugBar.Widgets.MessagesWidget",
"map" => "$name.messages",
"default" => "[]",
],
"$name:badge" => [
"map" => "$name.count",
"default" => "null",
],
];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.