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

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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Event\ViewEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\Templating\TemplateReferenceInterface;
  21. /**
  22.  * The ViewResponseListener class handles the View core event as well as the "@extra:Template" annotation.
  23.  *
  24.  * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  25.  *
  26.  * @internal
  27.  */
  28. class ViewResponseListener implements EventSubscriberInterface
  29. {
  30.     private $viewHandler;
  31.     private $forceView;
  32.     /**
  33.      * Constructor.
  34.      *
  35.      * @param ViewHandlerInterface $viewHandler
  36.      * @param bool                 $forceView
  37.      */
  38.     public function __construct(ViewHandlerInterface $viewHandler$forceView)
  39.     {
  40.         $this->viewHandler $viewHandler;
  41.         $this->forceView $forceView;
  42.     }
  43.     /**
  44.      * Renders the parameters and template and initializes a new response object with the
  45.      * rendered content.
  46.      *
  47.      * @param ViewEvent $event
  48.      */
  49.     public function onKernelView($event)
  50.     {
  51.         $request $event->getRequest();
  52.         if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTEtrue)) {
  53.             return false;
  54.         }
  55.         $configuration $request->attributes->get('_template');
  56.         $view $event->getControllerResult();
  57.         if (!$view instanceof View) {
  58.             if (!$configuration instanceof ViewAnnotation && !$this->forceView) {
  59.                 return;
  60.             }
  61.             $view = new View($view);
  62.         }
  63.         if ($configuration instanceof ViewAnnotation) {
  64.             if ($configuration->getTemplateVar()) {
  65.                 $view->setTemplateVar($configuration->getTemplateVar());
  66.             }
  67.             if (null !== $configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
  68.                 $view->setStatusCode($configuration->getStatusCode());
  69.             }
  70.             $context $view->getContext();
  71.             if ($configuration->getSerializerGroups()) {
  72.                 if (null === $context->getGroups()) {
  73.                     $context->setGroups($configuration->getSerializerGroups());
  74.                 } else {
  75.                     $context->setGroups(array_merge($context->getGroups(), $configuration->getSerializerGroups()));
  76.                 }
  77.             }
  78.             if ($configuration->getSerializerEnableMaxDepthChecks()) {
  79.                 $context->setMaxDepth(0false);
  80.             }
  81.             if (true === $configuration->getSerializerEnableMaxDepthChecks()) {
  82.                 $context->enableMaxDepth();
  83.             } elseif (false === $configuration->getSerializerEnableMaxDepthChecks()) {
  84.                 $context->disableMaxDepth();
  85.             }
  86.             $owner $configuration->getOwner();
  87.             if ([] === $owner || null === $owner) {
  88.                 $controller $action null;
  89.             } else {
  90.                 [$controller$action] = $owner;
  91.             }
  92.             $vars $this->getDefaultVars($configuration$controller$action);
  93.         } else {
  94.             $vars null;
  95.         }
  96.         if (null === $view->getFormat()) {
  97.             $view->setFormat($request->getRequestFormat());
  98.         }
  99.         if ($this->viewHandler->isFormatTemplating($view->getFormat())
  100.             && !$view->getRoute()
  101.             && !$view->getLocation()
  102.         ) {
  103.             if (null !== $vars && !== count($vars)) {
  104.                 $parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
  105.                 foreach ($vars as $var) {
  106.                     if (!array_key_exists($var$parameters)) {
  107.                         $parameters[$var] = $request->attributes->get($var);
  108.                     }
  109.                 }
  110.                 $view->setData($parameters);
  111.             }
  112.             if ($configuration && ($template $configuration->getTemplate()) && !$view->getTemplate()) {
  113.                 if ($template instanceof TemplateReferenceInterface) {
  114.                     $template->set('format'null);
  115.                 }
  116.                 $view->setTemplate($template);
  117.             }
  118.         }
  119.         $response $this->viewHandler->handle($view$request);
  120.         $event->setResponse($response);
  121.     }
  122.     public static function getSubscribedEvents()
  123.     {
  124.         // Must be executed before SensioFrameworkExtraBundle's listener
  125.         return array(
  126.             KernelEvents::VIEW => array('onKernelView'30),
  127.         );
  128.     }
  129.     /**
  130.      * @param Template $template
  131.      * @param object   $controller
  132.      * @param string   $action
  133.      *
  134.      * @return array
  135.      *
  136.      * @see \Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::resolveDefaultParameters()
  137.      */
  138.     private function getDefaultVars(Template $template null$controller$action)
  139.     {
  140.         if (!== count($arguments $template->getVars())) {
  141.             return $arguments;
  142.         }
  143.         if (!$template instanceof ViewAnnotation || $template->isPopulateDefaultVars()) {
  144.             $r = new \ReflectionObject($controller);
  145.             $arguments = array();
  146.             foreach ($r->getMethod($action)->getParameters() as $param) {
  147.                 $arguments[] = $param->getName();
  148.             }
  149.             return $arguments;
  150.         }
  151.     }
  152. }