Drupal investigation

FlattenException.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. $previous = $exception->getPrevious();
  82. if ($previous instanceof \Exception) {
  83. $e->setPrevious(static::create($previous));
  84. } elseif ($previous instanceof \Throwable) {
  85. $e->setPrevious(static::create(new FatalThrowableError($previous)));
  86. }
  87. return $e;
  88. }
  89. public function toArray()
  90. {
  91. $exceptions = array();
  92. foreach (array_merge(array($this), $this->getAllPrevious()) as $exception) {
  93. $exceptions[] = array(
  94. 'message' => $exception->getMessage(),
  95. 'class' => $exception->getClass(),
  96. 'trace' => $exception->getTrace(),
  97. );
  98. }
  99. return $exceptions;
  100. }
  101. public function getStatusCode()
  102. {
  103. return $this->statusCode;
  104. }
  105. public function setStatusCode($code)
  106. {
  107. $this->statusCode = $code;
  108. }
  109. public function getHeaders()
  110. {
  111. return $this->headers;
  112. }
  113. public function setHeaders(array $headers)
  114. {
  115. $this->headers = $headers;
  116. }
  117. public function getClass()
  118. {
  119. return $this->class;
  120. }
  121. public function setClass($class)
  122. {
  123. $this->class = $class;
  124. }
  125. public function getFile()
  126. {
  127. return $this->file;
  128. }
  129. public function setFile($file)
  130. {
  131. $this->file = $file;
  132. }
  133. public function getLine()
  134. {
  135. return $this->line;
  136. }
  137. public function setLine($line)
  138. {
  139. $this->line = $line;
  140. }
  141. public function getMessage()
  142. {
  143. return $this->message;
  144. }
  145. public function setMessage($message)
  146. {
  147. $this->message = $message;
  148. }
  149. public function getCode()
  150. {
  151. return $this->code;
  152. }
  153. public function setCode($code)
  154. {
  155. $this->code = $code;
  156. }
  157. public function getPrevious()
  158. {
  159. return $this->previous;
  160. }
  161. public function setPrevious(FlattenException $previous)
  162. {
  163. $this->previous = $previous;
  164. }
  165. public function getAllPrevious()
  166. {
  167. $exceptions = array();
  168. $e = $this;
  169. while ($e = $e->getPrevious()) {
  170. $exceptions[] = $e;
  171. }
  172. return $exceptions;
  173. }
  174. public function getTrace()
  175. {
  176. return $this->trace;
  177. }
  178. public function setTraceFromException(\Exception $exception)
  179. {
  180. $this->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
  181. }
  182. public function setTrace($trace, $file, $line)
  183. {
  184. $this->trace = array();
  185. $this->trace[] = array(
  186. 'namespace' => '',
  187. 'short_class' => '',
  188. 'class' => '',
  189. 'type' => '',
  190. 'function' => '',
  191. 'file' => $file,
  192. 'line' => $line,
  193. 'args' => array(),
  194. );
  195. foreach ($trace as $entry) {
  196. $class = '';
  197. $namespace = '';
  198. if (isset($entry['class'])) {
  199. $parts = explode('\\', $entry['class']);
  200. $class = array_pop($parts);
  201. $namespace = implode('\\', $parts);
  202. }
  203. $this->trace[] = array(
  204. 'namespace' => $namespace,
  205. 'short_class' => $class,
  206. 'class' => isset($entry['class']) ? $entry['class'] : '',
  207. 'type' => isset($entry['type']) ? $entry['type'] : '',
  208. 'function' => isset($entry['function']) ? $entry['function'] : null,
  209. 'file' => isset($entry['file']) ? $entry['file'] : null,
  210. 'line' => isset($entry['line']) ? $entry['line'] : null,
  211. 'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : array(),
  212. );
  213. }
  214. }
  215. private function flattenArgs($args, $level = 0, &$count = 0)
  216. {
  217. $result = array();
  218. foreach ($args as $key => $value) {
  219. if (++$count > 1e4) {
  220. return array('array', '*SKIPPED over 10000 entries*');
  221. }
  222. if ($value instanceof \__PHP_Incomplete_Class) {
  223. // is_object() returns false on PHP<=7.1
  224. $result[$key] = array('incomplete-object', $this->getClassNameFromIncomplete($value));
  225. } elseif (is_object($value)) {
  226. $result[$key] = array('object', get_class($value));
  227. } elseif (is_array($value)) {
  228. if ($level > 10) {
  229. $result[$key] = array('array', '*DEEP NESTED ARRAY*');
  230. } else {
  231. $result[$key] = array('array', $this->flattenArgs($value, $level + 1, $count));
  232. }
  233. } elseif (null === $value) {
  234. $result[$key] = array('null', null);
  235. } elseif (is_bool($value)) {
  236. $result[$key] = array('boolean', $value);
  237. } elseif (is_resource($value)) {
  238. $result[$key] = array('resource', get_resource_type($value));
  239. } else {
  240. $result[$key] = array('string', (string) $value);
  241. }
  242. }
  243. return $result;
  244. }
  245. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
  246. {
  247. $array = new \ArrayObject($value);
  248. return $array['__PHP_Incomplete_Class_Name'];
  249. }
  250. }