vendor/api-platform/core/src/Symfony/EventListener/ReadListener.php line 59

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\UriVariablesConverterInterface as LegacyUriVariablesConverterInterface;
  13. use ApiPlatform\Exception\InvalidIdentifierException;
  14. use ApiPlatform\Exception\InvalidUriVariableException;
  15. use ApiPlatform\Metadata\Put;
  16. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  17. use ApiPlatform\Metadata\UriVariablesConverterInterface;
  18. use ApiPlatform\Metadata\Util\CloneTrait;
  19. use ApiPlatform\Serializer\SerializerContextBuilderInterface;
  20. use ApiPlatform\State\Exception\ProviderNotFoundException;
  21. use ApiPlatform\State\ProviderInterface;
  22. use ApiPlatform\State\UriVariablesResolverTrait;
  23. use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
  24. use ApiPlatform\State\Util\RequestParser;
  25. use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
  26. use Symfony\Component\HttpKernel\Event\RequestEvent;
  27. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  28. /**
  29.  * Retrieves data from the applicable data provider and sets it as a request parameter called data.
  30.  *
  31.  * @author Kévin Dunglas <dunglas@gmail.com>
  32.  */
  33. final class ReadListener
  34. {
  35.     use CloneTrait;
  36.     use OperationRequestInitiatorTrait;
  37.     use UriVariablesResolverTrait;
  38.     public function __construct(
  39.         private readonly ProviderInterface $provider,
  40.         ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory null,
  41.         private readonly ?SerializerContextBuilderInterface $serializerContextBuilder null,
  42.         LegacyUriVariablesConverterInterface|UriVariablesConverterInterface|null $uriVariablesConverter null,
  43.     ) {
  44.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  45.         $this->uriVariablesConverter $uriVariablesConverter;
  46.     }
  47.     /**
  48.      * Calls the data provider and sets the data attribute.
  49.      *
  50.      * @throws NotFoundHttpException
  51.      */
  52.     public function onKernelRequest(RequestEvent $event): void
  53.     {
  54.         $request $event->getRequest();
  55.         $operation $this->initializeOperation($request);
  56.         if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
  57.             return;
  58.         }
  59.         if (!($attributes RequestAttributesExtractor::extractAttributes($request))) {
  60.             return;
  61.         }
  62.         if (!$attributes['receive'] || !$operation || !($operation->canRead() ?? true) || (!$operation->getUriVariables() && !$request->isMethodSafe())) {
  63.             return;
  64.         }
  65.         $context = ['operation' => $operation];
  66.         if (null === $filters $request->attributes->get('_api_filters')) {
  67.             $queryString RequestParser::getQueryString($request);
  68.             $filters $queryString RequestParser::parseRequestParams($queryString) : null;
  69.         }
  70.         if ($filters) {
  71.             $context['filters'] = $filters;
  72.         }
  73.         if ($this->serializerContextBuilder) {
  74.             // Builtin data providers are able to use the serialization context to automatically add join clauses
  75.             $context += $normalizationContext $this->serializerContextBuilder->createFromRequest($requesttrue$attributes);
  76.             $request->attributes->set('_api_normalization_context'$normalizationContext);
  77.         }
  78.         $parameters $request->attributes->all();
  79.         $resourceClass $operation->getClass() ?? $attributes['resource_class'];
  80.         try {
  81.             $uriVariables $this->getOperationUriVariables($operation$parameters$resourceClass);
  82.             $data $this->provider->provide($operation$uriVariables$context);
  83.         } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
  84.             throw new NotFoundHttpException('Invalid identifier value or configuration.'$e);
  85.         } catch (ProviderNotFoundException $e) {
  86.             $data null;
  87.         }
  88.         if (
  89.             null === $data
  90.             && 'POST' !== $operation->getMethod()
  91.             && (
  92.                 'PUT' !== $operation->getMethod()
  93.                 || ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
  94.             )
  95.         ) {
  96.             throw new NotFoundHttpException('Not Found');
  97.         }
  98.         $request->attributes->set('data'$data);
  99.         $request->attributes->set('previous_data'$this->clone($data));
  100.     }
  101. }