/** * Handles a request to convert it to a response * * @param Request $request The request * @param int $type The type of request * @param bool $catch Whether to catch exceptions or not * * @return Response * * @throws Exception When an exception occurs during processing */ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { $accept = $request->headers->get('Accept'); if ($accept !== null) { $priorities = $this->formatNegotiator->normalizePriorities($this->formatPriorities); $accept = $this->formatNegotiator->getBest($accept, $priorities); $request->attributes->set('_accept', $accept); if ($accept !== null & !$accept->isMediaRange()) { $mimeType = $accept->getValue(); $request->attributes->set('_mime_type', $mimeType); $request->attributes->set('_format', $this->formatNegotiator->getFormat($mimeType)); } } $acceptLang = $request->headers->get('Accept-Language'); if ($acceptLang !== null) { $acceptLang = $this->languageNegotiator->getBest($acceptLang, $this->languagePriorities); $request->attributes->set('_accept_language', $acceptLang); if ($acceptLang !== null) { $request->attributes->set('_language', $acceptLang->getValue()); } } try { $this->decodeBody($request); } catch (BadRequestHttpException $exception) { if ($catch === true) { return new Response($exception->getMessage(), Response::HTTP_BAD_REQUEST); } throw $exception; } return $this->kernel->handle($request, $type, $catch); }
/** * Retrieve the formatter to use for the current request. * * Uses content negotiation to find the best available output format for * the requested content type. * * @param ServerRequestInterface $request * @return AbstractFormatter */ protected function formatter(ServerRequestInterface $request) { $accept = $request->getHeaderLine('Accept'); $priorities = $this->priorities(); $preferred = $this->negotiator->getBest($accept, array_keys($priorities)); if ($preferred) { $formatter = $priorities[$preferred->getValue()]; } else { $formatter = array_shift($priorities); } return call_user_func($this->resolver, $formatter); }