FlattenException.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Debug\Exception;
  11. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  12. /**
  13. * FlattenException wraps a PHP Exception to be able to serialize it.
  14. *
  15. * Basically, this class removes all objects from the trace.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class FlattenException
  20. {
  21. private $message;
  22. private $code;
  23. private $previous;
  24. private $trace;
  25. private $class;
  26. private $statusCode;
  27. private $headers;
  28. private $file;
  29. private $line;
  30. public static function create(\Exception $exception, $statusCode = null, array $headers = array())
  31. {
  32. $e = new static();
  33. $e->setMessage($exception->getMessage());
  34. $e->setCode($exception->getCode());
  35. if ($exception instanceof HttpExceptionInterface) {
  36. $statusCode = $exception->getStatusCode();
  37. $headers = array_merge($headers, $exception->getHeaders());
  38. }
  39. if (null === $statusCode) {
  40. $statusCode = 500;
  41. }
  42. $e->setStatusCode($statusCode);
  43. $e->setHeaders($headers);
  44. $e->setTraceFromException($exception);
  45. $e->setClass(get_class($exception));
  46. $e->setFile($exception->getFile());
  47. $e->setLine($exception->getLine());
  48. if ($exception->getPrevious()) {
  49. $e->setPrevious(static::create($exception->getPrevious()));
  50. }
  51. return $e;
  52. }
  53. public function toArray()
  54. {
  55. $exceptions = array();
  56. foreach (array_merge(array($this), $this->getAllPrevious()) as $exception) {
  57. $exceptions[] = array(
  58. 'message' => $exception->getMessage(),
  59. 'class' => $exception->getClass(),
  60. 'trace' => $exception->getTrace(),
  61. );
  62. }
  63. return $exceptions;
  64. }
  65. public function getStatusCode()
  66. {
  67. return $this->statusCode;
  68. }
  69. public function setStatusCode($code)
  70. {
  71. $this->statusCode = $code;
  72. }
  73. public function getHeaders()
  74. {
  75. return $this->headers;
  76. }
  77. public function setHeaders(array $headers)
  78. {
  79. $this->headers = $headers;
  80. }
  81. public function getClass()
  82. {
  83. return $this->class;
  84. }
  85. public function setClass($class)
  86. {
  87. $this->class = $class;
  88. }
  89. public function getFile()
  90. {
  91. return $this->file;
  92. }
  93. public function setFile($file)
  94. {
  95. $this->file = $file;
  96. }
  97. public function getLine()
  98. {
  99. return $this->line;
  100. }
  101. public function setLine($line)
  102. {
  103. $this->line = $line;
  104. }
  105. public function getMessage()
  106. {
  107. return $this->message;
  108. }
  109. public function setMessage($message)
  110. {
  111. $this->message = $message;
  112. }
  113. public function getCode()
  114. {
  115. return $this->code;
  116. }
  117. public function setCode($code)
  118. {
  119. $this->code = $code;
  120. }
  121. public function getPrevious()
  122. {
  123. return $this->previous;
  124. }
  125. public function setPrevious(FlattenException $previous)
  126. {
  127. $this->previous = $previous;
  128. }
  129. public function getAllPrevious()
  130. {
  131. $exceptions = array();
  132. $e = $this;
  133. while ($e = $e->getPrevious()) {
  134. $exceptions[] = $e;
  135. }
  136. return $exceptions;
  137. }
  138. public function getTrace()
  139. {
  140. return $this->trace;
  141. }
  142. public function setTraceFromException(\Exception $exception)
  143. {
  144. $trace = $exception->getTrace();
  145. if ($exception instanceof FatalErrorException) {
  146. if (function_exists('xdebug_get_function_stack')) {
  147. $trace = array_slice(array_reverse(xdebug_get_function_stack()), 4);
  148. foreach ($trace as $i => $frame) {
  149. // XDebug pre 2.1.1 doesn't currently set the call type key http://bugs.xdebug.org/view.php?id=695
  150. if (!isset($frame['type'])) {
  151. $trace[$i]['type'] = '??';
  152. }
  153. if ('dynamic' === $trace[$i]['type']) {
  154. $trace[$i]['type'] = '->';
  155. } elseif ('static' === $trace[$i]['type']) {
  156. $trace[$i]['type'] = '::';
  157. }
  158. // XDebug also has a different name for the parameters array
  159. if (isset($frame['params']) && !isset($frame['args'])) {
  160. $trace[$i]['args'] = $frame['params'];
  161. unset($trace[$i]['params']);
  162. }
  163. }
  164. } else {
  165. $trace = array_slice(array_reverse($trace), 1);
  166. }
  167. }
  168. $this->setTrace($trace, $exception->getFile(), $exception->getLine());
  169. }
  170. public function setTrace($trace, $file, $line)
  171. {
  172. $this->trace = array();
  173. $this->trace[] = array(
  174. 'namespace' => '',
  175. 'short_class' => '',
  176. 'class' => '',
  177. 'type' => '',
  178. 'function' => '',
  179. 'file' => $file,
  180. 'line' => $line,
  181. 'args' => array(),
  182. );
  183. foreach ($trace as $entry) {
  184. $class = '';
  185. $namespace = '';
  186. if (isset($entry['class'])) {
  187. $parts = explode('\\', $entry['class']);
  188. $class = array_pop($parts);
  189. $namespace = implode('\\', $parts);
  190. }
  191. $this->trace[] = array(
  192. 'namespace' => $namespace,
  193. 'short_class' => $class,
  194. 'class' => isset($entry['class']) ? $entry['class'] : '',
  195. 'type' => isset($entry['type']) ? $entry['type'] : '',
  196. 'function' => isset($entry['function']) ? $entry['function'] : null,
  197. 'file' => isset($entry['file']) ? $entry['file'] : null,
  198. 'line' => isset($entry['line']) ? $entry['line'] : null,
  199. 'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : array(),
  200. );
  201. }
  202. }
  203. private function flattenArgs($args, $level = 0)
  204. {
  205. $result = array();
  206. foreach ($args as $key => $value) {
  207. if (is_object($value)) {
  208. $result[$key] = array('object', get_class($value));
  209. } elseif (is_array($value)) {
  210. if ($level > 10) {
  211. $result[$key] = array('array', '*DEEP NESTED ARRAY*');
  212. } else {
  213. $result[$key] = array('array', $this->flattenArgs($value, ++$level));
  214. }
  215. } elseif (null === $value) {
  216. $result[$key] = array('null', null);
  217. } elseif (is_bool($value)) {
  218. $result[$key] = array('boolean', $value);
  219. } elseif (is_resource($value)) {
  220. $result[$key] = array('resource', get_resource_type($value));
  221. } elseif ($value instanceof \__PHP_Incomplete_Class) {
  222. // Special case of object, is_object will return false
  223. $result[$key] = array('incomplete-object', $this->getClassNameFromIncomplete($value));
  224. } else {
  225. $result[$key] = array('string', (string) $value);
  226. }
  227. }
  228. return $result;
  229. }
  230. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
  231. {
  232. $array = new \ArrayObject($value);
  233. return $array['__PHP_Incomplete_Class_Name'];
  234. }
  235. }