vendor/friendsofsymfony/rest-bundle/Controller/ExceptionController.php line 14

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSRestBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\RestBundle\Controller;
  11. @trigger_error(sprintf('The %s\ExceptionController class is deprecated since FOSRestBundle 2.8.'__NAMESPACE__), E_USER_DEPRECATED);
  12. use FOS\RestBundle\Exception\FlattenException as FosFlattenException;
  13. use FOS\RestBundle\Util\ExceptionValueMap;
  14. use FOS\RestBundle\View\View;
  15. use FOS\RestBundle\View\ViewHandlerInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  19. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  20. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  21. /**
  22.  * Custom ExceptionController that uses the view layer and supports HTTP response status code mapping.
  23.  *
  24.  * @deprecated since 2.8
  25.  */
  26. class ExceptionController
  27. {
  28.     private $viewHandler;
  29.     private $exceptionCodes;
  30.     private $showException;
  31.     public function __construct(
  32.         ViewHandlerInterface $viewHandler,
  33.         ExceptionValueMap $exceptionCodes,
  34.         bool $showException
  35.     ) {
  36.         $this->viewHandler $viewHandler;
  37.         $this->exceptionCodes $exceptionCodes;
  38.         $this->showException $showException;
  39.     }
  40.     /**
  41.      * @param \Throwable $exception
  42.      */
  43.     public function showAction(Request $request$exceptionDebugLoggerInterface $logger null)
  44.     {
  45.         $currentContent $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
  46.         if ($exception instanceof \Exception) {
  47.             $code $this->getStatusCode($exception);
  48.         } else {
  49.             $code $this->getStatusCodeFromThrowable($exception);
  50.         }
  51.         $templateData $this->getTemplateData($currentContent$code$exception$logger);
  52.         if ($exception instanceof \Exception) {
  53.             $view $this->createView($exception$code$templateData$request$this->showException);
  54.         } else {
  55.             $view $this->createViewFromThrowable($exception$code$templateData$request$this->showException);
  56.         }
  57.         $response $this->viewHandler->handle($view);
  58.         return $response;
  59.     }
  60.     /**
  61.      * @param int  $code
  62.      * @param bool $showException
  63.      *
  64.      * @return View
  65.      */
  66.     protected function createView(\Exception $exception$code, array $templateDataRequest $request$showException)
  67.     {
  68.         return $this->createViewFromThrowable($exception$code$templateData);
  69.     }
  70.     /**
  71.      * @return int
  72.      */
  73.     protected function getStatusCode(\Exception $exception)
  74.     {
  75.         return $this->getStatusCodeFromThrowable($exception);
  76.     }
  77.     private function createViewFromThrowable(\Throwable $exception$code, array $templateData): View
  78.     {
  79.         $view = new View($exception$code$exception instanceof HttpExceptionInterface $exception->getHeaders() : []);
  80.         $view->setTemplateVar('raw_exception'false);
  81.         $view->setTemplateData($templateDatafalse);
  82.         return $view;
  83.     }
  84.     private function getTemplateData(string $currentContentint $code, \Throwable $throwableDebugLoggerInterface $logger null): array
  85.     {
  86.         if (class_exists(FlattenException::class)) {
  87.             $exception FlattenException::createFromThrowable($throwable);
  88.         } else {
  89.             $exception FosFlattenException::createFromThrowable($throwable);
  90.         }
  91.         return [
  92.             'exception' => $exception,
  93.             'status' => 'error',
  94.             'status_code' => $code,
  95.             'status_text' => array_key_exists($codeResponse::$statusTexts) ? Response::$statusTexts[$code] : 'error',
  96.             'currentContent' => $currentContent,
  97.             'logger' => $logger,
  98.         ];
  99.     }
  100.     /**
  101.      * Gets and cleans any content that was already outputted.
  102.      *
  103.      * This code comes from Symfony and should be synchronized on a regular basis
  104.      * see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
  105.      */
  106.     private function getAndCleanOutputBuffering($startObLevel): string
  107.     {
  108.         if (ob_get_level() <= $startObLevel) {
  109.             return '';
  110.         }
  111.         Response::closeOutputBuffers($startObLevel 1true);
  112.         return ob_get_clean();
  113.     }
  114.     private function getStatusCodeFromThrowable(\Throwable $exception): int
  115.     {
  116.         // If matched
  117.         if ($statusCode $this->exceptionCodes->resolveThrowable($exception)) {
  118.             return $statusCode;
  119.         }
  120.         // Otherwise, default
  121.         if ($exception instanceof HttpExceptionInterface) {
  122.             return $exception->getStatusCode();
  123.         }
  124.         return 500;
  125.     }
  126. }