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

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\Metadata\Error as ErrorOperation;
  14. use ApiPlatform\Metadata\HttpOperation;
  15. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  16. use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
  17. use Negotiation\Exception\InvalidArgument;
  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.     public function __construct(private readonly Negotiator $negotiator, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory null, private readonly array $formats = [], private readonly array $errorFormats = [], private readonly array $docsFormats = [], private readonly ?bool $eventsBackwardCompatibility null// @phpstan-ignore-line
  32.     {
  33.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  34.     }
  35.     /**
  36.      * Sets the applicable format to the HttpFoundation Request.
  37.      *
  38.      * @throws NotFoundHttpException
  39.      * @throws NotAcceptableHttpException
  40.      */
  41.     public function onKernelRequest(RequestEvent $event): void
  42.     {
  43.         $request $event->getRequest();
  44.         $operation $this->initializeOperation($request);
  45.         if ('api_platform.action.entrypoint' === $request->attributes->get('_controller')) {
  46.             return;
  47.         }
  48.         if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
  49.             return;
  50.         }
  51.         if ($operation instanceof ErrorOperation) {
  52.             return;
  53.         }
  54.         if (!($request->attributes->has('_api_resource_class')
  55.             || $request->attributes->getBoolean('_api_respond'false)
  56.             || $request->attributes->getBoolean('_graphql'false)
  57.         )) {
  58.             return;
  59.         }
  60.         $formats $operation?->getOutputFormats() ?? ('api_doc' === $request->attributes->get('_route') ? $this->docsFormats $this->formats);
  61.         $this->addRequestFormats($request$formats);
  62.         // Empty strings must be converted to null because the Symfony router doesn't support parameter typing before 3.2 (_format)
  63.         if (null === $routeFormat $request->attributes->get('_format') ?: null) {
  64.             $flattenedMimeTypes $this->flattenMimeTypes($formats);
  65.             $mimeTypes array_keys($flattenedMimeTypes);
  66.         } elseif (!isset($formats[$routeFormat])) {
  67.             if (!$request->attributes->get('data') instanceof \Exception) {
  68.                 throw new NotFoundHttpException(sprintf('Format "%s" is not supported'$routeFormat));
  69.             }
  70.             $this->setRequestErrorFormat($operation$request);
  71.             return;
  72.         } else {
  73.             $mimeTypes Request::getMimeTypes($routeFormat);
  74.             $flattenedMimeTypes $this->flattenMimeTypes([$routeFormat => $mimeTypes]);
  75.         }
  76.         // First, try to guess the format from the Accept header
  77.         /** @var string|null $accept */
  78.         $accept $request->headers->get('Accept');
  79.         if (null !== $accept) {
  80.             $mediaType null;
  81.             try {
  82.                 $mediaType $this->negotiator->getBest($accept$mimeTypes);
  83.             } catch (InvalidArgument) {
  84.                 throw $this->getNotAcceptableHttpException($accept$flattenedMimeTypes);
  85.             }
  86.             if (null === $mediaType) {
  87.                 if (!$request->attributes->get('data') instanceof \Exception) {
  88.                     throw $this->getNotAcceptableHttpException($accept$flattenedMimeTypes);
  89.                 }
  90.                 $this->setRequestErrorFormat($operation$request);
  91.                 return;
  92.             }
  93.             $formatMatcher = new FormatMatcher($formats);
  94.             $request->setRequestFormat($formatMatcher->getFormat($mediaType->getType()));
  95.             return;
  96.         }
  97.         // Then use the Symfony request format if available and applicable
  98.         $requestFormat $request->getRequestFormat('') ?: null;
  99.         if (null !== $requestFormat) {
  100.             $mimeType $request->getMimeType($requestFormat);
  101.             if (isset($flattenedMimeTypes[$mimeType])) {
  102.                 return;
  103.             }
  104.             if ($request->attributes->get('data') instanceof \Exception) {
  105.                 $this->setRequestErrorFormat($operation$request);
  106.                 return;
  107.             }
  108.             throw $this->getNotAcceptableHttpException($mimeType$flattenedMimeTypes);
  109.         }
  110.         // Finally, if no Accept header nor Symfony request format is set, return the default format
  111.         foreach ($formats as $format => $mimeType) {
  112.             $request->setRequestFormat($format);
  113.             return;
  114.         }
  115.     }
  116.     /**
  117.      * Adds the supported formats to the request.
  118.      *
  119.      * This is necessary for {@see Request::getMimeType} and {@see Request::getMimeTypes} to work.
  120.      */
  121.     private function addRequestFormats(Request $request, array $formats): void
  122.     {
  123.         foreach ($formats as $format => $mimeTypes) {
  124.             $request->setFormat($format, (array) $mimeTypes);
  125.         }
  126.     }
  127.     /**
  128.      * Retries the flattened list of MIME types.
  129.      */
  130.     private function flattenMimeTypes(array $formats): array
  131.     {
  132.         $flattenedMimeTypes = [];
  133.         foreach ($formats as $format => $mimeTypes) {
  134.             foreach ($mimeTypes as $mimeType) {
  135.                 $flattenedMimeTypes[$mimeType] = $format;
  136.             }
  137.         }
  138.         return $flattenedMimeTypes;
  139.     }
  140.     /**
  141.      * Retrieves an instance of NotAcceptableHttpException.
  142.      */
  143.     private function getNotAcceptableHttpException(string $accept, array $mimeTypes): NotAcceptableHttpException
  144.     {
  145.         return new NotAcceptableHttpException(sprintf(
  146.             'Requested format "%s" is not supported. Supported MIME types are "%s".',
  147.             $accept,
  148.             implode('", "'array_keys($mimeTypes))
  149.         ));
  150.     }
  151.     public function setRequestErrorFormat(?HttpOperation $operationRequest $request): void
  152.     {
  153.         $errorResourceFormats array_merge($operation?->getOutputFormats() ?? [], $operation?->getFormats() ?? [], $this->errorFormats);
  154.         $flattened $this->flattenMimeTypes($errorResourceFormats);
  155.         if ($flattened[$accept $request->headers->get('Accept')] ?? false) {
  156.             $request->setRequestFormat($flattened[$accept]);
  157.             return;
  158.         }
  159.         if (isset($errorResourceFormats['jsonproblem'])) {
  160.             $request->setRequestFormat('jsonproblem');
  161.             $request->setFormat('jsonproblem'$errorResourceFormats['jsonproblem']);
  162.             return;
  163.         }
  164.         $request->setRequestFormat(array_key_first($errorResourceFormats));
  165.     }
  166. }