vendor/api-platform/core/src/State/CallableProvider.php line 43

  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\State;
  12. use ApiPlatform\Exception\RuntimeException;
  13. use ApiPlatform\Metadata\Operation;
  14. use Psr\Container\ContainerInterface;
  15. final class CallableProvider implements ProviderInterface
  16. {
  17.     public function __construct(private readonly ContainerInterface $locator)
  18.     {
  19.     }
  20.     /**
  21.      * {@inheritDoc}
  22.      */
  23.     public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
  24.     {
  25.         if (\is_callable($provider $operation->getProvider())) {
  26.             return $provider($operation$uriVariables$context);
  27.         }
  28.         if (\is_string($provider)) {
  29.             if (!$this->locator->has($provider)) {
  30.                 throw new RuntimeException(sprintf('Provider "%s" not found on operation "%s"'$provider$operation->getName()));
  31.             }
  32.             /** @var ProviderInterface $providerInstance */
  33.             $providerInstance $this->locator->get($provider);
  34.             return $providerInstance->provide($operation$uriVariables$context);
  35.         }
  36.         throw new RuntimeException(sprintf('Provider not found on operation "%s"'$operation->getName()));
  37.     }
  38. }