[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/debug/Symfony/Component/Debug/Exception/ -> FlattenException.php (source)

   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\HttpKernel\Exception;
  13  
  14  use Symfony\Component\Debug\Exception\FlattenException as DebugFlattenException;
  15  
  16  /**
  17   * FlattenException wraps a PHP Exception to be able to serialize it.
  18   *
  19   * Basically, this class removes all objects from the trace.
  20   *
  21   * @author Fabien Potencier <fabien@symfony.com>
  22   *
  23   * @deprecated Deprecated in 2.3, to be removed in 3.0. Use the same class from the Debug component instead.
  24   */
  25  class FlattenException
  26  {
  27      private $handler;
  28  
  29      public static function __callStatic($method, $args)
  30      {
  31          if (!method_exists('Symfony\Component\Debug\Exception\FlattenException', $method)) {
  32              throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_called_class(), $method));
  33          }
  34  
  35          return call_user_func_array(array('Symfony\Component\Debug\Exception\FlattenException', $method), $args);
  36      }
  37  
  38      public function __call($method, $args)
  39      {
  40          if (!isset($this->handler)) {
  41              $this->handler = new DebugFlattenException();
  42          }
  43  
  44          if (!method_exists($this->handler, $method)) {
  45              throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $method));
  46          }
  47  
  48          return call_user_func_array(array($this->handler, $method), $args);
  49      }
  50  }
  51  
  52  namespace Symfony\Component\Debug\Exception;
  53  
  54  use Symfony\Component\HttpKernel\Exception\FlattenException as LegacyFlattenException;
  55  use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  56  
  57  /**
  58   * FlattenException wraps a PHP Exception to be able to serialize it.
  59   *
  60   * Basically, this class removes all objects from the trace.
  61   *
  62   * @author Fabien Potencier <fabien@symfony.com>
  63   */
  64  class FlattenException extends LegacyFlattenException
  65  {
  66      private $message;
  67      private $code;
  68      private $previous;
  69      private $trace;
  70      private $class;
  71      private $statusCode;
  72      private $headers;
  73      private $file;
  74      private $line;
  75  
  76      public static function create(\Exception $exception, $statusCode = null, array $headers = array())
  77      {
  78          $e = new static();
  79          $e->setMessage($exception->getMessage());
  80          $e->setCode($exception->getCode());
  81  
  82          if ($exception instanceof HttpExceptionInterface) {
  83              $statusCode = $exception->getStatusCode();
  84              $headers = array_merge($headers, $exception->getHeaders());
  85          }
  86  
  87          if (null === $statusCode) {
  88              $statusCode = 500;
  89          }
  90  
  91          $e->setStatusCode($statusCode);
  92          $e->setHeaders($headers);
  93          $e->setTraceFromException($exception);
  94          $e->setClass(get_class($exception));
  95          $e->setFile($exception->getFile());
  96          $e->setLine($exception->getLine());
  97          if ($exception->getPrevious()) {
  98              $e->setPrevious(static::create($exception->getPrevious()));
  99          }
 100  
 101          return $e;
 102      }
 103  
 104      public function toArray()
 105      {
 106          $exceptions = array();
 107          foreach (array_merge(array($this), $this->getAllPrevious()) as $exception) {
 108              $exceptions[] = array(
 109                  'message' => $exception->getMessage(),
 110                  'class' => $exception->getClass(),
 111                  'trace' => $exception->getTrace(),
 112              );
 113          }
 114  
 115          return $exceptions;
 116      }
 117  
 118      public function getStatusCode()
 119      {
 120          return $this->statusCode;
 121      }
 122  
 123      public function setStatusCode($code)
 124      {
 125          $this->statusCode = $code;
 126      }
 127  
 128      public function getHeaders()
 129      {
 130          return $this->headers;
 131      }
 132  
 133      public function setHeaders(array $headers)
 134      {
 135          $this->headers = $headers;
 136      }
 137  
 138      public function getClass()
 139      {
 140          return $this->class;
 141      }
 142  
 143      public function setClass($class)
 144      {
 145          $this->class = $class;
 146      }
 147  
 148      public function getFile()
 149      {
 150          return $this->file;
 151      }
 152  
 153      public function setFile($file)
 154      {
 155          $this->file = $file;
 156      }
 157  
 158      public function getLine()
 159      {
 160          return $this->line;
 161      }
 162  
 163      public function setLine($line)
 164      {
 165          $this->line = $line;
 166      }
 167  
 168      public function getMessage()
 169      {
 170          return $this->message;
 171      }
 172  
 173      public function setMessage($message)
 174      {
 175          $this->message = $message;
 176      }
 177  
 178      public function getCode()
 179      {
 180          return $this->code;
 181      }
 182  
 183      public function setCode($code)
 184      {
 185          $this->code = $code;
 186      }
 187  
 188      public function getPrevious()
 189      {
 190          return $this->previous;
 191      }
 192  
 193      public function setPrevious(FlattenException $previous)
 194      {
 195          $this->previous = $previous;
 196      }
 197  
 198      public function getAllPrevious()
 199      {
 200          $exceptions = array();
 201          $e = $this;
 202          while ($e = $e->getPrevious()) {
 203              $exceptions[] = $e;
 204          }
 205  
 206          return $exceptions;
 207      }
 208  
 209      public function getTrace()
 210      {
 211          return $this->trace;
 212      }
 213  
 214      public function setTraceFromException(\Exception $exception)
 215      {
 216          $trace = $exception->getTrace();
 217  
 218          if ($exception instanceof FatalErrorException) {
 219              if (function_exists('xdebug_get_function_stack')) {
 220                  $trace = array_slice(array_reverse(xdebug_get_function_stack()), 4);
 221  
 222                  foreach ($trace as $i => $frame) {
 223                      //  XDebug pre 2.1.1 doesn't currently set the call type key http://bugs.xdebug.org/view.php?id=695
 224                      if (!isset($frame['type'])) {
 225                          $trace[$i]['type'] = '??';
 226                      }
 227  
 228                      if ('dynamic' === $trace[$i]['type']) {
 229                          $trace[$i]['type'] = '->';
 230                      } elseif ('static' === $trace[$i]['type']) {
 231                          $trace[$i]['type'] = '::';
 232                      }
 233  
 234                      // XDebug also has a different name for the parameters array
 235                      if (isset($frame['params']) && !isset($frame['args'])) {
 236                          $trace[$i]['args'] = $frame['params'];
 237                          unset($trace[$i]['params']);
 238                      }
 239                  }
 240              } else {
 241                  $trace = array_slice(array_reverse($trace), 1);
 242              }
 243          }
 244  
 245          $this->setTrace($trace, $exception->getFile(), $exception->getLine());
 246      }
 247  
 248      public function setTrace($trace, $file, $line)
 249      {
 250          $this->trace = array();
 251          $this->trace[] = array(
 252              'namespace' => '',
 253              'short_class' => '',
 254              'class' => '',
 255              'type' => '',
 256              'function' => '',
 257              'file' => $file,
 258              'line' => $line,
 259              'args' => array(),
 260          );
 261          foreach ($trace as $entry) {
 262              $class = '';
 263              $namespace = '';
 264              if (isset($entry['class'])) {
 265                  $parts = explode('\\', $entry['class']);
 266                  $class = array_pop($parts);
 267                  $namespace = implode('\\', $parts);
 268              }
 269  
 270              $this->trace[] = array(
 271                  'namespace' => $namespace,
 272                  'short_class' => $class,
 273                  'class' => isset($entry['class']) ? $entry['class'] : '',
 274                  'type' => isset($entry['type']) ? $entry['type'] : '',
 275                  'function' => isset($entry['function']) ? $entry['function'] : null,
 276                  'file' => isset($entry['file']) ? $entry['file'] : null,
 277                  'line' => isset($entry['line']) ? $entry['line'] : null,
 278                  'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : array(),
 279              );
 280          }
 281      }
 282  
 283      private function flattenArgs($args, $level = 0, &$count = 0)
 284      {
 285          $result = array();
 286          foreach ($args as $key => $value) {
 287              if (++$count > 1e4) {
 288                  return array('array', '*SKIPPED over 10000 entries*');
 289              }
 290              if (is_object($value)) {
 291                  $result[$key] = array('object', get_class($value));
 292              } elseif (is_array($value)) {
 293                  if ($level > 10) {
 294                      $result[$key] = array('array', '*DEEP NESTED ARRAY*');
 295                  } else {
 296                      $result[$key] = array('array', $this->flattenArgs($value, $level + 1, $count));
 297                  }
 298              } elseif (null === $value) {
 299                  $result[$key] = array('null', null);
 300              } elseif (is_bool($value)) {
 301                  $result[$key] = array('boolean', $value);
 302              } elseif (is_resource($value)) {
 303                  $result[$key] = array('resource', get_resource_type($value));
 304              } elseif ($value instanceof \__PHP_Incomplete_Class) {
 305                  // Special case of object, is_object will return false
 306                  $result[$key] = array('incomplete-object', $this->getClassNameFromIncomplete($value));
 307              } else {
 308                  $result[$key] = array('string', (string) $value);
 309              }
 310          }
 311  
 312          return $result;
 313      }
 314  
 315      private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
 316      {
 317          $array = new \ArrayObject($value);
 318  
 319          return $array['__PHP_Incomplete_Class_Name'];
 320      }
 321  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1