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;
  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.     private $serializerContextBuilder;
  37.     private $provider;
  38.     public function __construct(ProviderInterface $providerResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactorySerializerContextBuilderInterface $serializerContextBuilder nullUriVariablesConverterInterface $uriVariablesConverter null)
  39.     {
  40.         $this->provider $provider;
  41.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  42.         $this->serializerContextBuilder $serializerContextBuilder;
  43.         $this->uriVariablesConverter $uriVariablesConverter;
  44.     }
  45.     /**
  46.      * Calls the data provider and sets the data attribute.
  47.      *
  48.      * @throws NotFoundHttpException
  49.      */
  50.     public function onKernelRequest(RequestEvent $event): void
  51.     {
  52.         $request $event->getRequest();
  53.         $operation $this->initializeOperation($request);
  54.         if (!($attributes RequestAttributesExtractor::extractAttributes($request))) {
  55.             return;
  56.         }
  57.         if (!$attributes['receive'] || !$operation || !($operation->canRead() ?? true) || (($extraProperties $operation->getExtraProperties())['is_legacy_resource_metadata'] ?? false) || ($extraProperties['is_legacy_subresource'] ?? false) || (!$operation->getUriVariables() && !$request->isMethodSafe())) {
  58.             return;
  59.         }
  60.         $context = ['operation' => $operation];
  61.         if (null === $filters $request->attributes->get('_api_filters')) {
  62.             $queryString RequestParser::getQueryString($request);
  63.             $filters $queryString RequestParser::parseRequestParams($queryString) : null;
  64.         }
  65.         if ($filters) {
  66.             $context['filters'] = $filters;
  67.         }
  68.         if ($this->serializerContextBuilder) {
  69.             // Builtin data providers are able to use the serialization context to automatically add join clauses
  70.             $context += $normalizationContext $this->serializerContextBuilder->createFromRequest($requesttrue$attributes);
  71.             $request->attributes->set('_api_normalization_context'$normalizationContext);
  72.         }
  73.         $parameters $request->attributes->all();
  74.         $resourceClass $operation->getClass() ?? $attributes['resource_class'];
  75.         try {
  76.             $uriVariables $this->getOperationUriVariables($operation$parameters$resourceClass);
  77.             $data $this->provider->provide($operation$uriVariables$context);
  78.         } catch (InvalidIdentifierException $e) {
  79.             throw new NotFoundHttpException('Invalid identifier value or configuration.'$e);
  80.         } catch (InvalidUriVariableException $e) {
  81.             throw new NotFoundHttpException('Invalid identifier value or configuration.'$e);
  82.         }
  83.         if (null === $data) {
  84.             throw new NotFoundHttpException('Not Found');
  85.         }
  86.         $request->attributes->set('data'$data);
  87.         $request->attributes->set('previous_data'$this->clone($data));
  88.     }
  89. }