-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathFlattenException.php
More file actions
299 lines (270 loc) · 7.84 KB
/
Copy pathFlattenException.php
File metadata and controls
299 lines (270 loc) · 7.84 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\debug;
/**
* FlattenException wraps a PHP Exception to be able to serialize it.
* Implements the Throwable interface
* Basically, this class removes all objects from the trace.
* Ported from Symfony components @link https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Debug/Exception/FlattenException.php
*
* @author Dmitry Bashkarev <dmitry@bashkarev.com>
* @since 2.0.10
*/
class FlattenException
{
/**
* @var string
*/
protected $message;
/**
* @var mixed|int
*/
protected $code;
/**
* @var string
*/
protected $file;
/**
* @var int
*/
protected $line;
/**
* @var FlattenException|null
*/
private $_previous;
/**
* @var array
*/
private $_trace;
/**
* @var string
*/
private $_toString;
/**
* @var string
*/
private $_class;
/**
* FlattenException constructor.
* @param \Exception $exception
*/
public function __construct(\Exception $exception)
{
$this->setMessage($exception->getMessage());
$this->setCode($exception->getCode());
$this->setFile($exception->getFile());
$this->setLine($exception->getLine());
$this->setTrace($exception->getTrace());
$this->setToString($exception->__toString());
$this->setClass(get_class($exception));
$previous = $exception->getPrevious();
if ($previous instanceof \Exception) {
$this->setPrevious(new self($previous));
}
}
/**
* Gets the Exception message
* @return string the Exception message as a string.
*/
public function getMessage()
{
return $this->message;
}
/**
* Gets the Exception code
* @return mixed|int the exception code as integer.
*/
public function getCode()
{
return $this->code;
}
/**
* Gets the file in which the exception occurred
* @return string the filename in which the exception was created.
*/
public function getFile()
{
return $this->file;
}
/**
* Gets the line in which the exception occurred
* @return int the line number where the exception was created.
*/
public function getLine()
{
return $this->line;
}
/**
* Gets the stack trace
* @return array the Exception stack trace as an array.
*/
public function getTrace()
{
return $this->_trace;
}
/**
* Returns previous Exception
* @return FlattenException the previous `FlattenException` if available or null otherwise.
*/
public function getPrevious()
{
return $this->_previous;
}
/**
* Gets the stack trace as a string
* @return string the Exception stack trace as a string.
*/
public function getTraceAsString()
{
$remove = "Stack trace:\n";
$len = strpos($this->_toString, $remove);
if ($len === false) {
return '';
}
return substr($this->_toString, $len + strlen($remove));
}
/**
* String representation of the exception
* @return string the string representation of the exception.
*/
public function __toString()
{
return $this->_toString;
}
/**
* @return string the name of the class in which the exception was created.
*/
public function getClass()
{
return $this->_class;
}
/**
* @param string $message the Exception message as a string.
*/
protected function setMessage($message)
{
$this->message = $message;
}
/**
* @param mixed|int $code the exception code as integer.
*/
protected function setCode($code)
{
$this->code = $code;
}
/**
* @param string $file the filename in which the exception was created.
*/
protected function setFile($file)
{
$this->file = $file;
}
/**
* @param int $line the line number where the exception was created.
*/
protected function setLine($line)
{
$this->line = $line;
}
/**
* @param array $trace the Exception stack trace as an array.
*/
protected function setTrace($trace)
{
$this->_trace = [];
foreach ($trace as $entry) {
$class = '';
$namespace = '';
if (isset($entry['class'])) {
$parts = explode('\\', $entry['class']);
$class = array_pop($parts);
$namespace = implode('\\', $parts);
}
$this->_trace[] = [
'namespace' => $namespace,
'short_class' => $class,
'class' => isset($entry['class']) ? $entry['class'] : '',
'type' => isset($entry['type']) ? $entry['type'] : '',
'function' => isset($entry['function']) ? $entry['function'] : null,
'file' => isset($entry['file']) ? $entry['file'] : null,
'line' => isset($entry['line']) ? $entry['line'] : null,
'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : [],
];
}
}
/**
* @param string $string the string representation of the thrown object.
*/
protected function setToString($string)
{
$this->_toString = $string;
}
/**
* @param FlattenException $previous previous Exception.
*/
protected function setPrevious(FlattenException $previous)
{
$this->_previous = $previous;
}
/**
* @param string $class the name of the class in which the exception was created.
*/
protected function setClass($class)
{
$this->_class = $class;
}
/**
* Allows you to sterilize the Exception trace arguments
* @param array $args
* @param int $level recursion level
* @param int $count number of records counter
* @return array arguments tracing.
*/
private function flattenArgs($args, $level = 0, &$count = 0)
{
$result = [];
foreach ($args as $key => $value) {
if (++$count > 10000) {
return ['array', '*SKIPPED over 10000 entries*'];
}
if ($value instanceof \__PHP_Incomplete_Class) {
// is_object() returns false on PHP<=7.1
$result[$key] = ['incomplete-object', $this->getClassNameFromIncomplete($value)];
} elseif (is_object($value)) {
$result[$key] = ['object', get_class($value)];
} elseif (is_array($value)) {
if ($level > 10) {
$result[$key] = ['array', '*DEEP NESTED ARRAY*'];
} else {
$result[$key] = ['array', $this->flattenArgs($value, $level + 1, $count)];
}
} elseif (null === $value) {
$result[$key] = ['null', null];
} elseif (is_bool($value)) {
$result[$key] = ['boolean', $value];
} elseif (is_int($value)) {
$result[$key] = ['integer', $value];
} elseif (is_float($value)) {
$result[$key] = ['float', $value];
} elseif (is_resource($value)) {
$result[$key] = ['resource', get_resource_type($value)];
} else {
$result[$key] = ['string', (string)$value];
}
}
return $result;
}
/**
* @param \__PHP_Incomplete_Class $value
* @return string the real class name of an incomplete class
*/
private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
{
$array = \get_object_vars($value);
return (string) ($array['__PHP_Incomplete_Class_Name'] ?? '');
}
}