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

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