983dea414f84338aa1cc97fb0a924e434163a197
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 namespace Invoker\ParameterResolver\Container;
4
5 use Interop\Container\ContainerInterface;
6 use Invoker\ParameterResolver\ParameterResolver;
7 use ReflectionFunctionAbstract;
8
9 /**
10 * Inject entries from a DI container using the parameter names.
11 *
12 * @author Matthieu Napoli <matthieu@mnapoli.fr>
13 */
14 class ParameterNameContainerResolver implements ParameterResolver
15 {
16 /**
17 * @var ContainerInterface
18 */
19 private $container;
20
21 /**
22 * @param ContainerInterface $container The container to get entries from.
23 */
24 public function __construct(ContainerInterface $container)
25 {
26 $this->container = $container;
27 }
28
29 public function getParameters(
30 ReflectionFunctionAbstract $reflection,
31 array $providedParameters,
32 array $resolvedParameters
33 ) {
34 $parameters = $reflection->getParameters();
35
36 // Skip parameters already resolved
37 if (! empty($resolvedParameters)) {
38 $parameters = array_diff_key($parameters, $resolvedParameters);
39 }
40
41 foreach ($parameters as $index => $parameter) {
42 $name = $parameter->name;
43
44 if ($name && $this->container->has($name)) {
45 $resolvedParameters[$index] = $this->container->get($name);
46 }
47 }
48
49 return $resolvedParameters;
50 }
51 }