/**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
  */
 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 (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);
 }
Esempio n. 2
0
 public function testSetFormat()
 {
     $view = new View();
     $format = 'bar';
     $view->setFormat($format);
     $this->assertEquals($format, $view->getFormat());
 }
Esempio n. 3
0
 /**
  * @param ViewHandler   $handler
  * @param View          $view
  * @param Request       $request
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function createResponse(ViewHandler $handler, View $view, Request $request)
 {
     $format = $view->getFormat() ?: $request->getRequestFormat();
     $data = $view->getData();
     if ($data instanceof Cursor) {
         $view->setData(iterator_to_array($data, false));
         $view->getResponse()->headers->set('X-Total-Count', $data->count());
         return $handler->createResponse($view, $request, $view->getFormat());
     }
     if ($data instanceof Form && Codes::HTTP_BAD_REQUEST === $view->getStatusCode()) {
         $view->setData($this->formatFormErrors($data));
         return $handler->createResponse($view, $request, $format);
     }
     return $handler->createResponse($view, $request, $format);
 }
 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     $view = $event->getControllerResult();
     if (!$view instanceof View) {
         if (!$request->attributes->get('_view') && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
             return;
         }
         $view = new View($view);
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     if (!empty($vars)) {
         $parameters = $view->getData();
         if (null !== $parameters && !is_array($parameters)) {
             throw new \RuntimeException('View data must be an array if using a templating aware format.');
         }
         $parameters = (array) $parameters;
         foreach ($vars as $var) {
             if (!array_key_exists($var, $parameters)) {
                 $parameters[$var] = $request->attributes->get($var);
             }
         }
         $view->setData($parameters);
     }
     $viewHandler = $this->container->get('fos_rest.view_handler');
     if ($viewHandler->isFormatTemplating($view->getFormat())) {
         $template = $request->attributes->get('_template');
         if ($template) {
             if ($template instanceof TemplateReference) {
                 $template->set('format', null);
                 $template->set('engine', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $viewHandler->handle($view, $request);
     $event->setResponse($response);
 }
 public function createResponse(ViewHandler $handler, View $view, Request $request, $format)
 {
     $data = $view->getData();
     $context = $view->getSerializationContext();
     $container = $handler->getContainer();
     $format = $view->getFormat();
     $file = $container->get('claroline.library.view.serializer.serializer')->serialize($data, $format);
     $response = new StreamedResponse();
     $response->setCallBack(function () use($file) {
         readfile($file);
     });
     $response->headers->set('Content-Transfer-Encoding', 'octet-stream');
     $response->headers->set('Content-Type', 'application/force-download');
     $response->headers->set('Content-Disposition', 'attachment; filename=file.' . $format);
     switch ($format) {
         case 'csv':
             $response->headers->set('Content-Type', 'text/csv');
             break;
         case 'xls':
             $response->headers->set('Content-Type', 'application/vnd.ms-excel');
             break;
     }
     return $response;
 }
Esempio n. 6
0
 /**
  * Handles a request with the proper handler.
  *
  * Decides on which handler to use based on the request format.
  *
  * @param View    $view
  * @param Request $request
  *
  * @throws UnsupportedMediaTypeHttpException
  *
  * @return Response
  */
 public function handle(View $view, Request $request = null)
 {
     if (null === $request) {
         $request = $this->requestStack->getCurrentRequest();
     }
     $format = $view->getFormat() ?: $request->getRequestFormat();
     if (!$this->supports($format)) {
         $msg = "Format '{$format}' not supported, handler must be implemented";
         throw new UnsupportedMediaTypeHttpException($msg);
     }
     if (isset($this->customHandlers[$format])) {
         return call_user_func($this->customHandlers[$format], $this, $view, $request, $format);
     }
     return $this->createResponse($view, $request, $format);
 }
 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     /** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     $customViewDefined = true;
     if (!$view instanceof View) {
         if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
             return parent::onKernelView($event);
         }
         $view = new View($view);
         $customViewDefined = false;
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Codes::HTTP_OK === $view->getStatusCode())) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         if ($configuration->getSerializerGroups() && !$customViewDefined) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->setGroups($configuration->getSerializerGroups());
             $view->setSerializationContext($context);
         }
         if ($configuration->getSerializerEnableMaxDepthChecks()) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->enableMaxDepthChecks();
             $view->setSerializationContext($context);
         }
         $populateDefaultVars = $configuration->isPopulateDefaultVars();
     } else {
         $populateDefaultVars = true;
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars && $populateDefaultVars) {
         $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);
 }
 /**
  * Handles a request with the proper handler
  *
  * Decides on which handler to use based on the request format
  *
  * @param View    $view
  * @param Request $request Request object
  *
  * @return Response
  */
 public function handle(View $view, Request $request = null)
 {
     if (null === $request) {
         $request = $this->container->get('request');
     }
     $format = $view->getFormat() ?: $request->getRequestFormat();
     if (!$this->supports($format)) {
         $msg = "Format '{$format}' not supported, handler must be implemented";
         throw new HttpException(Codes::HTTP_UNSUPPORTED_MEDIA_TYPE, $msg);
     }
     if (isset($this->customHandlers[$format])) {
         return call_user_func($this->customHandlers[$format], $this, $view, $request, $format);
     }
     return $this->createResponse($view, $request, $format);
 }
 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
         return false;
     }
     /** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     $customViewDefined = true;
     if (!$view instanceof View) {
         if (!$configuration && !$this->forceView) {
             return;
         }
         $view = new View($view);
         $customViewDefined = false;
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         $context = $view->getContext();
         if ($configuration->getSerializerGroups() && !$customViewDefined) {
             $context->addGroups($configuration->getSerializerGroups());
         }
         if ($configuration->getSerializerEnableMaxDepthChecks()) {
             $context->setMaxDepth(0);
         }
         $populateDefaultVars = $configuration->isPopulateDefaultVars();
     } else {
         $populateDefaultVars = true;
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars && $populateDefaultVars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     if ($this->viewHandler->isFormatTemplating($view->getFormat()) && !$view->getRoute() && !$view->getLocation()) {
         if (!empty($vars)) {
             $parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
             foreach ($vars as $var) {
                 if (!array_key_exists($var, $parameters)) {
                     $parameters[$var] = $request->attributes->get($var);
                 }
             }
             $view->setData($parameters);
         }
         $template = null !== $configuration && $configuration->getTemplate() ? $configuration->getTemplate() : $request->attributes->get('_template');
         if ($template && !$view->getTemplate()) {
             if ($template instanceof TemplateReferenceInterface) {
                 $template->set('format', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $this->viewHandler->handle($view, $request);
     $event->setResponse($response);
 }