/
CliDebugView.php
204 lines (180 loc) · 5.65 KB
/
CliDebugView.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
<?php
namespace SilverStripe\Dev;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Convert;
/**
* A basic HTML wrapper for stylish rendering of a development info view.
* Used to output error messages, and test results.
*/
class CliDebugView extends DebugView
{
/**
* Render HTML header for development views
*
* @param HTTPRequest $httpRequest
* @return string
*/
public function renderHeader($httpRequest = null)
{
return null;
}
/**
* Render HTML footer for development views
*/
public function renderFooter()
{
}
/**
* Write information about the error to the screen
*
* @param string $httpRequest
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return string
*/
public function renderError($httpRequest, $errno, $errstr, $errfile, $errline)
{
if (!isset(self::$error_types[$errno])) {
$errorTypeTitle = "UNKNOWN TYPE, ERRNO $errno";
} else {
$errorTypeTitle = self::$error_types[$errno]['title'];
}
$output = CLI::text("ERROR [" . $errorTypeTitle . "]: $errstr\nIN $httpRequest\n", "red", null, true);
$output .= CLI::text("Line $errline in $errfile\n\n", "red");
return $output;
}
/**
* Write 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 Index of the line in $lines which has the error
* @return string
*/
public function renderSourceFragment($lines, $errline)
{
$output = "Source\n======\n";
foreach ($lines as $offset => $line) {
$output .= ($offset == $errline) ? "* " : " ";
$output .= str_pad("$offset:", 5);
$output .= wordwrap($line ?? '', self::config()->columns ?? 0, "\n ");
}
$output .= "\n";
return $output;
}
/**
* Write a backtrace
*
* @param array $trace
* @return string
*/
public function renderTrace($trace = null)
{
$output = "Trace\n=====\n";
$output .= Backtrace::get_rendered_backtrace($trace ? $trace : debug_backtrace(), true);
return $output;
}
public function renderParagraph($text)
{
return wordwrap($text ?? '', self::config()->columns ?? 0) . "\n\n";
}
/**
* Render the information header for the view
*
* @param string $title
* @param string $subtitle
* @param string $description
* @return string
*/
public function renderInfo($title, $subtitle, $description = null)
{
$output = wordwrap(strtoupper($title ?? ''), self::config()->columns ?? 0) . "\n";
$output .= wordwrap($subtitle ?? '', self::config()->columns ?? 0) . "\n";
$output .= str_repeat('-', min(self::config()->columns, max(strlen($title ?? ''), strlen($subtitle ?? ''))) ?? 0) . "\n";
$output .= wordwrap($description ?? '', self::config()->columns ?? 0) . "\n\n";
return $output;
}
public function renderVariable($val, $caller)
{
$output = PHP_EOL;
$output .= CLI::text(str_repeat('=', self::config()->columns ?? 0), 'green');
$output .= PHP_EOL;
$output .= CLI::text($this->formatCaller($caller), 'blue', null, true);
$output .= PHP_EOL . PHP_EOL;
if (is_string($val)) {
$output .= wordwrap($val ?? '', self::config()->columns ?? 0);
} else {
$output .= var_export($val, true);
}
$output .= PHP_EOL;
$output .= CLI::text(str_repeat('=', self::config()->columns ?? 0), 'green');
$output .= PHP_EOL;
return $output;
}
/**
* 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 "Debug ($callerFormatted)\n{$text}\n\n";
} 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 => $valItem) {
$valText = $this->debugVariableText($valItem);
$result .= "$key = $valText\n";
}
return $result;
}
// Format object
if (is_object($val)) {
return print_r($val, true);
}
// Format bool
if (is_bool($val)) {
return '(bool) ' . ($val ? 'true' : 'false');
}
// Format text
if (is_string($val)) {
return wordwrap($val ?? '', self::config()->columns ?? 0);
}
// Other
return var_export($val, true);
}
public function renderMessage($message, $caller, $showHeader = true)
{
$header = '';
if ($showHeader) {
$file = basename($caller['file'] ?? '');
$line = $caller['line'];
$header .= "Debug (line {$line} of {$file}):\n";
}
return $header . "{$message}\n\n";
}
}