-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathHtmlFormatter.php
More file actions
275 lines (248 loc) · 8.38 KB
/
Copy pathHtmlFormatter.php
File metadata and controls
275 lines (248 loc) · 8.38 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
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
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Error\Debug;
use InvalidArgumentException;
use function Cake\Core\h;
/**
* A Debugger formatter for generating interactive styled HTML output.
*
* @internal
*/
class HtmlFormatter implements FormatterInterface
{
/**
* @var bool
*/
protected static bool $outputHeader = false;
/**
* Random id so that HTML ids are not shared between dump outputs.
*
* @var string
*/
protected string $id;
/**
* Constructor.
*/
public function __construct()
{
$this->id = uniqid('', true);
}
/**
* Check if the current environment is not a CLI context
*
* @return bool
*/
public static function environmentMatches(): bool
{
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
return false;
}
return true;
}
/**
* @inheritDoc
*/
public function formatWrapper(string $contents, array $location): string
{
$lineInfo = '';
if (isset($location['file'], $location['line'])) {
$lineInfo = sprintf(
'<span><strong>%s</strong> (line <strong>%s</strong>)</span>',
$location['file'],
$location['line'],
);
}
$parts = [
'<div class="cake-debug-output cake-debug" style="direction:ltr">',
$lineInfo,
$contents,
'</div>',
];
return implode("\n", $parts);
}
/**
* Generate the CSS and Javascript for dumps
*
* Only output once per process as we don't need it more than once.
*
* @return string
*/
protected function dumpHeader(): string
{
ob_start();
include __DIR__ . DIRECTORY_SEPARATOR . 'dumpHeader.html';
return (string)ob_get_clean();
}
/**
* Convert a tree of NodeInterface objects into HTML
*
* @param \Cake\Error\Debug\NodeInterface $node The node tree to dump.
* @return string
*/
public function dump(NodeInterface $node): string
{
$html = $this->export($node, 0);
$head = '';
if (!static::$outputHeader) {
static::$outputHeader = true;
$head = $this->dumpHeader();
}
return $head . '<div class="cake-debug">' . $html . '</div>';
}
/**
* Convert a tree of NodeInterface objects into HTML
*
* @param \Cake\Error\Debug\NodeInterface $var The node tree to dump.
* @param int $indent The current indentation level.
* @return string
*/
protected function export(NodeInterface $var, int $indent): string
{
if ($var instanceof ScalarNode) {
return match ($var->getType()) {
'bool' => $this->style('const', $var->getValue() ? 'true' : 'false'),
'null' => $this->style('const', 'null'),
'string' => $this->style('string', "'" . $var->getValue() . "'"),
'int', 'float' => $this->style('visibility', "({$var->getType()})") .
' ' . $this->style('number', "{$var->getValue()}"),
default => "({$var->getType()}) {$var->getValue()}",
};
}
if ($var instanceof ArrayNode) {
return $this->exportArray($var, $indent + 1);
}
if ($var instanceof ClassNode || $var instanceof ReferenceNode) {
return $this->exportObject($var, $indent + 1);
}
if ($var instanceof SpecialNode) {
return $this->style('special', $var->getValue());
}
throw new InvalidArgumentException('Unknown node received ' . $var::class);
}
/**
* Export an array type object
*
* @param \Cake\Error\Debug\ArrayNode $var The array to export.
* @param int $indent The current indentation level.
* @return string Exported array.
*/
protected function exportArray(ArrayNode $var, int $indent): string
{
$open = '<span class="cake-debug-array">' .
$this->style('punct', '[') .
'<samp class="cake-debug-array-items">';
$vars = [];
$break = "\n" . str_repeat(' ', $indent);
$endBreak = "\n" . str_repeat(' ', $indent - 1);
$arrow = $this->style('punct', ' => ');
foreach ($var->getChildren() as $item) {
$val = $item->getValue();
$vars[] = $break . '<span class="cake-debug-array-item">' .
$this->export($item->getKey(), $indent) . $arrow . $this->export($val, $indent) .
$this->style('punct', ',') .
'</span>';
}
$close = '</samp>' .
$endBreak .
$this->style('punct', ']') .
'</span>';
return $open . implode('', $vars) . $close;
}
/**
* Handles object to string conversion.
*
* @param \Cake\Error\Debug\ClassNode|\Cake\Error\Debug\ReferenceNode $var Object to convert.
* @param int $indent The current indentation level.
* @return string
* @see \Cake\Error\Debugger::exportVar()
*/
protected function exportObject(ClassNode|ReferenceNode $var, int $indent): string
{
$objectId = "cake-db-object-{$this->id}-{$var->getId()}";
$out = sprintf(
'<span class="cake-debug-object" id="%s">',
$objectId,
);
$break = "\n" . str_repeat(' ', $indent);
$endBreak = "\n" . str_repeat(' ', $indent - 1);
if ($var instanceof ReferenceNode) {
$link = sprintf(
'<a class="cake-debug-ref" href="#%s">id: %s</a>',
$objectId,
$var->getId(),
);
return '<span class="cake-debug-ref">' .
$this->style('punct', 'object(') .
$this->style('class', $var->getValue()) .
$this->style('punct', ') ') .
$link .
$this->style('punct', ' {}') .
'</span>';
}
$out .= $this->style('punct', 'object(') .
$this->style('class', $var->getValue()) .
$this->style('punct', ') id:') .
$this->style('number', (string)$var->getId()) .
$this->style('punct', ' {') .
'<samp class="cake-debug-object-props">';
$props = [];
foreach ($var->getChildren() as $property) {
$arrow = $this->style('punct', ' => ');
$visibility = $property->getVisibility();
$name = $property->getName();
if ($visibility && $visibility !== 'public') {
$props[] = $break .
'<span class="cake-debug-prop">' .
$this->style('visibility', $visibility) .
' ' .
$this->style('property', $name) .
$arrow .
$this->export($property->getValue(), $indent) .
'</span>';
} else {
$props[] = $break .
'<span class="cake-debug-prop">' .
$this->style('property', $name) .
$arrow .
$this->export($property->getValue(), $indent) .
'</span>';
}
}
$end = '</samp>' .
$endBreak .
$this->style('punct', '}') .
'</span>';
if ($props !== []) {
return $out . implode('', $props) . $end;
}
return $out . $end;
}
/**
* Style text with HTML class names
*
* @param string $style The style name to use.
* @param string $text The text to style.
* @return string The styled output.
*/
protected function style(string $style, string $text): string
{
return sprintf(
'<span class="cake-debug-%s">%s</span>',
$style,
h($text),
);
}
}