vendor/api-platform/core/src/Symfony/EventListener/AddFormatListener.php line 75

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Symfony\EventListener;
  12. use ApiPlatform\Api\FormatMatcher;
  13. use ApiPlatform\Core\Api\FormatsProviderInterface;
  14. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  15. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  16. use ApiPlatform\Util\OperationRequestInitiatorTrait;
  17. use ApiPlatform\Util\RequestAttributesExtractor;
  18. use Negotiation\Negotiator;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpKernel\Event\RequestEvent;
  21. use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
  22. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  23. /**
  24.  * Chooses the format to use according to the Accept header and supported formats.
  25.  *
  26.  * @author Kévin Dunglas <dunglas@gmail.com>
  27.  */
  28. final class AddFormatListener
  29. {
  30.     use OperationRequestInitiatorTrait;
  31.     private $negotiator;
  32.     private $resourceMetadataFactory;
  33.     private $formats = [];
  34.     private $formatsProvider;
  35.     private $formatMatcher;
  36.     /**
  37.      * @param ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface|FormatsProviderInterface|array $resourceMetadataFactory
  38.      */
  39.     public function __construct(Negotiator $negotiator$resourceMetadataFactory, array $formats = [])
  40.     {
  41.         if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface && !$resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  42.             trigger_deprecation('api-plaform/core''2.5'sprintf('Passing an array or an instance of "%s" as 2nd parameter of the constructor of "%s" is deprecated since API Platform 2.5, pass an instance of "%s" instead'FormatsProviderInterface::class, __CLASS__ResourceMetadataFactoryInterface::class));
  43.         }
  44.         if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface && $resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  45.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  46.         }
  47.         $this->negotiator $negotiator;
  48.         $this->resourceMetadataFactory $resourceMetadataFactory instanceof ResourceMetadataFactoryInterface $resourceMetadataFactory null;
  49.         $this->formats $formats;
  50.         $this->resourceMetadataCollectionFactory $resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory null;
  51.         if (\is_array($resourceMetadataFactory)) {
  52.             $this->formats $resourceMetadataFactory;
  53.         } elseif ($resourceMetadataFactory instanceof FormatsProviderInterface) {
  54.             $this->formatsProvider $resourceMetadataFactory;
  55.         }
  56.     }
  57.     /**
  58.      * Sets the applicable format to the HttpFoundation Request.
  59.      *
  60.      * @throws NotFoundHttpException
  61.      * @throws NotAcceptableHttpException
  62.      */
  63.     public function onKernelRequest(RequestEvent $event): void
  64.     {
  65.         $request $event->getRequest();
  66.         $operation $this->initializeOperation($request);
  67.         if (!(
  68.             $request->attributes->has('_api_resource_class')
  69.             || $request->attributes->getBoolean('_api_respond'false)
  70.             || $request->attributes->getBoolean('_graphql'false)
  71.         )) {
  72.             return;
  73.         }
  74.         $attributes RequestAttributesExtractor::extractAttributes($request);
  75.         $formats $this->formats;
  76.         // BC check to be removed in 3.0
  77.         if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface && $attributes) {
  78.             // TODO: Subresource operation metadata aren't available by default, for now we have to fallback on default formats.
  79.             // TODO: A better approach would be to always populate the subresource operation array.
  80.             $formats $this
  81.                 ->resourceMetadataFactory
  82.                 ->create($attributes['resource_class'])
  83.                 ->getOperationAttribute($attributes'output_formats'$this->formatstrue);
  84.         } elseif ($this->formatsProvider instanceof FormatsProviderInterface) {
  85.             $formats $this->formatsProvider->getFormatsFromAttributes($attributes);
  86.         } elseif ($operation && $operation->getOutputFormats()) {
  87.             $formats $operation->getOutputFormats();
  88.         }
  89.         $this->addRequestFormats($request$formats);
  90.         $this->formatMatcher = new FormatMatcher($formats);
  91.         // Empty strings must be converted to null because the Symfony router doesn't support parameter typing before 3.2 (_format)
  92.         if (null === $routeFormat $request->attributes->get('_format') ?: null) {
  93.             $flattenedMimeTypes $this->flattenMimeTypes($formats);
  94.             $mimeTypes array_keys($flattenedMimeTypes);
  95.         } elseif (!isset($formats[$routeFormat])) {
  96.             throw new NotFoundHttpException(sprintf('Format "%s" is not supported'$routeFormat));
  97.         } else {
  98.             $mimeTypes Request::getMimeTypes($routeFormat);
  99.             $flattenedMimeTypes $this->flattenMimeTypes([$routeFormat => $mimeTypes]);
  100.         }
  101.         // First, try to guess the format from the Accept header
  102.         /** @var string|null $accept */
  103.         $accept $request->headers->get('Accept');
  104.         if (null !== $accept) {
  105.             if (null === $mediaType $this->negotiator->getBest($accept$mimeTypes)) {
  106.                 throw $this->getNotAcceptableHttpException($accept$flattenedMimeTypes);
  107.             }
  108.             $request->setRequestFormat($this->formatMatcher->getFormat($mediaType->getType()));
  109.             return;
  110.         }
  111.         // Then use the Symfony request format if available and applicable
  112.         $requestFormat $request->getRequestFormat('') ?: null;
  113.         if (null !== $requestFormat) {
  114.             $mimeType $request->getMimeType($requestFormat);
  115.             if (isset($flattenedMimeTypes[$mimeType])) {
  116.                 return;
  117.             }
  118.             throw $this->getNotAcceptableHttpException($mimeType$flattenedMimeTypes);
  119.         }
  120.         // Finally, if no Accept header nor Symfony request format is set, return the default format
  121.         foreach ($formats as $format => $mimeType) {
  122.             $request->setRequestFormat($format);
  123.             return;
  124.         }
  125.     }
  126.     /**
  127.      * Adds the supported formats to the request.
  128.      *
  129.      * This is necessary for {@see Request::getMimeType} and {@see Request::getMimeTypes} to work.
  130.      */
  131.     private function addRequestFormats(Request $request, array $formats): void
  132.     {
  133.         foreach ($formats as $format => $mimeTypes) {
  134.             $request->setFormat($format, (array) $mimeTypes);
  135.         }
  136.     }
  137.     /**
  138.      * Retries the flattened list of MIME types.
  139.      */
  140.     private function flattenMimeTypes(array $formats): array
  141.     {
  142.         $flattenedMimeTypes = [];
  143.         foreach ($formats as $format => $mimeTypes) {
  144.             foreach ($mimeTypes as $mimeType) {
  145.                 $flattenedMimeTypes[$mimeType] = $format;
  146.             }
  147.         }
  148.         return $flattenedMimeTypes;
  149.     }
  150.     /**
  151.      * Retrieves an instance of NotAcceptableHttpException.
  152.      */
  153.     private function getNotAcceptableHttpException(string $accept, array $mimeTypes): NotAcceptableHttpException
  154.     {
  155.         return new NotAcceptableHttpException(sprintf(
  156.             'Requested format "%s" is not supported. Supported MIME types are "%s".',
  157.             $accept,
  158.             implode('", "'array_keys($mimeTypes))
  159.         ));
  160.     }
  161. }
  162. class_alias(AddFormatListener::class, \ApiPlatform\Core\EventListener\AddFormatListener::class);