[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/error-handler/ErrorRenderer/ -> SerializerErrorRenderer.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <[email protected]>
   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\ErrorHandler\ErrorRenderer;
  13  
  14  use Symfony\Component\ErrorHandler\Exception\FlattenException;
  15  use Symfony\Component\HttpFoundation\Request;
  16  use Symfony\Component\HttpFoundation\RequestStack;
  17  use Symfony\Component\Serializer\Exception\NotEncodableValueException;
  18  use Symfony\Component\Serializer\SerializerInterface;
  19  
  20  /**
  21   * Formats an exception using Serializer for rendering.
  22   *
  23   * @author Nicolas Grekas <[email protected]>
  24   */
  25  class SerializerErrorRenderer implements ErrorRendererInterface
  26  {
  27      private $serializer;
  28      private $format;
  29      private $fallbackErrorRenderer;
  30      private $debug;
  31  
  32      /**
  33       * @param string|callable(FlattenException) $format The format as a string or a callable that should return it
  34       *                                                  formats not supported by Request::getMimeTypes() should be given as mime types
  35       * @param bool|callable                     $debug  The debugging mode as a boolean or a callable that should return it
  36       */
  37      public function __construct(SerializerInterface $serializer, $format, ErrorRendererInterface $fallbackErrorRenderer = null, $debug = false)
  38      {
  39          if (!\is_string($format) && !\is_callable($format)) {
  40              throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be a string or a callable, "%s" given.', __METHOD__, \gettype($format)));
  41          }
  42  
  43          if (!\is_bool($debug) && !\is_callable($debug)) {
  44              throw new \TypeError(sprintf('Argument 4 passed to "%s()" must be a boolean or a callable, "%s" given.', __METHOD__, \gettype($debug)));
  45          }
  46  
  47          $this->serializer = $serializer;
  48          $this->format = $format;
  49          $this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
  50          $this->debug = $debug;
  51      }
  52  
  53      /**
  54       * {@inheritdoc}
  55       */
  56      public function render(\Throwable $exception): FlattenException
  57      {
  58          $headers = [];
  59          $debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
  60          if ($debug) {
  61              $headers['X-Debug-Exception'] = rawurlencode($exception->getMessage());
  62              $headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
  63          }
  64  
  65          $flattenException = FlattenException::createFromThrowable($exception, null, $headers);
  66  
  67          try {
  68              $format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
  69              $headers = [
  70                  'Content-Type' => Request::getMimeTypes($format)[0] ?? $format,
  71                  'Vary' => 'Accept',
  72              ];
  73  
  74              return $flattenException->setAsString($this->serializer->serialize($flattenException, $format, [
  75                  'exception' => $exception,
  76                  'debug' => $debug,
  77              ]))
  78              ->setHeaders($flattenException->getHeaders() + $headers);
  79          } catch (NotEncodableValueException $e) {
  80              return $this->fallbackErrorRenderer->render($exception);
  81          }
  82      }
  83  
  84      public static function getPreferredFormat(RequestStack $requestStack): \Closure
  85      {
  86          return static function () use ($requestStack) {
  87              if (!$request = $requestStack->getCurrentRequest()) {
  88                  throw new NotEncodableValueException();
  89              }
  90  
  91              return $request->getPreferredFormat();
  92          };
  93      }
  94  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer