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