[TASK] Prettify Extbase Debugger Utility
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / DebuggerUtility.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Utility;
3
4 /* *
5 * This script belongs to the Extbase framework *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 /**
24 * This class is a backport of the corresponding class of TYPO3 Flow.
25 * All credits go to the TYPO3 Flow team.
26 */
27 /**
28 * A debugging utility class
29 *
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 * @api
32 */
33 class DebuggerUtility {
34
35 const PLAINTEXT_INDENT = ' ';
36 const HTML_INDENT = '&nbsp;&nbsp;&nbsp;';
37
38 /**
39 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
40 */
41 static protected $renderedObjects;
42
43 /**
44 * Hardcoded list of Extbase class names (regex) which should not be displayed during debugging
45 *
46 * @var array
47 */
48 static protected $blacklistedClassNames = array(
49 'PHPUnit_Framework_MockObject_InvocationMocker',
50 \TYPO3\CMS\Extbase\Reflection\ReflectionService::class,
51 \TYPO3\CMS\Extbase\Object\ObjectManager::class,
52 \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class,
53 \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class,
54 \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory::class,
55 \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class
56 );
57
58 /**
59 * Hardcoded list of property names (regex) which should not be displayed during debugging
60 *
61 * @var array
62 */
63 static protected $blacklistedPropertyNames = array('warning');
64
65 /**
66 * Is set to TRUE once the CSS file is included in the current page to prevent double inclusions of the CSS file.
67 *
68 * @var bool
69 */
70 static protected $stylesheetEchoed = FALSE;
71
72 /**
73 * Defines the max recursion depth of the dump, set to 8 due to common memory limits
74 *
75 * @var int
76 */
77 static protected $maxDepth = 8;
78
79 /**
80 * Clear the state of the debugger
81 *
82 * @return void
83 */
84 static protected function clearState() {
85 self::$renderedObjects = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
86 }
87
88 /**
89 * Renders a dump of the given value
90 *
91 * @param mixed $value
92 * @param int $level
93 * @param bool $plainText
94 * @param bool $ansiColors
95 * @return string
96 */
97 static protected function renderDump($value, $level, $plainText, $ansiColors) {
98 $dump = '';
99 if (is_string($value)) {
100 $croppedValue = strlen($value) > 2000 ? substr($value, 0, 2000) . '...' : $value;
101 if ($plainText) {
102 $dump = self::ansiEscapeWrap(('"' . implode((PHP_EOL . str_repeat(self::PLAINTEXT_INDENT, ($level + 1))), str_split($croppedValue, 76)) . '"'), '33', $ansiColors) . ' (' . strlen($value) . ' chars)';
103 } else {
104 $dump = sprintf('\'<span class="extbase-debug-string">%s</span>\' (%s chars)', implode('<br />' . str_repeat(self::HTML_INDENT, ($level + 1)), str_split(htmlspecialchars($croppedValue), 76)), strlen($value));
105 }
106 } elseif (is_numeric($value)) {
107 $dump = sprintf('%s (%s)', self::ansiEscapeWrap($value, '35', $ansiColors), gettype($value));
108 } elseif (is_bool($value)) {
109 $dump = $value ? self::ansiEscapeWrap('TRUE', '32', $ansiColors) : self::ansiEscapeWrap('FALSE', '32', $ansiColors);
110 } elseif (is_null($value) || is_resource($value)) {
111 $dump = gettype($value);
112 } elseif (is_array($value)) {
113 $dump = self::renderArray($value, $level + 1, $plainText, $ansiColors);
114 } elseif (is_object($value)) {
115 $dump = self::renderObject($value, $level + 1, $plainText, $ansiColors);
116 }
117 return $dump;
118 }
119
120 /**
121 * Renders a dump of the given array
122 *
123 * @param array|\Traversable $array
124 * @param int $level
125 * @param bool $plainText
126 * @param bool $ansiColors
127 * @return string
128 */
129 static protected function renderArray($array, $level, $plainText = FALSE, $ansiColors = FALSE) {
130 $content = '';
131 $count = count($array);
132
133 if ($plainText) {
134 $header = self::ansiEscapeWrap('array', '36', $ansiColors);
135 } else {
136 $header = '<span class="extbase-debug-type">array</span>';
137 }
138 $header .= $count > 0 ? '(' . $count . ' item' . ($count > 1 ? 's' : '') . ')' : '(empty)';
139 if ($level >= self::$maxDepth) {
140 if ($plainText) {
141 $header .= ' ' . self::ansiEscapeWrap('max depth', '47;30', $ansiColors);
142 } else {
143 $header .= '<span class="extbase-debug-filtered">max depth</span>';
144 }
145 } else {
146 $content = self::renderCollection($array, $level, $plainText, $ansiColors);
147 if (!$plainText) {
148 $header = ($level > 1 && $count > 0 ? '<input type="checkbox" /><span class="extbase-debug-header" >' : '<span>') . $header . '</span >';
149 }
150 }
151 if ($level > 1 && $count > 0 && !$plainText) {
152 $dump = '<span class="extbase-debugger-tree">' . $header . '<span class="extbase-debug-content">' . $content . '</span></span>';
153 } else {
154 $dump = $header . $content;
155 }
156 return $dump;
157 }
158
159 /**
160 * Renders a dump of the given object
161 *
162 * @param object $object
163 * @param int $level
164 * @param bool $plainText
165 * @param bool $ansiColors
166 * @return string
167 */
168 static protected function renderObject($object, $level, $plainText = FALSE, $ansiColors = FALSE) {
169 if ($object instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
170 $object = $object->_loadRealInstance();
171 }
172 $header = self::renderHeader($object, $level, $plainText, $ansiColors);
173 if ($level < self::$maxDepth && !self::isBlacklisted($object) && !(self::isAlreadyRendered($object) && $plainText !== TRUE)) {
174 $content = self::renderContent($object, $level, $plainText, $ansiColors);
175 } else {
176 $content = '';
177 }
178 if ($plainText) {
179 return $header . $content;
180 } else {
181 return '<span class="extbase-debugger-tree">' . $header . '<span class="extbase-debug-content">' . $content . '</span></span>';
182 }
183 }
184
185 /**
186 * Checks if a given object or property should be excluded/filtered
187 *
188 * @param object $value An ReflectionProperty or other Object
189 * @return bool TRUE if the given object should be filtered
190 */
191 static protected function isBlacklisted($value) {
192 $result = FALSE;
193 if ($value instanceof \ReflectionProperty) {
194 $result = (strpos(implode('|', self::$blacklistedPropertyNames), $value->getName()) > 0);
195 } elseif (is_object($value)) {
196 $result = (strpos(implode('|', self::$blacklistedClassNames), get_class($value)) > 0);
197 }
198 return $result;
199 }
200
201 /**
202 * Checks if a given object was already rendered.
203 *
204 * @param object $object
205 * @return bool TRUE if the given object was already rendered
206 */
207 static protected function isAlreadyRendered($object) {
208 return self::$renderedObjects->contains($object);
209 }
210
211 /**
212 * Renders the header of a given object/collection. It is usually the class name along with some flags.
213 *
214 * @param object $object
215 * @param int $level
216 * @param bool $plainText
217 * @param bool $ansiColors
218 * @return string The rendered header with tags
219 */
220 static protected function renderHeader($object, $level, $plainText, $ansiColors) {
221 $dump = '';
222 $persistenceType = '';
223 $className = get_class($object);
224 $classReflection = new \ReflectionClass($className);
225 if ($plainText) {
226 $dump .= self::ansiEscapeWrap($className, '36', $ansiColors);
227 } else {
228 $dump .= '<span class="extbase-debug-type">' . $className . '</span>';
229 }
230 if ($object instanceof \TYPO3\CMS\Core\SingletonInterface) {
231 $scope = 'singleton';
232 } else {
233 $scope = 'prototype';
234 }
235 if ($plainText) {
236 $dump .= ' ' . self::ansiEscapeWrap($scope, '44;37', $ansiColors);
237 } else {
238 $dump .= $scope ? '<span class="extbase-debug-scope">' . $scope . '</span>' : '';
239 }
240 if ($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject) {
241 if ($object->_isDirty()) {
242 $persistenceType = 'modified';
243 } elseif ($object->_isNew()) {
244 $persistenceType = 'transient';
245 } else {
246 $persistenceType = 'persistent';
247 }
248 }
249 if ($object instanceof \TYPO3\CMS\Extbase\Persistence\ObjectStorage && $object->_isDirty()) {
250 $persistenceType = 'modified';
251 }
252 if ($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractEntity) {
253 $domainObjectType = 'entity';
254 } elseif ($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject) {
255 $domainObjectType = 'valueobject';
256 } else {
257 $domainObjectType = 'object';
258 }
259 if ($plainText) {
260 $dump .= ' ' . self::ansiEscapeWrap(($persistenceType . ' ' . $domainObjectType), '42;30', $ansiColors);
261 } else {
262 $dump .= '<span class="extbase-debug-ptype">' . ($persistenceType ? $persistenceType . ' ' : '') . $domainObjectType . '</span>';
263 }
264 if (strpos(implode('|', self::$blacklistedClassNames), get_class($object)) > 0) {
265 if ($plainText) {
266 $dump .= ' ' . self::ansiEscapeWrap('filtered', '47;30', $ansiColors);
267 } else {
268 $dump .= '<span class="extbase-debug-filtered">filtered</span>';
269 }
270 } elseif (self::$renderedObjects->contains($object) && !$plainText) {
271 $dump = '<a href="javascript:;" onclick="document.location.hash=\'#' . spl_object_hash($object) . '\';" class="extbase-debug-seeabove">' . $dump . '<span class="extbase-debug-filtered">see above</span></a>';
272 } elseif ($level >= self::$maxDepth && !$object instanceof \DateTime) {
273 if ($plainText) {
274 $dump .= ' ' . self::ansiEscapeWrap('max depth', '47;30', $ansiColors);
275 } else {
276 $dump .= '<span class="extbase-debug-filtered">max depth</span>';
277 }
278 } elseif ($level > 1 && !$object instanceof \DateTime && !$plainText) {
279 if (($object instanceof \Countable && empty($object)) || empty($classReflection->getProperties())) {
280 $dump = '<span>' . $dump . '</span>';
281 } else {
282 $dump = '<input type="checkbox" id="' . spl_object_hash($object) . '" /><span class="extbase-debug-header">' . $dump . '</span>';
283 }
284 }
285 if ($object instanceof \Countable) {
286 $objectCount = count($object);
287 $dump .= $objectCount > 0 ? ' (' . $objectCount . ' items)' : ' (empty)';
288 }
289 if ($object instanceof \DateTime) {
290 $dump .= ' (' . $object->format(\DateTime::RFC3339) . ', ' . $object->getTimestamp() . ')';
291 }
292 if ($object instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface && !$object->_isNew()) {
293 $dump .= ' (uid=' . $object->getUid() . ', pid=' . $object->getPid() . ')';
294 }
295 return $dump;
296 }
297
298 /**
299 * @param object $object
300 * @param int $level
301 * @param bool $plainText
302 * @param bool $ansiColors
303 * @return string The rendered body content of the Object(Storage)
304 */
305 static protected function renderContent($object, $level, $plainText, $ansiColors) {
306 $dump = '';
307 if ($object instanceof \TYPO3\CMS\Extbase\Persistence\ObjectStorage || $object instanceof \Iterator || $object instanceof \ArrayObject) {
308 $dump .= self::renderCollection($object, $level, $plainText, $ansiColors);
309 } else {
310 self::$renderedObjects->attach($object);
311 if (!$plainText) {
312 $dump .= '<a name="' . spl_object_hash($object) . '" id="' . spl_object_hash($object) . '"></a>';
313 }
314 if (get_class($object) === 'stdClass') {
315 $objReflection = new \ReflectionObject($object);
316 $properties = $objReflection->getProperties();
317 } else {
318 $classReflection = new \ReflectionClass(get_class($object));
319 $properties = $classReflection->getProperties();
320 }
321 foreach ($properties as $property) {
322 if (self::isBlacklisted($property)) {
323 continue;
324 }
325 $dump .= PHP_EOL . str_repeat(self::PLAINTEXT_INDENT, $level) . ($plainText ? '' : '<span class="extbase-debug-property">') . self::ansiEscapeWrap($property->getName(), '37', $ansiColors) . ($plainText ? '' : '</span>') . ' => ';
326 $property->setAccessible(TRUE);
327 $dump .= self::renderDump($property->getValue($object), $level, $plainText, $ansiColors);
328 if ($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject && !$object->_isNew() && $object->_isDirty($property->getName())) {
329 if ($plainText) {
330 $dump .= ' ' . self::ansiEscapeWrap('modified', '43;30', $ansiColors);
331 } else {
332 $dump .= '<span class="extbase-debug-dirty">modified</span>';
333 }
334 }
335 }
336 }
337 return $dump;
338 }
339
340 /**
341 * @param mixed $collection
342 * @param int $level
343 * @param bool $plainText
344 * @param bool $ansiColors
345 * @return string
346 */
347 static protected function renderCollection($collection, $level, $plainText, $ansiColors) {
348 $dump = '';
349 foreach ($collection as $key => $value) {
350 $dump .= PHP_EOL . str_repeat(self::PLAINTEXT_INDENT, $level) . ($plainText ? '' : '<span class="extbase-debug-property">') . self::ansiEscapeWrap($key, '37', $ansiColors) . ($plainText ? '' : '</span>') . ' => ';
351 $dump .= self::renderDump($value, $level, $plainText, $ansiColors);
352 }
353 if ($collection instanceof \Iterator) {
354 $collection->rewind();
355 }
356 return $dump;
357 }
358
359 /**
360 * Wrap a string with the ANSI escape sequence for colorful output
361 *
362 * @param string $string The string to wrap
363 * @param string $ansiColors The ansi color sequence (e.g. "1;37")
364 * @param bool $enable If FALSE, the raw string will be returned
365 * @return string The wrapped or raw string
366 */
367 static protected function ansiEscapeWrap($string, $ansiColors, $enable = TRUE) {
368 if ($enable) {
369 return '\e[' . $ansiColors . 'm' . $string . '\e[0m';
370 } else {
371 return $string;
372 }
373 }
374
375 /**
376 * A var_dump function optimized for Extbase's object structures
377 *
378 * @param mixed $variable The value to dump
379 * @param string $title optional custom title for the debug output
380 * @param int $maxDepth Sets the max recursion depth of the dump. De- or increase the number according to your needs and memory limit.
381 * @param bool $plainText If TRUE, the dump is in plain text, if FALSE the debug output is in HTML format.
382 * @param bool $ansiColors If TRUE (default), ANSI color codes is added to the output, if FALSE the debug output not colored.
383 * @param bool $return if TRUE, the dump is returned for custom post-processing (e.g. embed in custom HTML). If FALSE (default), the dump is directly displayed.
384 * @param array $blacklistedClassNames An array of class names (RegEx) to be filtered. Default is an array of some common class names.
385 * @param array $blacklistedPropertyNames An array of property names and/or array keys (RegEx) to be filtered. Default is an array of some common property names.
386 * @return string if $return is TRUE, the dump is returned. By default, the dump is directly displayed, and nothing is returned.
387 * @api
388 */
389 static public function var_dump($variable, $title = NULL, $maxDepth = 8, $plainText = FALSE, $ansiColors = TRUE, $return = FALSE, $blacklistedClassNames = NULL, $blacklistedPropertyNames = NULL) {
390 self::$maxDepth = $maxDepth;
391 if ($title === NULL) {
392 $title = 'Extbase Variable Dump';
393 }
394 $ansiColors = $plainText && $ansiColors;
395 if ($ansiColors === TRUE) {
396 $title = '\e[1m' . $title . '\e[0m';
397 }
398 if (is_array($blacklistedClassNames)) {
399 self::$blacklistedClassNames = $blacklistedClassNames;
400 }
401 if (is_array($blacklistedPropertyNames)) {
402 self::$blacklistedPropertyNames = $blacklistedPropertyNames;
403 }
404 self::clearState();
405 if (!$plainText && self::$stylesheetEchoed === FALSE) {
406 echo '
407 <style type=\'text/css\'>
408 .extbase-debugger-tree{position:relative}
409 .extbase-debugger-tree input{position:absolute;top:0;left:0;height:14px;width:14px;margin:0;cursor:pointer;opacity:0;z-index:2}
410 .extbase-debugger-tree input~.extbase-debug-content{display:none}
411 .extbase-debugger-tree .extbase-debug-header:before{position:relative;top:3px;content:"";padding:0;line-height:10px;height:12px;width:12px;text-align:center;margin:0 3px 0 0;background-image:url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkViZW5lXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgMTIgMTIiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDEyIDEyOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+PHN0eWxlIHR5cGU9InRleHQvY3NzIj4uc3Qwe2ZpbGw6Izg4ODg4ODt9PC9zdHlsZT48cGF0aCBpZD0iQm9yZGVyIiBjbGFzcz0ic3QwIiBkPSJNMTEsMTFIMFYwaDExVjExeiBNMTAsMUgxdjloOVYxeiIvPjxnIGlkPSJJbm5lciI+PHJlY3QgeD0iMiIgeT0iNSIgY2xhc3M9InN0MCIgd2lkdGg9IjciIGhlaWdodD0iMSIvPjxyZWN0IHg9IjUiIHk9IjIiIGNsYXNzPSJzdDAiIHdpZHRoPSIxIiBoZWlnaHQ9IjciLz48L2c+PC9zdmc+);display:inline-block}
412 .extbase-debugger-tree input:checked~.extbase-debug-content{display:inline}
413 .extbase-debugger-tree input:checked~.extbase-debug-header:before{background-image:url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkViZW5lXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgMTIgMTIiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDEyIDEyOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+PHN0eWxlIHR5cGU9InRleHQvY3NzIj4uc3Qwe2ZpbGw6Izg4ODg4ODt9PC9zdHlsZT48cGF0aCBpZD0iQm9yZGVyIiBjbGFzcz0ic3QwIiBkPSJNMTEsMTFIMFYwaDExVjExeiBNMTAsMUgxdjloOVYxeiIvPjxnIGlkPSJJbm5lciI+PHJlY3QgeD0iMiIgeT0iNSIgY2xhc3M9InN0MCIgd2lkdGg9IjciIGhlaWdodD0iMSIvPjwvZz48L3N2Zz4=)}
414 .extbase-debugger{display:block;text-align:left;background:#2a2a2a;border:1px solid #2a2a2a;box-shadow:0 3px 0 rgba(0,0,0,.5);color:#000;margin:20px;overflow:hidden;border-radius:4px}
415 .extbase-debugger-floating{position:relative;z-index:999}
416 .extbase-debugger-top{background:#444;font-size:12px;font-family:monospace;color:#f1f1f1;padding:6px 15px}
417 .extbase-debugger-center{padding:0 15px;margin:15px 0;background-image:repeating-linear-gradient(to bottom,transparent 0,transparent 20px,#252525 20px,#252525 40px)}
418 .extbase-debugger-center,.extbase-debugger-center .extbase-debug-string,.extbase-debugger-center a,.extbase-debugger-center p,.extbase-debugger-center pre,.extbase-debugger-center strong{font-size:12px;font-weight:400;font-family:monospace;line-height:20px;color:#f1f1f1}
419 .extbase-debugger-center pre{background-color:transparent;margin:0;padding:0;border:0;word-wrap:break-word;color:#999}
420 .extbase-debugger-center .extbase-debug-string{color:#ce9178;white-space:normal}
421 .extbase-debugger-center .extbase-debug-type{color:#569CD6;padding-right:4px}
422 .extbase-debugger-center .extbase-debug-unregistered{background-color:#dce1e8}
423 .extbase-debugger-center .extbase-debug-filtered,.extbase-debugger-center .extbase-debug-proxy,.extbase-debugger-center .extbase-debug-ptype,.extbase-debugger-center .extbase-debug-scope{color:#fff;font-size:10px;line-height:12px;padding:2px 4px;margin-right:2px;position:relative;top:-1px}
424 .extbase-debugger-center .extbase-debug-scope{background-color:#497AA2}
425 .extbase-debugger-center .extbase-debug-ptype{background-color:#698747}
426 .extbase-debugger-center .extbase-debug-dirty{background-color:#FFFFB6}
427 .extbase-debugger-center .extbase-debug-filtered{background-color:#4F4F4F}
428 .extbase-debugger-center .extbase-debug-seeabove{text-decoration:none;font-style:italic}
429 .extbase-debugger-center .extbase-debug-property{color:#f1f1f1}
430 </style>';
431 self::$stylesheetEchoed = TRUE;
432 }
433 if ($plainText) {
434 $output = $title . PHP_EOL . self::renderDump($variable, 0, TRUE, $ansiColors) . PHP_EOL . PHP_EOL;
435 } else {
436 $output = '
437 <div class="extbase-debugger ' . ($return ? 'extbase-debugger-inline' : 'extbase-debugger-floating') . '">
438 <div class="extbase-debugger-top">' . htmlspecialchars($title) . '</div>
439 <div class="extbase-debugger-center">
440 <pre dir="ltr">' . self::renderDump($variable, 0, FALSE, FALSE) . '</pre>
441 </div>
442 </div>
443 ';
444 }
445 if ($return === TRUE) {
446 return $output;
447 } else {
448 echo $output;
449 }
450 return '';
451 }
452
453 }