vendor/api-platform/core/src/Core/DataProvider/ChainSubresourceDataProvider.php line 43

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 Antoine Bluchet <soyuka@gmail.com>
  17. */
  18. final class ChainSubresourceDataProvider implements SubresourceDataProviderInterface
  19. {
  20. /**
  21. * @var iterable<SubresourceDataProviderInterface>
  22. *
  23. * @internal
  24. */
  25. public $dataProviders;
  26. /**
  27. * @param SubresourceDataProviderInterface[] $dataProviders
  28. */
  29. public function __construct(iterable $dataProviders)
  30. {
  31. $this->dataProviders = $dataProviders;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null)
  37. {
  38. foreach ($this->dataProviders as $dataProvider) {
  39. try {
  40. if ($dataProvider instanceof RestrictedDataProviderInterface && !$dataProvider->supports($resourceClass, $operationName, $context)) {
  41. continue;
  42. }
  43. return $dataProvider->getSubresource($resourceClass, $identifiers, $context, $operationName);
  44. } catch (ResourceClassNotSupportedException $e) {
  45. @trigger_error(sprintf('Throwing a "%s" in a data provider is deprecated in favor of implementing "%s"', ResourceClassNotSupportedException::class, RestrictedDataProviderInterface::class), \E_USER_DEPRECATED);
  46. continue;
  47. }
  48. }
  49. return ($context['collection'] ?? false) ? [] : null;
  50. }
  51. }