예제 #1
0
 /**
  * FOSRest's ViewHandler has a private getStatusCode method. Because we cannot use it, we added
  * this lightweight method (without forms support) for Hal custom handler.
  *
  * @param View $view view instance
  * 
  * @return int HTTP status code
  */
 private function getStatusCode(View $view)
 {
     if (null !== ($code = $view->getStatusCode())) {
         return $code;
     }
     return Codes::HTTP_OK;
 }
예제 #2
0
 public function createRedirectResponse(View $view, $location, $format)
 {
     if ($format == 'html' && $view->getData() != null && ($view->getStatusCode() == Codes::HTTP_CREATED || $view->getStatusCode() == Codes::HTTP_ACCEPTED)) {
         $prevStatus = $view->getStatusCode();
         $view->setStatusCode(Codes::HTTP_OK);
     }
     $response = parent::createRedirectResponse($view, $location, $format);
     if ($response->headers->has('Location') && $response->getStatusCode() !== Codes::HTTP_CREATED && ($response->getStatusCode() < 300 || $response->getStatusCode() >= 400)) {
         $response->headers->remove('Location');
     }
     if (isset($prevStatus)) {
         $view->setStatusCode($prevStatus);
         $code = isset($this->forceRedirects[$format]) ? $this->forceRedirects[$format] : $this->getStatusCode($view, $response->getContent());
         $response->setStatusCode($code);
     }
     return $response;
 }
 /**
  * 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 ($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);
 }
예제 #4
0
 /**
  * Creates the Response from the view.
  *
  * @param View   $view
  * @param string $location
  * @param string $format
  *
  * @return Response
  */
 public function createRedirectResponse(View $view, $location, $format)
 {
     $content = null;
     if (($view->getStatusCode() == Response::HTTP_CREATED || $view->getStatusCode() == Response::HTTP_ACCEPTED) && $view->getData() != null) {
         $response = $this->initResponse($view, $format);
     } else {
         $response = $view->getResponse();
         if ('html' === $format && isset($this->forceRedirects[$format])) {
             $redirect = new RedirectResponse($location);
             $content = $redirect->getContent();
             $response->setContent($content);
         }
     }
     $code = isset($this->forceRedirects[$format]) ? $this->forceRedirects[$format] : $this->getStatusCode($view, $content);
     $response->setStatusCode($code);
     $response->headers->set('Location', $location);
     return $response;
 }
 /**
  * @return bool
  */
 public function isEntityEmpty()
 {
     return null === $this->view->getData() || $this->view->getStatusCode() != self::STATUS_OK && $this->view->getStatusCode() != self::STATUS_OK;
 }
 /**
  * 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);
 }
예제 #7
0
 public function testSetStatusCode()
 {
     $view = new View();
     $code = 404;
     $view->setStatusCode($code);
     $this->assertEquals($code, $view->getStatusCode());
 }
예제 #8
0
 /**
  * Gets a response HTTP status code from a View instance
  *
  * By default it will return 200. However if there is a FormInterface stored for
  * the key 'form' in the View's data it will return the failed_validation
  * configuration if the form instance has errors.
  *
  * @param View $view view instance
  * @param mixed $content
  *
  * @return int HTTP status code
  */
 protected function getStatusCode(View $view, $content = null)
 {
     if (200 !== ($code = $view->getStatusCode())) {
         return $code;
     }
     $data = $view->getData();
     if ($data instanceof FormInterface) {
         $form = $data;
     } elseif (is_array($data) && isset($data['form']) && $data['form'] instanceof FormInterface) {
         $form = $data['form'];
     } else {
         $form = false;
     }
     if ($form && $form->isBound() && !$form->isValid()) {
         return $this->failedValidationCode;
     }
     return null !== $content ? Codes::HTTP_OK : $this->emptyContentCode;
 }
예제 #9
0
 /**
  * Gets a response HTTP status code from a View instance
  *
  * By default it will return 200. However if there is a FormInterface stored for
  * the key 'form' in the View's data it will return the failed_validation
  * configuration if the form instance has errors.
  *
  * @param View $view view instance
  *
  * @return int HTTP status code
  */
 private function getStatusCode(View $view)
 {
     if (null !== ($code = $view->getStatusCode())) {
         return $code;
     }
     $data = $view->getData();
     if ($data instanceof FormInterface) {
         $form = $data;
     } elseif (is_array($data) && isset($data['form']) && $data['form'] instanceof FormInterface) {
         $form = $data['form'];
     } else {
         $form = false;
     }
     return $form && $form->isBound() && !$form->isValid() ? $this->failedValidationCode : Codes::HTTP_OK;
 }
예제 #10
0
 /**
  * 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);
 }
예제 #11
0
 /**
  * Gets a response HTTP status code from a View instance
  *
  * By default it will return 200. However if there is a FormInterface stored for
  * the key 'form' in the View's data it will return the failed_validation
  * configuration if the form instance has errors.
  *
  * @param View $view view instance
  * @return int HTTP status code
  */
 private function getStatusCode(View $view)
 {
     if (null !== ($code = $view->getStatusCode())) {
         return $code;
     }
     $data = $view->getData();
     if (!is_array($data) || empty($data['form']) || !$data['form'] instanceof FormInterface) {
         return Codes::HTTP_OK;
     }
     return $data['form']->isBound() && !$data['form']->isValid() ? $this->failedValidationCode : Codes::HTTP_OK;
 }
 private function statusCodeMatches(View $subject, $argument)
 {
     if (isset($argument['statusCode'])) {
         return $subject->getStatusCode() === $argument['statusCode'];
     }
     return true;
 }
예제 #13
0
 public function testGetStatusCodeFromResponse()
 {
     $view = new View();
     $this->assertNull($view->getStatusCode());
     $this->assertEquals(Response::HTTP_OK, $view->getResponse()->getStatusCode());
     // default code of the response.
 }