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