Exemple #1
0
 protected function doKernelResponse(Request $request, Response $response)
 {
     if (!$response instanceof DataResponse) {
         return;
     }
     $routeName = $request->attributes->get('_route');
     $route = $this->routes->get($routeName);
     if (!$route) {
         return;
     }
     $acceptedFormat = $route->getOption(RouteOptions::ACCEPTED_FORMAT);
     if (!$acceptedFormat) {
         $response->setContent('');
         $response->setStatusCode(406);
     }
     if ($this->encoder->supportsEncoding($acceptedFormat) && $acceptedFormat === 'json') {
         $contentType = $request->getMimeType($acceptedFormat);
         $jsonResponse = new JsonResponse($response->getContent());
         $response->setContent($jsonResponse->getContent());
         $response->headers->set('Content-Type', $contentType);
     } elseif ($this->encoder->supportsEncoding($acceptedFormat)) {
         $contentType = $request->getMimeType($acceptedFormat);
         $content = $this->encoder->encode($response->getContent(), $acceptedFormat);
         $response->setContent($content);
         $response->headers->set('Content-Type', $contentType);
     }
 }
 /**
  * Attempts to retrieve the format from the header identified by $headerName which is found in the $request object.
  * A default value will be returned if the format can not be retrieved.
  * As opposed to the getFormat() method, this method will also check if $serializer supports the retrieved
  * format. If it doesn't, an exception is thrown.
  * @param Request $request
  * @param string $headerName
  * @param EncoderInterface $serializer
  * @return string
  */
 public function getSupportedFormat(Request $request, $headerName, EncoderInterface $serializer)
 {
     $format = $this->parseFormatFromHeader($request, $headerName);
     if (!$serializer->supportsEncoding($format)) {
         throw new UnsupportedFormatException(sprintf('Format %s is unsupported', $format), UnsupportedFormatException::CODE);
     }
     return $format;
 }