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

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