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

  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\Exception\ProviderNotFoundException;
  20. use ApiPlatform\State\ProviderInterface;
  21. use ApiPlatform\State\UriVariablesResolverTrait;
  22. use ApiPlatform\Util\CloneTrait;
  23. use ApiPlatform\Util\OperationRequestInitiatorTrait;
  24. use ApiPlatform\Util\RequestAttributesExtractor;
  25. use ApiPlatform\Util\RequestParser;
  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.         UriVariablesConverterInterface $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 (!($attributes RequestAttributesExtractor::extractAttributes($request))) {
  57.             return;
  58.         }
  59.         if (!$attributes['receive'] || !$operation || !($operation->canRead() ?? true) || (!$operation->getUriVariables() && !$request->isMethodSafe())) {
  60.             return;
  61.         }
  62.         $context = ['operation' => $operation];
  63.         if (null === $filters $request->attributes->get('_api_filters')) {
  64.             $queryString RequestParser::getQueryString($request);
  65.             $filters $queryString RequestParser::parseRequestParams($queryString) : null;
  66.         }
  67.         if ($filters) {
  68.             $context['filters'] = $filters;
  69.         }
  70.         if ($this->serializerContextBuilder) {
  71.             // Builtin data providers are able to use the serialization context to automatically add join clauses
  72.             $context += $normalizationContext $this->serializerContextBuilder->createFromRequest($requesttrue$attributes);
  73.             $request->attributes->set('_api_normalization_context'$normalizationContext);
  74.         }
  75.         $parameters $request->attributes->all();
  76.         $resourceClass $operation->getClass() ?? $attributes['resource_class'];
  77.         try {
  78.             $uriVariables $this->getOperationUriVariables($operation$parameters$resourceClass);
  79.             $data $this->provider->provide($operation$uriVariables$context);
  80.         } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
  81.             throw new NotFoundHttpException('Invalid identifier value or configuration.'$e);
  82.         } catch (ProviderNotFoundException $e) {
  83.             $data null;
  84.         }
  85.         if (
  86.             null === $data
  87.             && HttpOperation::METHOD_POST !== $operation->getMethod()
  88.             && (
  89.                 HttpOperation::METHOD_PUT !== $operation->getMethod()
  90.                 || ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
  91.             )
  92.         ) {
  93.             throw new NotFoundHttpException('Not Found');
  94.         }
  95.         $request->attributes->set('data'$data);
  96.         $request->attributes->set('previous_data'$this->clone($data));
  97.     }
  98. }