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\GetResponseForControllerResultEvent;
  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 GetResponseForControllerResultEvent $event
  48.      */
  49.     public function onKernelView(GetResponseForControllerResultEvent $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.                 $context->addGroups($configuration->getSerializerGroups());
  73.             }
  74.             if ($configuration->getSerializerEnableMaxDepthChecks()) {
  75.                 $context->setMaxDepth(0false);
  76.             }
  77.             if (true === $configuration->getSerializerEnableMaxDepthChecks()) {
  78.                 $context->enableMaxDepth();
  79.             } elseif (false === $configuration->getSerializerEnableMaxDepthChecks()) {
  80.                 $context->disableMaxDepth();
  81.             }
  82.             list($controller$action) = $configuration->getOwner();
  83.             $vars $this->getDefaultVars($configuration$controller$action);
  84.         } else {
  85.             $vars null;
  86.         }
  87.         if (null === $view->getFormat()) {
  88.             $view->setFormat($request->getRequestFormat());
  89.         }
  90.         if ($this->viewHandler->isFormatTemplating($view->getFormat())
  91.             && !$view->getRoute()
  92.             && !$view->getLocation()
  93.         ) {
  94.             if (null !== $vars && !== count($vars)) {
  95.                 $parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
  96.                 foreach ($vars as $var) {
  97.                     if (!array_key_exists($var$parameters)) {
  98.                         $parameters[$var] = $request->attributes->get($var);
  99.                     }
  100.                 }
  101.                 $view->setData($parameters);
  102.             }
  103.             if ($configuration && ($template $configuration->getTemplate()) && !$view->getTemplate()) {
  104.                 if ($template instanceof TemplateReferenceInterface) {
  105.                     $template->set('format'null);
  106.                 }
  107.                 $view->setTemplate($template);
  108.             }
  109.         }
  110.         $response $this->viewHandler->handle($view$request);
  111.         $event->setResponse($response);
  112.     }
  113.     public static function getSubscribedEvents()
  114.     {
  115.         // Must be executed before SensioFrameworkExtraBundle's listener
  116.         return array(
  117.             KernelEvents::VIEW => array('onKernelView'30),
  118.         );
  119.     }
  120.     /**
  121.      * @param Request  $request
  122.      * @param Template $template
  123.      * @param object   $controller
  124.      * @param string   $action
  125.      *
  126.      * @return array
  127.      *
  128.      * @see \Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::resolveDefaultParameters()
  129.      */
  130.     private function getDefaultVars(Template $template null$controller$action)
  131.     {
  132.         if (!== count($arguments $template->getVars())) {
  133.             return $arguments;
  134.         }
  135.         if (!$template instanceof ViewAnnotation || $template->isPopulateDefaultVars()) {
  136.             $r = new \ReflectionObject($controller);
  137.             $arguments = array();
  138.             foreach ($r->getMethod($action)->getParameters() as $param) {
  139.                 $arguments[] = $param->getName();
  140.             }
  141.             return $arguments;
  142.         }
  143.     }
  144. }