vendor/api-platform/core/src/Core/DataProvider/ChainCollectionDataProvider.php line 38

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\DataProvider;
  12. use ApiPlatform\Exception\ResourceClassNotSupportedException;
  13. /**
  14. * Tries each configured data provider and returns the result of the first able to handle the resource class.
  15. *
  16. * @author Kévin Dunglas <dunglas@gmail.com>
  17. */
  18. final class ChainCollectionDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface
  19. {
  20. use RestrictDataProviderTrait;
  21. /**
  22. * @param CollectionDataProviderInterface[] $dataProviders
  23. */
  24. public function __construct(iterable $dataProviders)
  25. {
  26. $this->dataProviders = $dataProviders;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
  32. {
  33. foreach ($this->dataProviders as $dataProvider) {
  34. try {
  35. if ($dataProvider instanceof RestrictedDataProviderInterface
  36. && !$dataProvider->supports($resourceClass, $operationName, $context)) {
  37. continue;
  38. }
  39. return $dataProvider->getCollection($resourceClass, $operationName, $context);
  40. } catch (ResourceClassNotSupportedException $e) {
  41. @trigger_error(sprintf('Throwing a "%s" in a data provider is deprecated in favor of implementing "%s"', ResourceClassNotSupportedException::class, RestrictedDataProviderInterface::class), \E_USER_DEPRECATED);
  42. continue;
  43. }
  44. }
  45. return [];
  46. }
  47. }