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

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\Core\EventListener;
  12. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  13. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  14. use ApiPlatform\Core\DataProvider\OperationDataProviderTrait;
  15. use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
  16. use ApiPlatform\Core\Exception\InvalidIdentifierException;
  17. use ApiPlatform\Core\Exception\RuntimeException;
  18. use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
  19. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  20. use ApiPlatform\Core\Metadata\Resource\ToggleableOperationAttributeTrait;
  21. use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
  22. use ApiPlatform\Core\Util\CloneTrait;
  23. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  24. use ApiPlatform\Core\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 OperationDataProviderTrait;
  36.     use ToggleableOperationAttributeTrait;
  37.     public const OPERATION_ATTRIBUTE_KEY 'read';
  38.     private $serializerContextBuilder;
  39.     public function __construct(CollectionDataProviderInterface $collectionDataProviderItemDataProviderInterface $itemDataProviderSubresourceDataProviderInterface $subresourceDataProvider nullSerializerContextBuilderInterface $serializerContextBuilder nullIdentifierConverterInterface $identifierConverter nullResourceMetadataFactoryInterface $resourceMetadataFactory null)
  40.     {
  41.         $this->collectionDataProvider $collectionDataProvider;
  42.         $this->itemDataProvider $itemDataProvider;
  43.         $this->subresourceDataProvider $subresourceDataProvider;
  44.         $this->serializerContextBuilder $serializerContextBuilder;
  45.         $this->identifierConverter $identifierConverter;
  46.         $this->resourceMetadataFactory $resourceMetadataFactory;
  47.     }
  48.     /**
  49.      * Calls the data provider and sets the data attribute.
  50.      *
  51.      * @throws NotFoundHttpException
  52.      */
  53.     public function onKernelRequest(RequestEvent $event): void
  54.     {
  55.         $request $event->getRequest();
  56.         if (
  57.             !($attributes RequestAttributesExtractor::extractAttributes($request))
  58.             || !$attributes['receive']
  59.             || $request->isMethod('POST') && isset($attributes['collection_operation_name'])
  60.             || $this->isOperationAttributeDisabled($attributesself::OPERATION_ATTRIBUTE_KEY)
  61.         ) {
  62.             return;
  63.         }
  64.         if (null === $filters $request->attributes->get('_api_filters')) {
  65.             $queryString RequestParser::getQueryString($request);
  66.             $filters $queryString RequestParser::parseRequestParams($queryString) : null;
  67.         }
  68.         $context null === $filters ? [] : ['filters' => $filters];
  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.         if (isset($attributes['collection_operation_name'])) {
  75.             $request->attributes->set('data'$this->getCollectionData($attributes$context));
  76.             return;
  77.         }
  78.         $data = [];
  79.         if ($this->identifierConverter) {
  80.             $context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] = true;
  81.         }
  82.         try {
  83.             $identifiers $this->extractIdentifiers($request->attributes->all(), $attributes);
  84.             if (isset($attributes['item_operation_name'])) {
  85.                 $data $this->getItemData($identifiers$attributes$context);
  86.             } elseif (isset($attributes['subresource_operation_name'])) {
  87.                 // Legacy
  88.                 if (null === $this->subresourceDataProvider) {
  89.                     throw new RuntimeException('No subresource data provider.');
  90.                 }
  91.                 $data $this->getSubresourceData($identifiers$attributes$context);
  92.             }
  93.         } catch (InvalidIdentifierException $e) {
  94.             throw new NotFoundHttpException('Invalid identifier value or configuration.'$e);
  95.         }
  96.         if (null === $data) {
  97.             throw new NotFoundHttpException('Not Found');
  98.         }
  99.         $request->attributes->set('data'$data);
  100.         $request->attributes->set('previous_data'$this->clone($data));
  101.     }
  102. }