forked from TYPO3/Fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugViewHelper.php
More file actions
179 lines (165 loc) · 5.67 KB
/
DebugViewHelper.php
File metadata and controls
179 lines (165 loc) · 5.67 KB
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
<?php
namespace TYPO3Fluid\Fluid\ViewHelpers;
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\Variables\VariableExtractor;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* This ViewHelper is only meant to be used during development.
*
* Examples
* ========
*
* Inline notation and custom title
* --------------------------------
*
* ::
*
* {object -> f:debug(title: 'Custom title')}
*
* Output::
*
* all properties of {object} nicely highlighted (with custom title)
*
* Only output the type
* --------------------
*
* ::
*
* {object -> f:debug(typeOnly: true)}
*
* Output::
*
* the type or class name of {object}
*
* @api
*/
class DebugViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var boolean
*/
protected $escapeChildren = false;
/**
* @var boolean
*/
protected $escapeOutput = false;
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('typeOnly', 'boolean', 'If TRUE, debugs only the type of variables', false, false);
$this->registerArgument('levels', 'integer', 'Levels to render when rendering nested objects/arrays', false, 5);
$this->registerArgument('html', 'boolean', 'Render HTML. If FALSE, output is indented plaintext', false, false);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$typeOnly = $arguments['typeOnly'];
$expressionToExamine = $renderChildrenClosure();
if ($typeOnly === true) {
return (is_object($expressionToExamine) ? get_class($expressionToExamine) : gettype($expressionToExamine));
}
$html = $arguments['html'];
$levels = $arguments['levels'];
return static::dumpVariable($expressionToExamine, $html, 1, $levels);
}
/**
* @param mixed $variable
* @param boolean $html
* @param integer $level
* @param integer $levels
* @return string
*/
protected static function dumpVariable($variable, $html, $level, $levels)
{
$typeLabel = is_object($variable) ? get_class($variable) : gettype($variable);
if (!$html) {
if (is_scalar($variable)) {
$string = sprintf('%s %s', $typeLabel, var_export($variable, true)) . PHP_EOL;
} elseif (is_null($variable)) {
$string = 'null' . PHP_EOL;
} else {
$string = sprintf('%s: ', $typeLabel);
if ($level > $levels) {
$string .= '*Recursion limited*';
} else {
$string .= PHP_EOL;
foreach (static::getValuesOfNonScalarVariable($variable) as $property => $value) {
$string .= sprintf(
'%s"%s": %s',
str_repeat(' ', $level),
$property,
static::dumpVariable($value, $html, $level + 1, $levels)
);
}
}
}
} else {
if (is_scalar($variable) || is_null($variable)) {
$string = sprintf(
'<code>%s = %s</code>',
$typeLabel,
htmlspecialchars(var_export($variable, true), ENT_COMPAT, 'UTF-8', false)
);
} else {
$string = sprintf('<code>%s</code>', $typeLabel);
if ($level > $levels) {
$string .= '<i>Recursion limited</i>';
} else {
$string .= '<ul>';
foreach (static::getValuesOfNonScalarVariable($variable) as $property => $value) {
$string .= sprintf(
'<li>%s: %s</li>',
$property,
static::dumpVariable($value, $html, $level + 1, $levels)
);
}
$string .= '</ul>';
}
}
}
return $string;
}
/**
* @param mixed $variable
* @return array
*/
protected static function getValuesOfNonScalarVariable($variable)
{
if ($variable instanceof \ArrayObject || is_array($variable)) {
return (array) $variable;
} elseif ($variable instanceof \Iterator) {
return iterator_to_array($variable);
} elseif (is_resource($variable)) {
return stream_get_meta_data($variable);
} elseif ($variable instanceof \DateTimeInterface) {
return [
'class' => get_class($variable),
'ISO8601' => $variable->format(\DateTime::ATOM),
'UNIXTIME' => (integer) $variable->format('U')
];
} else {
$reflection = new \ReflectionObject($variable);
$properties = $reflection->getProperties();
$output = [];
foreach ($properties as $property) {
$propertyName = $property->getName();
$output[$propertyName] = VariableExtractor::extract($variable, $propertyName);
}
return $output;
}
}
}