/
DebugView.php
392 lines (361 loc) · 11.2 KB
/
DebugView.php
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
namespace SilverStripe\Dev;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
/**
* A basic HTML wrapper for stylish rendering of a development info view.
* Used to output error messages, and test results.
*/
class DebugView
{
use Configurable;
use Injectable;
/**
* Column size to wrap long strings to
*
* @var int
* @config
*/
private static $columns = 100;
protected static $error_types = [
0 => [
'title' => 'Emergency',
'class' => 'error'
],
1 => [
'title' => 'Alert',
'class' => 'error'
],
2 => [
'title' => 'Critical',
'class' => 'error'
],
3 => [
'title' => 'Error',
'class' => 'error'
],
4 => [
'title' => 'Warning',
'class' => 'warning'
],
5 => [
'title' => 'Notice',
'class' => 'notice'
],
6 => [
'title' => 'Information',
'class' => 'info'
],
7=> [
'title' => 'SilverStripe\\Dev\\Debug',
'class' => 'debug'
],
E_USER_ERROR => [
'title' => 'User Error',
'class' => 'error'
],
E_CORE_ERROR => [
'title' => 'Core Error',
'class' => 'error'
],
E_NOTICE => [
'title' => 'Notice',
'class' => 'notice'
],
E_USER_NOTICE => [
'title' => 'User Notice',
'class' => 'notice'
],
E_DEPRECATED => [
'title' => 'Deprecated',
'class' => 'notice'
],
E_USER_DEPRECATED => [
'title' => 'User Deprecated',
'class' => 'notice'
],
E_CORE_ERROR => [
'title' => 'Core Error',
'class' => 'error'
],
E_WARNING => [
'title' => 'Warning',
'class' => 'warning'
],
E_CORE_WARNING => [
'title' => 'Core Warning',
'class' => 'warning'
],
E_USER_WARNING => [
'title' => 'User Warning',
'class' => 'warning'
],
E_STRICT => [
'title' => 'Strict Notice',
'class' => 'notice'
],
E_RECOVERABLE_ERROR => [
'title' => 'Recoverable Error',
'class' => 'warning'
]
];
protected static $unknown_error = [
'title' => 'Unknown Error',
'class' => 'error'
];
/**
* Generate breadcrumb links to the URL path being displayed
*
* @return string
*/
public function Breadcrumbs()
{
$basePath = str_replace(Director::protocolAndHost() ?? '', '', Director::absoluteBaseURL() ?? '');
$relPath = parse_url(
substr($_SERVER['REQUEST_URI'] ?? '', strlen($basePath ?? ''), strlen($_SERVER['REQUEST_URI'] ?? '')),
PHP_URL_PATH
);
$parts = explode('/', $relPath ?? '');
$base = Director::absoluteBaseURL();
$pathPart = "";
$pathLinks = [];
foreach ($parts as $part) {
if ($part != '') {
$pathPart = Controller::join_links($pathPart, $part);
$href = Controller::join_links($base, $pathPart);
$pathLinks[] = "<a href=\"$href\">$part</a>";
}
}
return implode(' → ', $pathLinks);
}
/**
* Render HTML header for development views
*
* @param HTTPRequest $httpRequest
* @return string
*/
public function renderHeader($httpRequest = null)
{
$url = htmlentities(
$_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'],
ENT_COMPAT,
'UTF-8'
);
$debugCSS = ModuleResourceLoader::singleton()
->resolveURL('silverstripe/framework:client/styles/debug.css');
$output = '<!DOCTYPE html><html><head><title>' . $url . '</title>';
$output .= '<link rel="stylesheet" type="text/css" href="' . $debugCSS . '" />';
$output .= '<meta name="robots" content="noindex">';
$output .= '</head>';
$output .= '<body>';
return $output;
}
/**
* Render the information header for the view
*
* @param string $title The main title
* @param string $subtitle The subtitle
* @param string|bool $description The description to show
* @return string
*/
public function renderInfo($title, $subtitle, $description = false)
{
$output = '<div class="info header">';
$output .= "<h1>" . Convert::raw2xml($title) . "</h1>";
if ($subtitle) {
$output .= "<h3>" . Convert::raw2xml($subtitle) . "</h3>";
}
if ($description) {
$output .= "<p>$description</p>";
} else {
$output .= $this->Breadcrumbs();
}
$output .= '</div>';
return $output;
}
/**
* Render HTML footer for development views
* @return string
*/
public function renderFooter()
{
return "</body></html>";
}
/**
* Render an error.
*
* @param string $httpRequest the kind of request
* @param int $errno Codenumber of the error
* @param string $errstr The error message
* @param string $errfile The name of the source code file where the error occurred
* @param int $errline The line number on which the error occurred
* @return string
*/
public function renderError($httpRequest, $errno, $errstr, $errfile, $errline)
{
$errorType = isset(self::$error_types[$errno]) ? self::$error_types[$errno] : self::$unknown_error;
$httpRequestEnt = htmlentities($httpRequest ?? '', ENT_COMPAT, 'UTF-8');
if (filter_var(ini_get('html_errors'), FILTER_VALIDATE_BOOL)) {
$errstr = strip_tags($errstr ?? '');
} else {
$errstr = Convert::raw2xml($errstr);
}
$output = '<div class="header info ' . $errorType['class'] . '">';
$output .= "<h1>[" . $errorType['title'] . '] ' . $errstr . "</h1>";
$output .= "<h3>$httpRequestEnt</h3>";
$output .= "<p>Line <strong>$errline</strong> in <strong>$errfile</strong></p>";
$output .= '</div>';
return $output;
}
/**
* Render a fragment of the a source file
*
* @param array $lines An array of file lines; the keys should be the original line numbers
* @param int $errline The line of the error
* @return string
*/
public function renderSourceFragment($lines, $errline)
{
$output = '<div class="info"><h3>Source</h3>';
$output .= '<pre>';
foreach ($lines as $offset => $line) {
$line = htmlentities($line ?? '', ENT_COMPAT, 'UTF-8');
if ($offset == $errline) {
$output .= "<span>$offset</span> <span class=\"error\">$line</span>";
} else {
$output .= "<span>$offset</span> $line";
}
}
$output .= '</pre></div>';
return $output;
}
/**
* Render a call track
*
* @param array $trace The debug_backtrace() array
* @return string
*/
public function renderTrace($trace)
{
$output = '<div class="info">';
$output .= '<h3>Trace</h3>';
$output .= Backtrace::get_rendered_backtrace($trace);
$output .= '</div>';
return $output;
}
/**
* Render an arbitrary paragraph.
*
* @param string $text The HTML-escaped text to render
* @return string
*/
public function renderParagraph($text)
{
return '<div class="info"><p>' . $text . '</p></div>';
}
/**
* Formats the caller of a method
*
* @param array $caller
* @return string
*/
protected function formatCaller($caller)
{
$return = basename($caller['file'] ?? '') . ":" . $caller['line'];
if (!empty($caller['class']) && !empty($caller['function'])) {
$return .= " - {$caller['class']}::{$caller['function']}()";
}
return $return;
}
/**
* Outputs a variable in a user presentable way
*
* @param object $val
* @param array $caller Caller information
* @return string
*/
public function renderVariable($val, $caller)
{
$output = '<pre style="background-color:#ccc;padding:5px;font-size:14px;line-height:18px;">';
$output .= "<span style=\"font-size: 12px;color:#666;\">" . $this->formatCaller($caller) . " - </span>\n";
if (is_string($val)) {
$output .= wordwrap($val ?? '', self::config()->columns ?? 0);
} else {
$output .= var_export($val, true);
}
$output .= '</pre>';
return $output;
}
public function renderMessage($message, $caller, $showHeader = true)
{
$header = '';
if ($showHeader) {
$file = basename($caller['file'] ?? '');
$line = $caller['line'];
$header .= "<b>Debug (line {$line} of {$file}):</b>\n";
}
return "<p class=\"alert alert-warning\">\n" . $header . Convert::raw2xml($message) . "</p>\n";
}
/**
* Similar to renderVariable() but respects debug() method on object if available
*
* @param mixed $val
* @param array $caller
* @param bool $showHeader
* @return string
*/
public function debugVariable($val, $caller, $showHeader = true)
{
$text = $this->debugVariableText($val);
if ($showHeader) {
$callerFormatted = $this->formatCaller($caller);
return "<div style=\"background-color: white; text-align: left;\">\n<hr>\n"
. "<h3>Debug <span style=\"font-size: 65%\">($callerFormatted)</span>\n</h3>\n"
. $text
. "</div>";
} else {
return $text;
}
}
/**
* Get debug text for this object
*
* @param mixed $val
* @return string
*/
public function debugVariableText($val)
{
// Check debug
if (is_object($val) && ClassInfo::hasMethod($val, 'debug')) {
return $val->debug();
}
// Format as array
if (is_array($val)) {
$result = '';
foreach ($val as $key => $valueItem) {
$keyText = Convert::raw2xml($key);
$valueText = $this->debugVariableText($valueItem);
$result .= "<li>{$keyText} = {$valueText}</li>\n";
}
return "<ul>\n{$result}</ul>\n";
}
// Format object
if (is_object($val)) {
return var_export($val, true);
}
// Format bool
if (is_bool($val)) {
return '(bool) ' . ($val ? 'true' : 'false');
}
// Format text
$html = Convert::raw2xml($val);
return "<pre style=\"font-family: Courier new, serif\">{$html}</pre>\n";
}
}