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

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