PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/vendor/symfony/debug/Symfony/Component/Debug/Exception/FlattenException.php

https://github.com/AJenbo/ubuntudanmark.dk
PHP | 321 lines | 244 code | 50 blank | 27 comment | 20 complexity | 44a1a48ff4c1c4c318b7f27b81cf3e54 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. $trace = $exception->getTrace();
  178. if ($exception instanceof FatalErrorException) {
  179. if (function_exists('xdebug_get_function_stack')) {
  180. $trace = array_slice(array_reverse(xdebug_get_function_stack()), 4);
  181. foreach ($trace as $i => $frame) {
  182. // XDebug pre 2.1.1 doesn't currently set the call type key http://bugs.xdebug.org/view.php?id=695
  183. if (!isset($frame['type'])) {
  184. $trace[$i]['type'] = '??';
  185. }
  186. if ('dynamic' === $trace[$i]['type']) {
  187. $trace[$i]['type'] = '->';
  188. } elseif ('static' === $trace[$i]['type']) {
  189. $trace[$i]['type'] = '::';
  190. }
  191. // XDebug also has a different name for the parameters array
  192. if (isset($frame['params']) && !isset($frame['args'])) {
  193. $trace[$i]['args'] = $frame['params'];
  194. unset($trace[$i]['params']);
  195. }
  196. }
  197. } else {
  198. $trace = array_slice(array_reverse($trace), 1);
  199. }
  200. }
  201. $this->setTrace($trace, $exception->getFile(), $exception->getLine());
  202. }
  203. public function setTrace($trace, $file, $line)
  204. {
  205. $this->trace = array();
  206. $this->trace[] = array(
  207. 'namespace' => '',
  208. 'short_class' => '',
  209. 'class' => '',
  210. 'type' => '',
  211. 'function' => '',
  212. 'file' => $file,
  213. 'line' => $line,
  214. 'args' => array(),
  215. );
  216. foreach ($trace as $entry) {
  217. $class = '';
  218. $namespace = '';
  219. if (isset($entry['class'])) {
  220. $parts = explode('\\', $entry['class']);
  221. $class = array_pop($parts);
  222. $namespace = implode('\\', $parts);
  223. }
  224. $this->trace[] = array(
  225. 'namespace' => $namespace,
  226. 'short_class' => $class,
  227. 'class' => isset($entry['class']) ? $entry['class'] : '',
  228. 'type' => isset($entry['type']) ? $entry['type'] : '',
  229. 'function' => isset($entry['function']) ? $entry['function'] : null,
  230. 'file' => isset($entry['file']) ? $entry['file'] : null,
  231. 'line' => isset($entry['line']) ? $entry['line'] : null,
  232. 'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : array(),
  233. );
  234. }
  235. }
  236. private function flattenArgs($args, $level = 0, &$count = 0)
  237. {
  238. $result = array();
  239. foreach ($args as $key => $value) {
  240. if (++$count > 1e4) {
  241. return array('array', '*SKIPPED over 10000 entries*');
  242. }
  243. if (is_object($value)) {
  244. $result[$key] = array('object', get_class($value));
  245. } elseif (is_array($value)) {
  246. if ($level > 10) {
  247. $result[$key] = array('array', '*DEEP NESTED ARRAY*');
  248. } else {
  249. $result[$key] = array('array', $this->flattenArgs($value, $level + 1, $count));
  250. }
  251. } elseif (null === $value) {
  252. $result[$key] = array('null', null);
  253. } elseif (is_bool($value)) {
  254. $result[$key] = array('boolean', $value);
  255. } elseif (is_resource($value)) {
  256. $result[$key] = array('resource', get_resource_type($value));
  257. } elseif ($value instanceof \__PHP_Incomplete_Class) {
  258. // Special case of object, is_object will return false
  259. $result[$key] = array('incomplete-object', $this->getClassNameFromIncomplete($value));
  260. } else {
  261. $result[$key] = array('string', (string) $value);
  262. }
  263. }
  264. return $result;
  265. }
  266. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
  267. {
  268. $array = new \ArrayObject($value);
  269. return $array['__PHP_Incomplete_Class_Name'];
  270. }
  271. }