|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpKernel\Exception; |
|
|
13 |
|
|
|
14 |
/** |
|
|
15 |
* FlattenException wraps a PHP Exception to be able to serialize it. |
|
|
16 |
* |
|
|
17 |
* Basically, this class removes all objects from the trace. |
|
|
18 |
* |
|
|
19 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
20 |
*/ |
|
|
21 |
class FlattenException |
|
|
22 |
{ |
|
|
23 |
private $message; |
|
|
24 |
private $code; |
|
|
25 |
private $previous; |
|
|
26 |
private $trace; |
|
|
27 |
private $class; |
|
|
28 |
private $statusCode; |
|
|
29 |
private $headers; |
|
|
30 |
|
|
|
31 |
static public function create(\Exception $exception, $statusCode = 500, array $headers = array()) |
|
|
32 |
{ |
|
|
33 |
$e = new static(); |
|
|
34 |
$e->setMessage($exception->getMessage()); |
|
|
35 |
$e->setCode($exception->getCode()); |
|
|
36 |
$e->setStatusCode($statusCode); |
|
|
37 |
$e->setHeaders($headers); |
|
|
38 |
$e->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine()); |
|
|
39 |
$e->setClass(get_class($exception)); |
|
|
40 |
if ($exception->getPrevious()) { |
|
|
41 |
$e->setPrevious(static::create($exception->getPrevious())); |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
return $e; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
public function toArray() |
|
|
48 |
{ |
|
|
49 |
$exceptions = array(); |
|
|
50 |
foreach (array_merge(array($this), $this->getAllPrevious()) as $exception) { |
|
|
51 |
$exceptions[] = array( |
|
|
52 |
'message' => $exception->getMessage(), |
|
|
53 |
'class' => $exception->getClass(), |
|
|
54 |
'trace' => $exception->getTrace(), |
|
|
55 |
); |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
return $exceptions; |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
public function getStatusCode() |
|
|
62 |
{ |
|
|
63 |
return $this->statusCode; |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
public function setStatusCode($code) |
|
|
67 |
{ |
|
|
68 |
$this->statusCode = $code; |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
public function getHeaders() |
|
|
72 |
{ |
|
|
73 |
return $this->headers; |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
public function setHeaders(array $headers) |
|
|
77 |
{ |
|
|
78 |
$this->headers = $headers; |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
public function getClass() |
|
|
82 |
{ |
|
|
83 |
return $this->class; |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
public function setClass($class) |
|
|
87 |
{ |
|
|
88 |
$this->class = $class; |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
public function getMessage() |
|
|
92 |
{ |
|
|
93 |
return $this->message; |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
public function setMessage($message) |
|
|
97 |
{ |
|
|
98 |
$this->message = $message; |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
public function getCode() |
|
|
102 |
{ |
|
|
103 |
return $this->code; |
|
|
104 |
} |
|
|
105 |
|
|
|
106 |
public function setCode($code) |
|
|
107 |
{ |
|
|
108 |
$this->code = $code; |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
public function getPrevious() |
|
|
112 |
{ |
|
|
113 |
return $this->previous; |
|
|
114 |
} |
|
|
115 |
|
|
|
116 |
public function setPrevious(FlattenException $previous) |
|
|
117 |
{ |
|
|
118 |
$this->previous = $previous; |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
public function getAllPrevious() |
|
|
122 |
{ |
|
|
123 |
$exceptions = array(); |
|
|
124 |
$e = $this; |
|
|
125 |
while ($e = $e->getPrevious()) { |
|
|
126 |
$exceptions[] = $e; |
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
return $exceptions; |
|
|
130 |
} |
|
|
131 |
|
|
|
132 |
public function getTrace() |
|
|
133 |
{ |
|
|
134 |
return $this->trace; |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
public function setTrace($trace, $file, $line) |
|
|
138 |
{ |
|
|
139 |
$this->trace = array(); |
|
|
140 |
$this->trace[] = array( |
|
|
141 |
'namespace' => '', |
|
|
142 |
'short_class' => '', |
|
|
143 |
'class' => '', |
|
|
144 |
'type' => '', |
|
|
145 |
'function' => '', |
|
|
146 |
'file' => $file, |
|
|
147 |
'line' => $line, |
|
|
148 |
'args' => array(), |
|
|
149 |
); |
|
|
150 |
foreach ($trace as $entry) { |
|
|
151 |
$class = ''; |
|
|
152 |
$namespace = ''; |
|
|
153 |
if (isset($entry['class'])) { |
|
|
154 |
$parts = explode('\\', $entry['class']); |
|
|
155 |
$class = array_pop($parts); |
|
|
156 |
$namespace = implode('\\', $parts); |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
$this->trace[] = array( |
|
|
160 |
'namespace' => $namespace, |
|
|
161 |
'short_class' => $class, |
|
|
162 |
'class' => isset($entry['class']) ? $entry['class'] : '', |
|
|
163 |
'type' => isset($entry['type']) ? $entry['type'] : '', |
|
|
164 |
'function' => $entry['function'], |
|
|
165 |
'file' => isset($entry['file']) ? $entry['file'] : null, |
|
|
166 |
'line' => isset($entry['line']) ? $entry['line'] : null, |
|
|
167 |
'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : array(), |
|
|
168 |
); |
|
|
169 |
} |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
private function flattenArgs($args, $level = 0) |
|
|
173 |
{ |
|
|
174 |
$result = array(); |
|
|
175 |
foreach ($args as $key => $value) { |
|
|
176 |
if (is_object($value)) { |
|
|
177 |
$result[$key] = array('object', get_class($value)); |
|
|
178 |
} elseif (is_array($value)) { |
|
|
179 |
if ($level > 10) { |
|
|
180 |
$result[$key] = array('array', '*DEEP NESTED ARRAY*'); |
|
|
181 |
} else { |
|
|
182 |
$result[$key] = array('array', $this->flattenArgs($value, ++$level)); |
|
|
183 |
} |
|
|
184 |
} elseif (null === $value) { |
|
|
185 |
$result[$key] = array('null', null); |
|
|
186 |
} elseif (is_bool($value)) { |
|
|
187 |
$result[$key] = array('boolean', $value); |
|
|
188 |
} elseif (is_resource($value)) { |
|
|
189 |
$result[$key] = array('resource', get_resource_type($value)); |
|
|
190 |
} else { |
|
|
191 |
$result[$key] = array('string', (string) $value); |
|
|
192 |
} |
|
|
193 |
} |
|
|
194 |
|
|
|
195 |
return $result; |
|
|
196 |
} |
|
|
197 |
} |