4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Exception;
14 use Symfony\Component\Debug\Exception\FlattenException as DebugFlattenException;
17 * FlattenException wraps a PHP Exception to be able to serialize it.
19 * Basically, this class removes all objects from the trace.
21 * @author Fabien Potencier <fabien@symfony.com>
23 * @deprecated Deprecated in 2.3, to be removed in 3.0. Use the same class from the Debug component instead.
25 class FlattenException
29 public static function __callStatic($method, $args)
31 if (!method_exists('Symfony\Component\Debug\Exception\FlattenException', $method)) {
32 throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_called_class(), $method));
35 return call_user_func_array(array('Symfony\Component\Debug\Exception\FlattenException', $method), $args);
38 public function __call($method, $args)
40 if (!isset($this->handler)) {
41 $this->handler = new DebugFlattenException();
44 if (!method_exists($this->handler, $method)) {
45 throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $method));
48 return call_user_func_array(array($this->handler, $method), $args);
52 namespace Symfony\Component\Debug\Exception;
54 use Symfony\Component\HttpKernel\Exception\FlattenException as LegacyFlattenException;
55 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
58 * FlattenException wraps a PHP Exception to be able to serialize it.
60 * Basically, this class removes all objects from the trace.
62 * @author Fabien Potencier <fabien@symfony.com>
64 class FlattenException extends LegacyFlattenException
76 public static function create(\Exception $exception, $statusCode = null, array $headers = array())
79 $e->setMessage($exception->getMessage());
80 $e->setCode($exception->getCode());
82 if ($exception instanceof HttpExceptionInterface) {
83 $statusCode = $exception->getStatusCode();
84 $headers = array_merge($headers, $exception->getHeaders());
87 if (null === $statusCode) {
91 $e->setStatusCode($statusCode);
92 $e->setHeaders($headers);
93 $e->setTraceFromException($exception);
94 $e->setClass(get_class($exception));
95 $e->setFile($exception->getFile());
96 $e->setLine($exception->getLine());
98 $previous = $exception->getPrevious();
100 if ($previous instanceof \Exception) {
101 $e->setPrevious(static::create($previous));
102 } elseif ($previous instanceof \Throwable) {
103 $e->setPrevious(static::create(new FatalThrowableError($previous)));
109 public function toArray()
111 $exceptions = array();
112 foreach (array_merge(array($this), $this->getAllPrevious()) as $exception) {
113 $exceptions[] = array(
114 'message' => $exception->getMessage(),
115 'class' => $exception->getClass(),
116 'trace' => $exception->getTrace(),
123 public function getStatusCode()
125 return $this->statusCode;
128 public function setStatusCode($code)
130 $this->statusCode = $code;
133 public function getHeaders()
135 return $this->headers;
138 public function setHeaders(array $headers)
140 $this->headers = $headers;
143 public function getClass()
148 public function setClass($class)
150 $this->class = $class;
153 public function getFile()
158 public function setFile($file)
163 public function getLine()
168 public function setLine($line)
173 public function getMessage()
175 return $this->message;
178 public function setMessage($message)
180 $this->message = $message;
183 public function getCode()
188 public function setCode($code)
193 public function getPrevious()
195 return $this->previous;
198 public function setPrevious(FlattenException $previous)
200 $this->previous = $previous;
203 public function getAllPrevious()
205 $exceptions = array();
207 while ($e = $e->getPrevious()) {
214 public function getTrace()
219 public function setTraceFromException(\Exception $exception)
221 $this->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
224 public function setTrace($trace, $file, $line)
226 $this->trace = array();
227 $this->trace[] = array(
237 foreach ($trace as $entry) {
240 if (isset($entry['class'])) {
241 $parts = explode('\\', $entry['class']);
242 $class = array_pop($parts);
243 $namespace = implode('\\', $parts);
246 $this->trace[] = array(
247 'namespace' => $namespace,
248 'short_class' => $class,
249 'class' => isset($entry['class']) ? $entry['class'] : '',
250 'type' => isset($entry['type']) ? $entry['type'] : '',
251 'function' => isset($entry['function']) ? $entry['function'] : null,
252 'file' => isset($entry['file']) ? $entry['file'] : null,
253 'line' => isset($entry['line']) ? $entry['line'] : null,
254 'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : array(),
259 private function flattenArgs($args, $level = 0, &$count = 0)
262 foreach ($args as $key => $value) {
263 if (++$count > 1e4) {
264 return array('array', '*SKIPPED over 10000 entries*');
266 if ($value instanceof \__PHP_Incomplete_Class) {
267 // is_object() returns false on PHP<=7.1
268 $result[$key] = array('incomplete-object', $this->getClassNameFromIncomplete($value));
269 } elseif (is_object($value)) {
270 $result[$key] = array('object', get_class($value));
271 } elseif (is_array($value)) {
273 $result[$key] = array('array', '*DEEP NESTED ARRAY*');
275 $result[$key] = array('array', $this->flattenArgs($value, $level + 1, $count));
277 } elseif (null === $value) {
278 $result[$key] = array('null', null);
279 } elseif (is_bool($value)) {
280 $result[$key] = array('boolean', $value);
281 } elseif (is_resource($value)) {
282 $result[$key] = array('resource', get_resource_type($value));
284 $result[$key] = array('string', (string) $value);
291 private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
293 $array = new \ArrayObject($value);
295 return $array['__PHP_Incomplete_Class_Name'];