本文整理汇总了PHP中Symfony\Component\HttpFoundation\AcceptHeader类的典型用法代码示例。如果您正苦于以下问题:PHP AcceptHeader类的具体用法?PHP AcceptHeader怎么用?PHP AcceptHeader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AcceptHeader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: searchAction
/**
* @param Request $request
*
* @throws ServiceCircularReferenceException When a circular reference is detected
* @throws ServiceNotFoundException When the service is not defined
*
* @return JsonResponse|Response
*/
public function searchAction(Request $request)
{
/* @var \Webburza\Sylius\LocationBundle\Doctrine\ORM\LocationRepository $locationRepository */
$locationRepository = $this->container->get('webburza_location.repository.location');
/* @var \Sylius\Component\Locale\Model\Locale */
$locale = $this->get('sylius.context.locale')->getCurrentLocale();
/* @var \Symfony\Component\Serializer\Serializer */
$serializer = $this->container->get('serializer');
if ($query = $request->get('query')) {
$locations = $locationRepository->findPublicByQuery($query, $locale);
} else {
$locations = $locationRepository->findPublicByQuery('', $locale);
}
$locationsJson = $serializer->serialize($locations, 'json');
$returnJson = AcceptHeader::fromString($request->headers->get('Accept'))->has('application/json');
if ($returnJson) {
$response = new JsonResponse($locationsJson);
$response->headers->set('Access-Control-Allow-Origin', '*');
return $response;
} else {
$googleMapsKey = $this->getParameter('webburza.sylius.location_bundle.google_maps_key');
$googleMapsEnabled = $this->getParameter('webburza.sylius.location_bundle.google_maps_enabled');
return $this->render('WebburzaSyliusLocationBundle:Frontend:_search.html.twig', ['locations' => $locations, 'locationsJson' => $locationsJson, 'googleMapsEnabled' => $googleMapsEnabled, 'googleMapsKey' => $googleMapsKey]);
}
}
示例2: viewAction
public function viewAction(Request $request)
{
$accept = AcceptHeader::fromString($request->headers->get('accept'));
$finder = new Finder();
$userFiles = $finder->in(STORAGE_PATH)->name('*.json')->sortByModifiedTime()->files();
$users = function () use($userFiles) {
foreach ($userFiles as $userFile) {
/** @var SplFileInfo $userFile */
$data = json_decode($userFile->getContents(), true);
$data['dob'] = new \DateTime($data['dob']['date']);
$data['id'] = $userFile->getBasename('.json');
(yield $data);
}
};
$response = new Response();
if ($accept->has('text/html')) {
$response->setContent($this->getTwig()->render('crud/view.html.twig', ['users' => $users(), 'urlGenerator' => $this->urlGenerator(), 'translator' => TranslationFactory::createTranslator($request), 'user' => $this->getUser()]));
} elseif ($accept->has('application/json')) {
$response = new Response('', Response::HTTP_OK, ['Content-Type' => 'application/json']);
$userList = iterator_to_array($users());
$response->setContent(json_encode($userList));
} elseif ($accept->has('text/xml')) {
$response = new Response('', Response::HTTP_OK, ['Content-Type' => 'text/xml']);
$userList = iterator_to_array($users());
$xml = new \SimpleXMLElement('<root />');
array_walk($userList, function ($userData) use($xml) {
$user = $xml->addChild('user');
$user->addChild('username', $userData['username']);
});
$response->setContent($xml->asXML());
}
return $response;
}
示例3: contentTypeHeaderValid
/**
* @param Request $request
* @return bool
*/
protected function contentTypeHeaderValid(Request $request)
{
$contentTypeHeader = AcceptHeader::fromString($request->header('content-type'));
if ($contentTypeHeader->has('application/vnd.api+json')) {
$attributes = $contentTypeHeader->get('application/vnd.api+json')->getAttributes();
return empty($attributes) || count($attributes) === 1 && $contentTypeHeader->get('application/vnd.api+json')->getAttribute('charset') === 'UTF-8';
}
return false;
}
示例4: parseFormatFromHeader
private function parseFormatFromHeader(Request $request, $headerName)
{
if (!$request->headers->has($headerName)) {
//if there is no header with the specified name, fall-back on the default format
return self::DEFAULT_FORMAT;
}
$acceptHeader = AcceptHeader::fromString($request->headers->get($headerName))->first();
$format = $acceptHeader->getAttribute('format', null);
return is_null($format) ? self::DEFAULT_FORMAT : $format;
}
示例5: onRequest
/**
* @param GetResponseEvent $event
* @return null
*/
public function onRequest(GetResponseEvent $event)
{
if (!$event->isMasterRequest()) {
return null;
}
$accept = AcceptHeader::fromString($event->getRequest()->headers->get('Accept'));
if ($accept->has('*/*') || $accept->has('application/json')) {
return null;
}
$event->setResponse($this->responseToSend);
}
示例6: getVersionByAcceptHeaders
/**
* @param Request $request
* @param $version
* @return mixed
*/
public function getVersionByAcceptHeaders(Request $request, $version)
{
$acceptHeader = AcceptHeader::fromString($request->headers->get('Accept'))->all();
foreach ($acceptHeader as $acceptHeaderItem) {
if ($acceptHeaderItem->hasAttribute('version')) {
$version = $acceptHeaderItem->getAttribute('version');
break;
}
}
return $version;
}
示例7: getVersion
/**
* Returns the API version.
*
* @return int $version
* The version number of the API (default: 1).
*
*/
public function getVersion()
{
$version = 1;
$accept = AcceptHeader::fromString($this->headers->get('accept'));
// We'll assume the first accept header to have a version is accurate.
foreach ($accept->all() as $item) {
if ($item->getAttribute('version')) {
$version = $item->getAttribute('version');
break;
}
}
return $version;
}
示例8: getContext
/**
* @param Request $request
*
* @return mixed|string
*/
public function getContext(Request $request = null)
{
$context = 'ld-json';
if (null !== $request) {
$acceptHeader = AcceptHeader::fromString($request->headers->get('Accept'))->all();
foreach ($acceptHeader as $acceptHeaderItem) {
if ($acceptHeaderItem->hasAttribute('version')) {
$context = str_ireplace('application/', '', $acceptHeaderItem->getValue());
break;
}
}
}
return $context;
}
示例9: onKernelRequest
/**
* parse Accept-Language header from request.
*
* @param GetResponseEvent $event listener event
*
* @return void
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$headers = AcceptHeader::fromString($request->headers->get('Accept-Language'));
$languages = array_intersect(array_map(function ($header) {
return $header->getValue();
}, $headers->all()), array_map(function ($language) {
return $language->getId();
}, $this->repository->findAll()));
if (empty($languages)) {
$languages[$this->defaultLocale] = $this->defaultLocale;
}
$request->attributes->set('languages', $languages);
}
示例10: getVersion
/**
* @param Request $request
*
* @return mixed|string
*/
public function getVersion(Request $request = null)
{
$version = 'v2';
if ($request instanceof Request) {
$acceptHeader = AcceptHeader::fromString($request->headers->get('Accept'))->all();
foreach ($acceptHeader as $acceptHeaderItem) {
if ($acceptHeaderItem->hasAttribute('version')) {
$version = $acceptHeaderItem->getAttribute('version');
break;
}
}
}
return $version;
}
示例11: checkContentTypeHeader
/**
* Check the content-type header.
*
* @param string $content
* @return boolean
*/
public function checkContentTypeHeader($content)
{
$header = AcceptHeader::fromString($content);
if (!$header->has(self::CONTENT_TYPE)) {
throw new InvalidJsonApiContentTypeHeader();
}
$attributes = $header->get(self::CONTENT_TYPE)->getAttributes();
if (count($attributes) > 1) {
throw new InvalidJsonApiContentTypeHeaderParameterCount();
}
if (count($attributes) === 1 && $header->get(self::CONTENT_TYPE)->getAttribute('charset') !== 'UTF-8') {
throw new InvalidJsonApiContentTypeHeaderCharset();
}
return true;
}
示例12: getVersion
private function getVersion(Request $request, $headerName)
{
if (!$request->headers->has($headerName)) {
return null;
}
$acceptHeader = AcceptHeader::fromString($request->headers->get($headerName))->first();
$version = $acceptHeader->getAttribute('version', null);
if (!is_null($version)) {
if (!ctype_digit((string) $version)) {
throw new UnsupportedVersionException(sprintf('Version %s is invalid', $version), UnsupportedVersionException::CODE);
}
return (int) $version;
}
return null;
}
示例13: getBestFormat
/**
* Detect the request format based on the priorities and the Accept header
*
* Note: Request "_format" parameter is considered the preferred Accept header
*
* @param Request $request The request
* @param array $priorities Ordered array of formats (highest priority first)
* @param Boolean $preferExtension If to consider the extension last or first
*
* @return void|string The format string
*/
public function getBestFormat(Request $request, array $priorities, $preferExtension = false)
{
// BC - Maintain this while 2.0 and 2.1 dont reach their end of life
// Note: Request::splitHttpAcceptHeader is deprecated since version 2.2, to be removed in 2.3.
if (class_exists('Symfony\\Component\\HttpFoundation\\AcceptHeader')) {
$mimetypes = array();
foreach (AcceptHeader::fromString($request->headers->get('Accept'))->all() as $item) {
$mimetypes[$item->getValue()] = $item->getQuality();
}
} else {
$mimetypes = $request->splitHttpAcceptHeader($request->headers->get('Accept'));
}
$extension = $request->get('_format');
if (null !== $extension && $request->getMimeType($extension)) {
$mimetypes[$request->getMimeType($extension)] = $preferExtension ? reset($mimetypes) + 1 : end($mimetypes) - 1;
arsort($mimetypes);
}
if (empty($mimetypes)) {
return null;
}
$catchAllEnabled = in_array('*/*', $priorities);
return $this->getFormatByPriorities($request, $mimetypes, $priorities, $catchAllEnabled);
}
示例14: getAcceptableContentTypes
/**
* Gets a list of content types acceptable by the client browser.
*
* @return array List of content types in preferable order
*
* @api
*/
public function getAcceptableContentTypes()
{
if (null !== $this->acceptableContentTypes) {
return $this->acceptableContentTypes;
}
return $this->acceptableContentTypes = array_keys(AcceptHeader::fromString($this->headers->get('Accept'))->all());
}
示例15: getAcceptedLocales
/**
* @return array
*/
public function getAcceptedLocales()
{
if (!$this->acceptLanguageHeader) {
return [];
}
return array_keys(AcceptHeader::fromString($this->acceptLanguageHeader)->all());
}