1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kint\Renderer; |
4
|
|
|
|
5
|
|
|
use Kint\Kint; |
6
|
|
|
use Kint\Object\BasicObject; |
7
|
|
|
use Kint\Object\BlobObject; |
8
|
|
|
use Kint\Object\InstanceObject; |
9
|
|
|
|
10
|
|
|
class TextRenderer extends Renderer |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* TextRenderer plugins should be instances of Kint\Renderer\Text\Plugin. |
14
|
|
|
*/ |
15
|
|
|
public static $plugins = array( |
16
|
|
|
'blacklist' => 'Kint\\Renderer\\Text\\BlacklistPlugin', |
17
|
|
|
'depth_limit' => 'Kint\\Renderer\\Text\\DepthLimitPlugin', |
18
|
|
|
'microtime' => 'Kint\\Renderer\\Text\\MicrotimePlugin', |
19
|
|
|
'nothing' => 'Kint\\Renderer\\Text\\NothingPlugin', |
20
|
|
|
'recursion' => 'Kint\\Renderer\\Text\\RecursionPlugin', |
21
|
|
|
'trace' => 'Kint\\Renderer\\Text\\TracePlugin', |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Parser plugins must be instanceof one of these or |
26
|
|
|
* it will be removed for performance reasons. |
27
|
|
|
*/ |
28
|
|
|
public static $parser_plugin_whitelist = array( |
|
|
|
|
29
|
|
|
'Kint\\Parser\\BlacklistPlugin', |
30
|
|
|
'Kint\\Parser\\MicrotimePlugin', |
31
|
|
|
'Kint\\Parser\\StreamPlugin', |
32
|
|
|
'Kint\\Parser\\TracePlugin', |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The maximum length of a string before it is truncated. |
37
|
|
|
* |
38
|
|
|
* Falsey to disable |
39
|
|
|
* |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
public static $strlen_max = 0; |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The default width of the terminal for headers. |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
public static $default_width = 80; |
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Indentation width. |
53
|
|
|
* |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
public static $default_indent = 4; |
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Decorate the header and footer. |
60
|
|
|
* |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
public static $decorations = true; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sort mode for object properties. |
67
|
|
|
* |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
public static $sort = self::SORT_NONE; |
71
|
|
|
|
72
|
|
|
public $header_width = 80; |
|
|
|
|
73
|
|
|
public $indent_width = 4; |
|
|
|
|
74
|
|
|
|
75
|
|
|
protected $plugin_objs = array(); |
|
|
|
|
76
|
|
|
protected $previous_caller; |
|
|
|
|
77
|
|
|
protected $callee; |
78
|
|
|
protected $show_minitrace = true; |
|
|
|
|
79
|
|
|
|
80
|
|
|
public function __construct(array $params = array()) |
81
|
|
|
{ |
82
|
|
|
parent::__construct($params); |
83
|
|
|
|
84
|
|
|
$params += array( |
85
|
|
|
'callee' => null, |
86
|
|
|
'caller' => null, |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$this->callee = $params['callee']; |
90
|
|
|
$this->previous_caller = $params['caller']; |
91
|
|
|
$this->show_minitrace = !empty($params['settings']['display_called_from']); |
92
|
|
|
$this->header_width = self::$default_width; |
93
|
|
|
$this->indent_width = self::$default_indent; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function render(BasicObject $o) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if ($plugin = $this->getPlugin(self::$plugins, $o->hints)) { |
99
|
|
|
if (strlen($output = $plugin->render($o))) { |
100
|
|
|
return $output; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$out = ''; |
105
|
|
|
|
106
|
|
|
if ($o->depth == 0) { |
107
|
|
|
$out .= $this->colorTitle($this->renderTitle($o)).PHP_EOL; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$out .= $this->renderHeader($o); |
111
|
|
|
$out .= $this->renderChildren($o).PHP_EOL; |
112
|
|
|
|
113
|
|
|
return $out; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function boxText($text, $width) |
117
|
|
|
{ |
118
|
|
|
if (BlobObject::strlen($text) > $width - 4) { |
119
|
|
|
$text = BlobObject::substr($text, 0, $width - 7).'...'; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$text .= str_repeat(' ', $width - 4 - BlobObject::strlen($text)); |
123
|
|
|
|
124
|
|
|
$out = '┌'.str_repeat('─', $width - 2).'┐'.PHP_EOL; |
125
|
|
|
$out .= '│ '.$this->escape($text).' │'.PHP_EOL; |
126
|
|
|
$out .= '└'.str_repeat('─', $width - 2).'┘'; |
127
|
|
|
|
128
|
|
|
return $out; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function renderTitle(BasicObject $o) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
if (($name = $o->getName()) === null) { |
134
|
|
|
$name = 'literal'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (self::$decorations) { |
138
|
|
|
return $this->boxText($name, $this->header_width); |
139
|
|
|
} elseif (BlobObject::strlen($name) > $this->header_width) { |
140
|
|
|
return BlobObject::substr($name, 0, $this->header_width - 3).'...'; |
141
|
|
|
} else { |
142
|
|
|
return $name; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function renderHeader(BasicObject $o) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$output = array(); |
149
|
|
|
|
150
|
|
|
if ($o->depth) { |
151
|
|
|
if (($s = $o->getModifiers()) !== null) { |
|
|
|
|
152
|
|
|
$output[] = $s; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($o->name !== null) { |
156
|
|
|
$output[] = $this->escape(var_export($o->name, true)); |
157
|
|
|
|
158
|
|
|
if (($s = $o->getOperator()) !== null) { |
159
|
|
|
$output[] = $this->escape($s); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (($s = $o->getType()) !== null) { |
165
|
|
|
if ($o->reference) { |
166
|
|
|
$s = '&'.$s; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$output[] = $this->colorType($this->escape($s)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
View Code Duplication |
if (($s = $o->getSize()) !== null) { |
|
|
|
|
173
|
|
|
$output[] = '('.$this->escape($s).')'; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
if (($s = $o->getValueShort()) !== null) { |
|
|
|
|
177
|
|
|
if (self::$strlen_max && BlobObject::strlen($s) > self::$strlen_max) { |
178
|
|
|
$s = substr($s, 0, self::$strlen_max).'...'; |
179
|
|
|
} |
180
|
|
|
$output[] = $this->colorValue($this->escape($s)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return str_repeat(' ', $o->depth * $this->indent_width).implode(' ', $output); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function renderChildren(BasicObject $o) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
if ($o->type === 'array') { |
189
|
|
|
$output = ' ['; |
190
|
|
|
} elseif ($o->type === 'object') { |
191
|
|
|
$output = ' ('; |
192
|
|
|
} else { |
193
|
|
|
return ''; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$children = ''; |
197
|
|
|
|
198
|
|
|
if ($o->value && is_array($o->value->contents)) { |
199
|
|
View Code Duplication |
if ($o instanceof InstanceObject && $o->value->getName() === 'properties') { |
|
|
|
|
200
|
|
|
foreach (self::sortProperties($o->value->contents, self::$sort) as $obj) { |
201
|
|
|
$children .= $this->render($obj); |
202
|
|
|
} |
203
|
|
|
} else { |
204
|
|
|
foreach ($o->value->contents as $child) { |
205
|
|
|
$children .= $this->render($child); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ($children) { |
211
|
|
|
$output .= PHP_EOL.$children; |
212
|
|
|
$output .= str_repeat(' ', $o->depth * $this->indent_width); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if ($o->type === 'array') { |
216
|
|
|
$output .= ']'; |
217
|
|
|
} elseif ($o->type === 'object') { |
218
|
|
|
$output .= ')'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $output; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function colorValue($string) |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
return $string; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function colorType($string) |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
return $string; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function colorTitle($string) |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
return $string; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function postRender() |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
if (self::$decorations) { |
242
|
|
|
$output = str_repeat('═', $this->header_width); |
243
|
|
|
} else { |
244
|
|
|
$output = ''; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if (!$this->show_minitrace) { |
248
|
|
|
return $this->colorTitle($output); |
249
|
|
|
} else { |
250
|
|
|
if ($output) { |
251
|
|
|
$output .= PHP_EOL; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $this->colorTitle($output.$this->calledFrom().PHP_EOL); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function parserPlugins(array $plugins) |
259
|
|
|
{ |
260
|
|
|
$return = array(); |
261
|
|
|
|
262
|
|
|
foreach ($plugins as $index => $plugin) { |
263
|
|
|
foreach (self::$parser_plugin_whitelist as $whitelist) { |
264
|
|
|
if ($plugin instanceof $whitelist) { |
265
|
|
|
$return[] = $plugin; |
266
|
|
|
continue 2; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $return; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
protected function calledFrom() |
275
|
|
|
{ |
276
|
|
|
$output = ''; |
277
|
|
|
|
278
|
|
|
if (isset($this->callee['file'])) { |
279
|
|
|
$output .= 'Called from '.$this->ideLink($this->callee['file'], $this->callee['line']); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$caller = ''; |
283
|
|
|
|
284
|
|
|
if (isset($this->previous_caller['class'])) { |
285
|
|
|
$caller .= $this->previous_caller['class']; |
286
|
|
|
} |
287
|
|
|
if (isset($this->previous_caller['type'])) { |
288
|
|
|
$caller .= $this->previous_caller['type']; |
289
|
|
|
} |
290
|
|
View Code Duplication |
if (isset($this->previous_caller['function']) |
|
|
|
|
291
|
|
|
&& !in_array( |
292
|
|
|
$this->previous_caller['function'], |
293
|
|
|
array('include', 'include_once', 'require', 'require_once') |
294
|
|
|
) |
295
|
|
|
) { |
296
|
|
|
$caller .= $this->previous_caller['function'].'()'; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if ($caller) { |
300
|
|
|
$output .= ' ['.$caller.']'; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
return $output; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function ideLink($file, $line) |
307
|
|
|
{ |
308
|
|
|
return $this->escape(Kint::shortenPath($file)).':'.$line; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
View Code Duplication |
protected function getPlugin(array $plugins, array $hints) |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
if ($plugins = $this->matchPlugins($plugins, $hints)) { |
|
|
|
|
314
|
|
|
$plugin = end($plugins); |
315
|
|
|
|
316
|
|
|
if (!isset($this->plugin_objs[$plugin])) { |
317
|
|
|
$this->plugin_objs[$plugin] = new $plugin($this); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return $this->plugin_objs[$plugin]; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function escape($string, $encoding = false) |
|
|
|
|
325
|
|
|
{ |
326
|
|
|
return $string; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.