Gets the mime types associated with the format.
public static getMimeTypes ( string $format ) : array | ||
$format | string | The format |
리턴 | array | The associated mime types |
private static function extendRequestFormats(Request $request, array $formats) { foreach ($formats as $format => $mimeTypes) { if (method_exists(get_class($request), 'getMimeTypes')) { $mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($format)); } elseif (null !== $request->getMimeType($format)) { $class = new \ReflectionClass(get_class($request)); $properties = $class->getStaticProperties(); if (isset($properties['formats'][$format])) { $mimeTypes = array_merge($mimeTypes, $properties['formats'][$format]); } } $request->setFormat($format, array_unique($mimeTypes)); } }
/** * Core request handler. * * @param GetResponseEvent $event The event */ public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) { return; } if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) { foreach ($this->mimeTypes as $format => $mimeTypes) { if (method_exists(Request::class, 'getMimeTypes')) { $mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($format)); } elseif (null !== $request->getMimeType($format)) { $class = new \ReflectionClass(Request::class); $properties = $class->getStaticProperties(); $mimeTypes = array_merge($mimeTypes, $properties['formats'][$format]); } $request->setFormat($format, $mimeTypes); } } }
/** * Sets the applicable format to the HttpFoundation Request. * * @param GetResponseEvent $event * * @throws NotFoundHttpException * @throws NotAcceptableHttpException */ public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); if (!$request->attributes->has('_api_resource_class') && !$request->attributes->has('_api_respond')) { return; } $this->populateMimeTypes(); $this->addRequestFormats($request, $this->formats); // Empty strings must be converted to null because the Symfony router doesn't support parameter typing before 3.2 (_format) if (null === ($routeFormat = $request->attributes->get('_format') ?: null)) { $mimeTypes = array_keys($this->mimeTypes); } elseif (!isset($this->formats[$routeFormat])) { throw new NotFoundHttpException('Not Found'); } else { $mimeTypes = Request::getMimeTypes($routeFormat); } // First, try to guess the format from the Accept header $accept = $request->headers->get('Accept'); if (null !== $accept) { if (null === ($acceptHeader = $this->negotiator->getBest($accept, $mimeTypes))) { throw $this->getNotAcceptableHttpException($accept, $mimeTypes); } $request->setRequestFormat($request->getFormat($acceptHeader->getType())); return; } // Then use the Symfony request format if available and applicable $requestFormat = $request->getRequestFormat(null) ?: null; if (null !== $requestFormat) { $mimeType = $request->getMimeType($requestFormat); if (isset($this->mimeTypes[$mimeType])) { return; } throw $this->getNotAcceptableHttpException($mimeType); } // Finally, if no Accept header nor Symfony request format is set, return the default format reset($this->formats); $request->setRequestFormat(each($this->formats)['key']); }
public function testGetMimeTypesFromInexistentFormat() { $request = new Request(); $this->assertNull($request->getMimeType('foo')); $this->assertEquals(array(), Request::getMimeTypes('foo')); }
/** * Transform the format (json, html, ...) to their mimeType form (application/json, text/html, ...). * * @param Request $request * @param string[] $priorities * * @return string[] formatted priorities */ private function normalizePriorities(Request $request, array $priorities) { $priorities = $this->sanitize($priorities); $mimeTypes = array(); foreach ($priorities as $priority) { if (strpos($priority, '/')) { $mimeTypes[] = $priority; continue; } if (method_exists(Request::class, 'getMimeTypes')) { $mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($priority)); } elseif (null !== $request->getMimeType($priority)) { $class = new \ReflectionClass(Request::class); $properties = $class->getStaticProperties(); $mimeTypes = array_merge($mimeTypes, $properties['formats'][$priority]); } if (isset($this->mimeTypes[$priority])) { foreach ($this->mimeTypes[$priority] as $mimeType) { $mimeTypes[] = $mimeType; } } } return $mimeTypes; }