1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Dev; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
6
|
|
|
use SilverStripe\Core\ClassInfo; |
7
|
|
|
use SilverStripe\Core\Convert; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A basic HTML wrapper for stylish rendering of a developement info view. |
11
|
|
|
* Used to output error messages, and test results. |
12
|
|
|
* |
13
|
|
|
* @todo Perhaps DebugView should be an interface / ABC, implemented by HTMLDebugView and CliDebugView? |
14
|
|
|
*/ |
15
|
|
|
class CliDebugView extends DebugView |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Render HTML header for development views |
20
|
|
|
* |
21
|
|
|
* @param HTTPRequest $httpRequest |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function renderHeader($httpRequest = null) |
25
|
|
|
{ |
26
|
|
|
return null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Render HTML footer for development views |
31
|
|
|
*/ |
32
|
|
|
public function renderFooter() |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Write information about the error to the screen |
38
|
|
|
* |
39
|
|
|
* @param string $httpRequest |
40
|
|
|
* @param int $errno |
41
|
|
|
* @param string $errstr |
42
|
|
|
* @param string $errfile |
43
|
|
|
* @param int $errline |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function renderError($httpRequest, $errno, $errstr, $errfile, $errline) |
47
|
|
|
{ |
48
|
|
|
if (!isset(self::$error_types[$errno])) { |
49
|
|
|
$errorTypeTitle = "UNKNOWN TYPE, ERRNO $errno"; |
50
|
|
|
} else { |
51
|
|
|
$errorTypeTitle = self::$error_types[$errno]['title']; |
52
|
|
|
} |
53
|
|
|
$output = CLI::text("ERROR [" . $errorTypeTitle . "]: $errstr\nIN $httpRequest\n", "red", null, true); |
54
|
|
|
$output .= CLI::text("Line $errline in $errfile\n\n", "red"); |
55
|
|
|
|
56
|
|
|
return $output; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Write a fragment of the a source file |
61
|
|
|
* |
62
|
|
|
* @param array $lines An array of file lines; the keys should be the original line numbers |
63
|
|
|
* @param int $errline Index of the line in $lines which has the error |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function renderSourceFragment($lines, $errline) |
67
|
|
|
{ |
68
|
|
|
$output = "Source\n======\n"; |
69
|
|
|
foreach ($lines as $offset => $line) { |
70
|
|
|
$output .= ($offset == $errline) ? "* " : " "; |
71
|
|
|
$output .= str_pad("$offset:", 5); |
72
|
|
|
$output .= wordwrap($line, self::config()->columns, "\n "); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
$output .= "\n"; |
75
|
|
|
|
76
|
|
|
return $output; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Write a backtrace |
81
|
|
|
* |
82
|
|
|
* @param array $trace |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function renderTrace($trace = null) |
86
|
|
|
{ |
87
|
|
|
$output = "Trace\n=====\n"; |
88
|
|
|
$output .= Backtrace::get_rendered_backtrace($trace ? $trace : debug_backtrace(), true); |
89
|
|
|
|
90
|
|
|
return $output; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function renderParagraph($text) |
94
|
|
|
{ |
95
|
|
|
return wordwrap($text, self::config()->columns) . "\n\n"; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Render the information header for the view |
100
|
|
|
* |
101
|
|
|
* @param string $title |
102
|
|
|
* @param string $subtitle |
103
|
|
|
* @param string $description |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function renderInfo($title, $subtitle, $description = null) |
107
|
|
|
{ |
108
|
|
|
$output = wordwrap(strtoupper($title), self::config()->columns) . "\n"; |
|
|
|
|
109
|
|
|
$output .= wordwrap($subtitle, self::config()->columns) . "\n"; |
110
|
|
|
$output .= str_repeat('-', min(self::config()->columns, max(strlen($title), strlen($subtitle)))) . "\n"; |
111
|
|
|
$output .= wordwrap($description, self::config()->columns) . "\n\n"; |
112
|
|
|
|
113
|
|
|
return $output; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function renderVariable($val, $caller) |
117
|
|
|
{ |
118
|
|
|
$output = PHP_EOL; |
119
|
|
|
$output .= CLI::text(str_repeat('=', self::config()->columns), 'green'); |
|
|
|
|
120
|
|
|
$output .= PHP_EOL; |
121
|
|
|
$output .= CLI::text($this->formatCaller($caller), 'blue', null, true); |
122
|
|
|
$output .= PHP_EOL . PHP_EOL; |
123
|
|
|
if (is_string($val)) { |
124
|
|
|
$output .= wordwrap($val, self::config()->columns); |
125
|
|
|
} else { |
126
|
|
|
$output .= var_export($val, true); |
127
|
|
|
} |
128
|
|
|
$output .= PHP_EOL; |
129
|
|
|
$output .= CLI::text(str_repeat('=', self::config()->columns), 'green'); |
130
|
|
|
$output .= PHP_EOL; |
131
|
|
|
|
132
|
|
|
return $output; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Similar to renderVariable() but respects debug() method on object if available |
137
|
|
|
* |
138
|
|
|
* @param mixed $val |
139
|
|
|
* @param array $caller |
140
|
|
|
* @param bool $showHeader |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function debugVariable($val, $caller, $showHeader = true) |
144
|
|
|
{ |
145
|
|
|
$text = $this->debugVariableText($val); |
146
|
|
|
if ($showHeader) { |
147
|
|
|
$callerFormatted = $this->formatCaller($caller); |
148
|
|
|
return "Debug ($callerFormatted)\n{$text}\n\n"; |
149
|
|
|
} else { |
150
|
|
|
return $text; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get debug text for this object |
156
|
|
|
* |
157
|
|
|
* @param mixed $val |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function debugVariableText($val) |
161
|
|
|
{ |
162
|
|
|
// Check debug |
163
|
|
|
if (is_object($val) && ClassInfo::hasMethod($val, 'debug')) { |
164
|
|
|
return $val->debug(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Format as array |
168
|
|
|
if (is_array($val)) { |
169
|
|
|
$result = ''; |
170
|
|
|
foreach ($val as $key => $valItem) { |
171
|
|
|
$valText = $this->debugVariableText($valItem); |
172
|
|
|
$result .= "$key = $valText\n"; |
173
|
|
|
} |
174
|
|
|
return $result; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// Format object |
178
|
|
|
if (is_object($val)) { |
179
|
|
|
return var_export($val, true); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// Format bool |
183
|
|
|
if (is_bool($val)) { |
184
|
|
|
return '(bool) ' . ($val ? 'true' : 'false'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// Format text |
188
|
|
|
if (is_string($val)) { |
189
|
|
|
return wordwrap($val, self::config()->columns); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// Other |
193
|
|
|
return var_export($val, true); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function renderMessage($message, $caller, $showHeader = true) |
197
|
|
|
{ |
198
|
|
|
$header = ''; |
199
|
|
|
if ($showHeader) { |
200
|
|
|
$file = basename($caller['file']); |
201
|
|
|
$line = $caller['line']; |
202
|
|
|
$header .= "Debug (line {$line} of {$file}):\n"; |
203
|
|
|
} |
204
|
|
|
return $header . "{$message}\n\n"; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|