vendor/api-platform/core/src/Bridge/Symfony/Bundle/DataProvider/TraceableChainItemDataProvider.php line 48

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\Bridge\Symfony\Bundle\DataProvider;
  12. use ApiPlatform\Core\DataProvider\ChainItemDataProvider;
  13. use ApiPlatform\Core\DataProvider\DenormalizedIdentifiersAwareItemDataProviderInterface;
  14. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  15. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  16. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  17. /**
  18.  * @author Anthony GRASSIOT <antograssiot@free.fr>
  19.  */
  20. final class TraceableChainItemDataProvider implements ItemDataProviderInterface
  21. {
  22.     private $dataProviders = [];
  23.     private $context = [];
  24.     private $providersResponse = [];
  25.     public function __construct(ItemDataProviderInterface $itemDataProvider)
  26.     {
  27.         if ($itemDataProvider instanceof ChainItemDataProvider) {
  28.             $this->dataProviders $itemDataProvider->dataProviders;
  29.         }
  30.     }
  31.     public function getProvidersResponse(): array
  32.     {
  33.         return $this->providersResponse;
  34.     }
  35.     public function getContext(): array
  36.     {
  37.         return $this->context;
  38.     }
  39.     public function getItem(string $resourceClass$idstring $operationName null, array $context = [])
  40.     {
  41.         $this->context $context;
  42.         $match false;
  43.         $result null;
  44.         foreach ($this->dataProviders as $dataProvider) {
  45.             $this->providersResponse[\get_class($dataProvider)] = $match null false;
  46.             if ($match) {
  47.                 continue;
  48.             }
  49.             try {
  50.                 if ($dataProvider instanceof RestrictedDataProviderInterface
  51.                     && !$dataProvider->supports($resourceClass$operationName$context)) {
  52.                     continue;
  53.                 }
  54.                 $identifier $id;
  55.                 if (!$dataProvider instanceof DenormalizedIdentifiersAwareItemDataProviderInterface && $identifier && \is_array($identifier)) {
  56.                     if (\count($identifier) > 1) {
  57.                         @trigger_error(sprintf('Receiving "$id" as non-array in an item data provider is deprecated in 2.3 in favor of implementing "%s".'DenormalizedIdentifiersAwareItemDataProviderInterface::class), E_USER_DEPRECATED);
  58.                         $identifier http_build_query($identifier''';');
  59.                     } else {
  60.                         $identifier current($identifier);
  61.                     }
  62.                 }
  63.                 $result $dataProvider->getItem($resourceClass$identifier$operationName$context);
  64.                 $this->providersResponse[\get_class($dataProvider)] = $match true;
  65.             } catch (ResourceClassNotSupportedException $e) {
  66.                 @trigger_error(sprintf('Throwing a "%s" is deprecated in favor of implementing "%s"', \get_class($e), RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
  67.                 continue;
  68.             }
  69.         }
  70.         return $result;
  71.     }
  72. }