本文整理汇总了PHP中Symfony\Component\HttpFoundation\Request::getMimeTypes方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::getMimeTypes方法的具体用法?PHP Request::getMimeTypes怎么用?PHP Request::getMimeTypes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpFoundation\Request
的用法示例。
在下文中一共展示了Request::getMimeTypes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: extendRequestFormats
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));
}
}
示例2: onKernelRequest
/**
* 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);
}
}
}
示例3: onKernelRequest
/**
* 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']);
}
示例4: testGetMimeTypesFromInexistentFormat
public function testGetMimeTypesFromInexistentFormat()
{
$request = new Request();
$this->assertNull($request->getMimeType('foo'));
$this->assertEquals(array(), Request::getMimeTypes('foo'));
}
示例5: normalizePriorities
/**
* 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;
}