123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Debug\Exception;
- use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
- /**
- * FlattenException wraps a PHP Exception to be able to serialize it.
- *
- * Basically, this class removes all objects from the trace.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
- class FlattenException
- {
- private $message;
- private $code;
- private $previous;
- private $trace;
- private $class;
- private $statusCode;
- private $headers;
- private $file;
- private $line;
- public static function create(\Exception $exception, $statusCode = null, array $headers = array())
- {
- $e = new static();
- $e->setMessage($exception->getMessage());
- $e->setCode($exception->getCode());
- if ($exception instanceof HttpExceptionInterface) {
- $statusCode = $exception->getStatusCode();
- $headers = array_merge($headers, $exception->getHeaders());
- }
- if (null === $statusCode) {
- $statusCode = 500;
- }
- $e->setStatusCode($statusCode);
- $e->setHeaders($headers);
- $e->setTraceFromException($exception);
- $e->setClass(get_class($exception));
- $e->setFile($exception->getFile());
- $e->setLine($exception->getLine());
- $previous = $exception->getPrevious();
- if ($previous instanceof \Exception) {
- $e->setPrevious(static::create($previous));
- } elseif ($previous instanceof \Throwable) {
- $e->setPrevious(static::create(new FatalThrowableError($previous)));
- }
- return $e;
- }
- public function toArray()
- {
- $exceptions = array();
- foreach (array_merge(array($this), $this->getAllPrevious()) as $exception) {
- $exceptions[] = array(
- 'message' => $exception->getMessage(),
- 'class' => $exception->getClass(),
- 'trace' => $exception->getTrace(),
- );
- }
- return $exceptions;
- }
- public function getStatusCode()
- {
- return $this->statusCode;
- }
- public function setStatusCode($code)
- {
- $this->statusCode = $code;
- }
- public function getHeaders()
- {
- return $this->headers;
- }
- public function setHeaders(array $headers)
- {
- $this->headers = $headers;
- }
- public function getClass()
- {
- return $this->class;
- }
- public function setClass($class)
- {
- $this->class = $class;
- }
- public function getFile()
- {
- return $this->file;
- }
- public function setFile($file)
- {
- $this->file = $file;
- }
- public function getLine()
- {
- return $this->line;
- }
- public function setLine($line)
- {
- $this->line = $line;
- }
- public function getMessage()
- {
- return $this->message;
- }
- public function setMessage($message)
- {
- $this->message = $message;
- }
- public function getCode()
- {
- return $this->code;
- }
- public function setCode($code)
- {
- $this->code = $code;
- }
- public function getPrevious()
- {
- return $this->previous;
- }
- public function setPrevious(FlattenException $previous)
- {
- $this->previous = $previous;
- }
- public function getAllPrevious()
- {
- $exceptions = array();
- $e = $this;
- while ($e = $e->getPrevious()) {
- $exceptions[] = $e;
- }
- return $exceptions;
- }
- public function getTrace()
- {
- return $this->trace;
- }
- public function setTraceFromException(\Exception $exception)
- {
- $this->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
- }
- public function setTrace($trace, $file, $line)
- {
- $this->trace = array();
- $this->trace[] = array(
- 'namespace' => '',
- 'short_class' => '',
- 'class' => '',
- 'type' => '',
- 'function' => '',
- 'file' => $file,
- 'line' => $line,
- 'args' => array(),
- );
- foreach ($trace as $entry) {
- $class = '';
- $namespace = '';
- if (isset($entry['class'])) {
- $parts = explode('\\', $entry['class']);
- $class = array_pop($parts);
- $namespace = implode('\\', $parts);
- }
- $this->trace[] = array(
- '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']) : array(),
- );
- }
- }
- private function flattenArgs($args, $level = 0, &$count = 0)
- {
- $result = array();
- foreach ($args as $key => $value) {
- if (++$count > 1e4) {
- return array('array', '*SKIPPED over 10000 entries*');
- }
- if (is_object($value)) {
- $result[$key] = array('object', get_class($value));
- } elseif (is_array($value)) {
- if ($level > 10) {
- $result[$key] = array('array', '*DEEP NESTED ARRAY*');
- } else {
- $result[$key] = array('array', $this->flattenArgs($value, $level + 1, $count));
- }
- } elseif (null === $value) {
- $result[$key] = array('null', null);
- } elseif (is_bool($value)) {
- $result[$key] = array('boolean', $value);
- } elseif (is_resource($value)) {
- $result[$key] = array('resource', get_resource_type($value));
- } elseif ($value instanceof \__PHP_Incomplete_Class) {
- // Special case of object, is_object will return false
- $result[$key] = array('incomplete-object', $this->getClassNameFromIncomplete($value));
- } else {
- $result[$key] = array('string', (string) $value);
- }
- }
- return $result;
- }
- private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
- {
- $array = new \ArrayObject($value);
- return $array['__PHP_Incomplete_Class_Name'];
- }
- }
|