vendor/friendsofsymfony/rest-bundle/EventListener/ViewResponseListener.php line 41

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\EventListener;
  11. use FOS\RestBundle\Controller\Annotations\View as ViewAnnotation;
  12. use FOS\RestBundle\FOSRestBundle;
  13. use FOS\RestBundle\View\View;
  14. use FOS\RestBundle\View\ViewHandlerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Event\ViewEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. /**
  20.  * The ViewResponseListener class handles the View core event as well as the "@extra:Template" annotation.
  21.  *
  22.  * @author Lukas Kahwe Smith <[email protected]>
  23.  *
  24.  * @internal
  25.  */
  26. class ViewResponseListener implements EventSubscriberInterface
  27. {
  28.     private $viewHandler;
  29.     private $forceView;
  30.     public function __construct(ViewHandlerInterface $viewHandlerbool $forceView)
  31.     {
  32.         $this->viewHandler $viewHandler;
  33.         $this->forceView $forceView;
  34.     }
  35.     public function onKernelView(ViewEvent $event): void
  36.     {
  37.         $request $event->getRequest();
  38.         if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTEtrue)) {
  39.             return;
  40.         }
  41.         $configuration $request->attributes->get('_template');
  42.         $view $event->getControllerResult();
  43.         if (!$view instanceof View) {
  44.             if (!$configuration instanceof ViewAnnotation && !$this->forceView) {
  45.                 return;
  46.             }
  47.             $view = new View($view);
  48.         }
  49.         if ($configuration instanceof ViewAnnotation) {
  50.             if (null !== $configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
  51.                 $view->setStatusCode($configuration->getStatusCode());
  52.             }
  53.             $context $view->getContext();
  54.             if ($configuration->getSerializerGroups()) {
  55.                 if (null === $context->getGroups()) {
  56.                     $context->setGroups($configuration->getSerializerGroups());
  57.                 } else {
  58.                     $context->setGroups(array_merge($context->getGroups(), $configuration->getSerializerGroups()));
  59.                 }
  60.             }
  61.             if (true === $configuration->getSerializerEnableMaxDepthChecks()) {
  62.                 $context->enableMaxDepth();
  63.             } elseif (false === $configuration->getSerializerEnableMaxDepthChecks()) {
  64.                 $context->disableMaxDepth();
  65.             }
  66.         }
  67.         if (null === $view->getFormat()) {
  68.             $view->setFormat($request->getRequestFormat());
  69.         }
  70.         $response $this->viewHandler->handle($view$request);
  71.         $event->setResponse($response);
  72.     }
  73.     public static function getSubscribedEvents(): array
  74.     {
  75.         // Must be executed before SensioFrameworkExtraBundle's listener
  76.         return [
  77.             KernelEvents::VIEW => ['onKernelView'30],
  78.         ];
  79.     }
  80. }