-
Notifications
You must be signed in to change notification settings - Fork 573
/
VariablesPanel.php
193 lines (176 loc) · 6.06 KB
/
VariablesPanel.php
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\Panel;
use Cake\Collection\Collection;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\Form\Form;
use Cake\ORM\Query;
use Cake\ORM\ResultSet;
use Cake\Utility\Hash;
use Closure;
use DebugKit\DebugPanel;
use Exception;
use InvalidArgumentException;
use PDO;
use RuntimeException;
use SimpleXMLElement;
/**
* Provides debug information on the View variables.
*
*/
class VariablesPanel extends DebugPanel
{
/**
* Extracts nested validation errors
*
* @param EntityInterface $entity Entity to extract
*
* @return array
*/
protected function _getErrors(EntityInterface $entity)
{
$errors = $entity->errors();
foreach ($entity->visibleProperties() as $property) {
$v = $entity[$property];
if ($v instanceof EntityInterface) {
$errors[$property] = $this->_getErrors($v);
} elseif (is_array($v)) {
foreach ($v as $key => $varValue) {
if ($varValue instanceof EntityInterface) {
$errors[$property][$key] = $this->_getErrors($varValue);
}
}
}
}
return Hash::filter($errors);
}
/**
* Safely retrieves debug information from an object
* and applies a callback.
*
* @param callable $walker The walker to apply on the debug info array.
* @param object $item The item whose debug info to retrieve.
*
* @return array|string
*/
protected function _walkDebugInfo(callable $walker, $item)
{
try {
$info = $item->__debugInfo();
} catch (\Exception $exception) {
return __d('debug_kit', 'Could not retrieve debug info - {0}. Error: {1} in {2}, line {3}', get_class($item), $exception->getMessage(), $exception->getFile(), $exception->getLine());
}
return array_map($walker, $info);
}
/**
* Shutdown event
*
* @param \Cake\Event\Event $event The event
* @return void
*/
public function shutdown(Event $event)
{
$controller = $event->getSubject();
$errors = [];
$walker = function (&$item) use (&$walker) {
if ($item instanceof Collection ||
$item instanceof Query ||
$item instanceof ResultSet
) {
try {
$item = $item->toArray();
} catch (\Cake\Database\Exception $e) {
//Likely issue is unbuffered query; fall back to __debugInfo
$item = $this->_walkDebugInfo($walker, $item);
} catch (RuntimeException $e) {
// Likely a non-select query.
$item = $this->_walkDebugInfo($walker, $item);
} catch (InvalidArgumentException $e) {
$item = $this->_walkDebugInfo($walker, $item);
}
} elseif ($item instanceof Closure ||
$item instanceof PDO ||
$item instanceof SimpleXMLElement
) {
$item = 'Unserializable object - ' . get_class($item);
} elseif ($item instanceof Exception) {
$item = sprintf(
'Unserializable object - %s. Error: %s in %s, line %s',
get_class($item),
$item->getMessage(),
$item->getFile(),
$item->getLine()
);
} elseif (is_object($item)) {
if (method_exists($item, '__debugInfo')) {
// Convert objects into using __debugInfo.
$item = $this->_walkDebugInfo($walker, $item);
} else {
$item = $this->trySerialize($item);
}
} elseif (is_resource($item)) {
$item = sprintf('[%s] %s', get_resource_type($item), $item);
}
return $this->trySerialize($item);
};
// Copy so viewVars is not mutated.
$vars = $controller->viewVars;
array_walk_recursive($vars, $walker);
foreach ($vars as $k => $v) {
// Get the validation errors for Entity
if ($v instanceof EntityInterface) {
$errors[$k] = $this->_getErrors($v);
} elseif ($v instanceof Form) {
$formError = $v->errors();
if (!empty($formError)) {
$errors[$k] = $formError;
}
}
}
$this->_data = [
'content' => $vars,
'errors' => $errors
];
}
/**
* Try to serialize an item, provide an error message if not possible
*
* @param mixed $item Item to check
* @return mixed The $item if it is serializable, error message if not
*/
protected function trySerialize($item)
{
try {
serialize($item);
return $item;
} catch (\Exception $e) {
if (is_object($item)) {
return __d('debug_kit', 'Unserializable object - {0}. Error: {1} in {2}, line {3}', get_class($item), $e->getMessage(), $e->getFile(), $e->getLine());
}
return __d('debug_kit', 'Unserializable Error: {1} in {2}, line {3}', $e->getMessage(), $e->getFile(), $e->getLine());
}
}
/**
* Get summary data for the variables panel.
*
* @return string
*/
public function summary()
{
if (!isset($this->_data['content'])) {
return '0';
}
return (string)count($this->_data['content']);
}
}