Просмотр файла vendor/php-di/invoker/src/ParameterResolver/Container/ParameterNameContainerResolver.php

Размер файла: 1.27Kb
  1. <?php declare(strict_types=1);
  2.  
  3. namespace Invoker\ParameterResolver\Container;
  4.  
  5. use Invoker\ParameterResolver\ParameterResolver;
  6. use Psr\Container\ContainerInterface;
  7. use ReflectionFunctionAbstract;
  8.  
  9. /**
  10. * Inject entries from a DI container using the parameter names.
  11. */
  12. class ParameterNameContainerResolver implements ParameterResolver
  13. {
  14. /** @var ContainerInterface */
  15. private $container;
  16.  
  17. /**
  18. * @param ContainerInterface $container The container to get entries from.
  19. */
  20. public function __construct(ContainerInterface $container)
  21. {
  22. $this->container = $container;
  23. }
  24.  
  25. public function getParameters(
  26. ReflectionFunctionAbstract $reflection,
  27. array $providedParameters,
  28. array $resolvedParameters
  29. ): array {
  30. $parameters = $reflection->getParameters();
  31.  
  32. // Skip parameters already resolved
  33. if (! empty($resolvedParameters)) {
  34. $parameters = array_diff_key($parameters, $resolvedParameters);
  35. }
  36.  
  37. foreach ($parameters as $index => $parameter) {
  38. $name = $parameter->name;
  39.  
  40. if ($name && $this->container->has($name)) {
  41. $resolvedParameters[$index] = $this->container->get($name);
  42. }
  43. }
  44.  
  45. return $resolvedParameters;
  46. }
  47. }