/vendor/symfony/debug/Exception/FlattenException.php

https://gitlab.com/judielsm/Handora · PHP · 292 lines · 222 code · 45 blank · 25 comment · 13 complexity · 889ba604e1612133e5cab02e910d72fe MD5 · raw file

  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\HttpKernel\Exception;
  11. use Symfony\Component\Debug\Exception\FlattenException as DebugFlattenException;
  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. * @deprecated Deprecated in 2.3, to be removed in 3.0. Use the same class from the Debug component instead.
  20. */
  21. class FlattenException
  22. {
  23. private $handler;
  24. public static function __callStatic($method, $args)
  25. {
  26. if (!method_exists('Symfony\Component\Debug\Exception\FlattenException', $method)) {
  27. throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_called_class(), $method));
  28. }
  29. return call_user_func_array(array('Symfony\Component\Debug\Exception\FlattenException', $method), $args);
  30. }
  31. public function __call($method, $args)
  32. {
  33. if (!isset($this->handler)) {
  34. $this->handler = new DebugFlattenException();
  35. }
  36. if (!method_exists($this->handler, $method)) {
  37. throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $method));
  38. }
  39. return call_user_func_array(array($this->handler, $method), $args);
  40. }
  41. }
  42. namespace Symfony\Component\Debug\Exception;
  43. use Symfony\Component\HttpKernel\Exception\FlattenException as LegacyFlattenException;
  44. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  45. /**
  46. * FlattenException wraps a PHP Exception to be able to serialize it.
  47. *
  48. * Basically, this class removes all objects from the trace.
  49. *
  50. * @author Fabien Potencier <fabien@symfony.com>
  51. */
  52. class FlattenException extends LegacyFlattenException
  53. {
  54. private $message;
  55. private $code;
  56. private $previous;
  57. private $trace;
  58. private $class;
  59. private $statusCode;
  60. private $headers;
  61. private $file;
  62. private $line;
  63. public static function create(\Exception $exception, $statusCode = null, array $headers = array())
  64. {
  65. $e = new static();
  66. $e->setMessage($exception->getMessage());
  67. $e->setCode($exception->getCode());
  68. if ($exception instanceof HttpExceptionInterface) {
  69. $statusCode = $exception->getStatusCode();
  70. $headers = array_merge($headers, $exception->getHeaders());
  71. }
  72. if (null === $statusCode) {
  73. $statusCode = 500;
  74. }
  75. $e->setStatusCode($statusCode);
  76. $e->setHeaders($headers);
  77. $e->setTraceFromException($exception);
  78. $e->setClass(get_class($exception));
  79. $e->setFile($exception->getFile());
  80. $e->setLine($exception->getLine());
  81. if ($exception->getPrevious()) {
  82. $e->setPrevious(static::create($exception->getPrevious()));
  83. }
  84. return $e;
  85. }
  86. public function toArray()
  87. {
  88. $exceptions = array();
  89. foreach (array_merge(array($this), $this->getAllPrevious()) as $exception) {
  90. $exceptions[] = array(
  91. 'message' => $exception->getMessage(),
  92. 'class' => $exception->getClass(),
  93. 'trace' => $exception->getTrace(),
  94. );
  95. }
  96. return $exceptions;
  97. }
  98. public function getStatusCode()
  99. {
  100. return $this->statusCode;
  101. }
  102. public function setStatusCode($code)
  103. {
  104. $this->statusCode = $code;
  105. }
  106. public function getHeaders()
  107. {
  108. return $this->headers;
  109. }
  110. public function setHeaders(array $headers)
  111. {
  112. $this->headers = $headers;
  113. }
  114. public function getClass()
  115. {
  116. return $this->class;
  117. }
  118. public function setClass($class)
  119. {
  120. $this->class = $class;
  121. }
  122. public function getFile()
  123. {
  124. return $this->file;
  125. }
  126. public function setFile($file)
  127. {
  128. $this->file = $file;
  129. }
  130. public function getLine()
  131. {
  132. return $this->line;
  133. }
  134. public function setLine($line)
  135. {
  136. $this->line = $line;
  137. }
  138. public function getMessage()
  139. {
  140. return $this->message;
  141. }
  142. public function setMessage($message)
  143. {
  144. $this->message = $message;
  145. }
  146. public function getCode()
  147. {
  148. return $this->code;
  149. }
  150. public function setCode($code)
  151. {
  152. $this->code = $code;
  153. }
  154. public function getPrevious()
  155. {
  156. return $this->previous;
  157. }
  158. public function setPrevious(FlattenException $previous)
  159. {
  160. $this->previous = $previous;
  161. }
  162. public function getAllPrevious()
  163. {
  164. $exceptions = array();
  165. $e = $this;
  166. while ($e = $e->getPrevious()) {
  167. $exceptions[] = $e;
  168. }
  169. return $exceptions;
  170. }
  171. public function getTrace()
  172. {
  173. return $this->trace;
  174. }
  175. public function setTraceFromException(\Exception $exception)
  176. {
  177. $this->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
  178. }
  179. public function setTrace($trace, $file, $line)
  180. {
  181. $this->trace = array();
  182. $this->trace[] = array(
  183. 'namespace' => '',
  184. 'short_class' => '',
  185. 'class' => '',
  186. 'type' => '',
  187. 'function' => '',
  188. 'file' => $file,
  189. 'line' => $line,
  190. 'args' => array(),
  191. );
  192. foreach ($trace as $entry) {
  193. $class = '';
  194. $namespace = '';
  195. if (isset($entry['class'])) {
  196. $parts = explode('\\', $entry['class']);
  197. $class = array_pop($parts);
  198. $namespace = implode('\\', $parts);
  199. }
  200. $this->trace[] = array(
  201. 'namespace' => $namespace,
  202. 'short_class' => $class,
  203. 'class' => isset($entry['class']) ? $entry['class'] : '',
  204. 'type' => isset($entry['type']) ? $entry['type'] : '',
  205. 'function' => isset($entry['function']) ? $entry['function'] : null,
  206. 'file' => isset($entry['file']) ? $entry['file'] : null,
  207. 'line' => isset($entry['line']) ? $entry['line'] : null,
  208. 'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : array(),
  209. );
  210. }
  211. }
  212. private function flattenArgs($args, $level = 0, &$count = 0)
  213. {
  214. $result = array();
  215. foreach ($args as $key => $value) {
  216. if (++$count > 1e4) {
  217. return array('array', '*SKIPPED over 10000 entries*');
  218. }
  219. if (is_object($value)) {
  220. $result[$key] = array('object', get_class($value));
  221. } elseif (is_array($value)) {
  222. if ($level > 10) {
  223. $result[$key] = array('array', '*DEEP NESTED ARRAY*');
  224. } else {
  225. $result[$key] = array('array', $this->flattenArgs($value, $level + 1, $count));
  226. }
  227. } elseif (null === $value) {
  228. $result[$key] = array('null', null);
  229. } elseif (is_bool($value)) {
  230. $result[$key] = array('boolean', $value);
  231. } elseif (is_resource($value)) {
  232. $result[$key] = array('resource', get_resource_type($value));
  233. } elseif ($value instanceof \__PHP_Incomplete_Class) {
  234. // Special case of object, is_object will return false
  235. $result[$key] = array('incomplete-object', $this->getClassNameFromIncomplete($value));
  236. } else {
  237. $result[$key] = array('string', (string) $value);
  238. }
  239. }
  240. return $result;
  241. }
  242. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
  243. {
  244. $array = new \ArrayObject($value);
  245. return $array['__PHP_Incomplete_Class_Name'];
  246. }
  247. }