Exemplo n.º 1
0
 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event
  * @return array|mixed
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     if (!$view instanceof View) {
         if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
             return parent::onKernelView($event);
         }
         $view = new View($view);
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if (null === $view->getStatusCode() && $configuration->getStatusCode()) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         if ($configuration->getSerializerGroups()) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->setGroups($configuration->getSerializerGroups());
             $view->setSerializationContext($context);
         }
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     $viewHandler = $this->container->get('fos_rest.view_handler');
     if ($viewHandler->isFormatTemplating($view->getFormat())) {
         if (!empty($vars)) {
             $parameters = (array) $viewHandler->prepareTemplateParameters($view);
             foreach ($vars as $var) {
                 if (!array_key_exists($var, $parameters)) {
                     $parameters[$var] = $request->attributes->get($var);
                 }
             }
             $view->setData($parameters);
         }
         $template = $request->attributes->get('_template');
         if ($template) {
             if ($template instanceof TemplateReference) {
                 $template->set('format', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $viewHandler->handle($view, $request);
     $event->setResponse($response);
 }
Exemplo n.º 2
0
 /**
  * Handles creation of a Response using either redirection or the templating/serializer service
  *
  * @param View    $view
  * @param Request $request
  * @param string  $format
  *
  * @return Response
  */
 public function createResponse($view, Request $request, $format)
 {
     $route = $view->getRoute();
     $location = $route ? $this->getRouter()->generate($route, (array) $view->getData(), true) : $view->getLocation();
     if ($location) {
         return $this->createRedirectResponse($view, $location, $format);
     }
     $content = null;
     if ($this->isFormatTemplating($format)) {
         $content = $this->renderTemplate($view, $format);
     } elseif ($this->serializeNull || null !== $view->getData()) {
         $serializer = $this->getSerializer($view);
         if ($serializer instanceof Serializer) {
             $context = $this->getSerializationContext($view);
             $content = $serializer->serialize($view->getData(), $format, $context);
         } else {
             $content = $serializer->serialize($view->getData(), $format);
         }
     }
     $response = $view->getResponse();
     $response->setStatusCode($this->getStatusCode($view, $content));
     if (null !== $content) {
         $response->setContent($content);
     }
     if (!$response->headers->has('Content-Type')) {
         $response->headers->set('Content-Type', $request->getMimeType($format));
     }
     return $response;
 }