1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace DI\Definition\Resolver;
|
|
|
6 |
|
|
|
7 |
use DI\Definition\Definition;
|
|
|
8 |
use DI\Definition\Exception\InvalidDefinition;
|
|
|
9 |
use DI\Definition\FactoryDefinition;
|
|
|
10 |
use DI\Invoker\FactoryParameterResolver;
|
|
|
11 |
use Invoker\Exception\NotCallableException;
|
|
|
12 |
use Invoker\Exception\NotEnoughParametersException;
|
|
|
13 |
use Invoker\Invoker;
|
|
|
14 |
use Invoker\ParameterResolver\AssociativeArrayResolver;
|
|
|
15 |
use Invoker\ParameterResolver\DefaultValueResolver;
|
|
|
16 |
use Invoker\ParameterResolver\NumericArrayResolver;
|
|
|
17 |
use Invoker\ParameterResolver\ResolverChain;
|
|
|
18 |
use Psr\Container\ContainerInterface;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Resolves a factory definition to a value.
|
|
|
22 |
*
|
|
|
23 |
* @template-implements DefinitionResolver<FactoryDefinition>
|
|
|
24 |
*
|
|
|
25 |
* @since 4.0
|
|
|
26 |
* @author Matthieu Napoli <matthieu@mnapoli.fr>
|
|
|
27 |
*/
|
|
|
28 |
class FactoryResolver implements DefinitionResolver
|
|
|
29 |
{
|
|
|
30 |
private ?Invoker $invoker = null;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* The resolver needs a container. This container will be passed to the factory as a parameter
|
|
|
34 |
* so that the factory can access other entries of the container.
|
|
|
35 |
*/
|
|
|
36 |
public function __construct(
|
|
|
37 |
private ContainerInterface $container,
|
|
|
38 |
private DefinitionResolver $resolver,
|
|
|
39 |
) {
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Resolve a factory definition to a value.
|
|
|
44 |
*
|
|
|
45 |
* This will call the callable of the definition.
|
|
|
46 |
*
|
|
|
47 |
* @param FactoryDefinition $definition
|
|
|
48 |
*/
|
|
|
49 |
public function resolve(Definition $definition, array $parameters = []) : mixed
|
|
|
50 |
{
|
|
|
51 |
if (! $this->invoker) {
|
|
|
52 |
$parameterResolver = new ResolverChain([
|
|
|
53 |
new AssociativeArrayResolver,
|
|
|
54 |
new FactoryParameterResolver($this->container),
|
|
|
55 |
new NumericArrayResolver,
|
|
|
56 |
new DefaultValueResolver,
|
|
|
57 |
]);
|
|
|
58 |
|
|
|
59 |
$this->invoker = new Invoker($parameterResolver, $this->container);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
$callable = $definition->getCallable();
|
|
|
63 |
|
|
|
64 |
try {
|
|
|
65 |
$providedParams = [$this->container, $definition];
|
|
|
66 |
$extraParams = $this->resolveExtraParams($definition->getParameters());
|
|
|
67 |
$providedParams = array_merge($providedParams, $extraParams, $parameters);
|
|
|
68 |
|
|
|
69 |
return $this->invoker->call($callable, $providedParams);
|
|
|
70 |
} catch (NotCallableException $e) {
|
|
|
71 |
// Custom error message to help debugging
|
|
|
72 |
if (is_string($callable) && class_exists($callable) && method_exists($callable, '__invoke')) {
|
|
|
73 |
throw new InvalidDefinition(sprintf(
|
|
|
74 |
'Entry "%s" cannot be resolved: factory %s. Invokable classes cannot be automatically resolved if autowiring is disabled on the container, you need to enable autowiring or define the entry manually.',
|
|
|
75 |
$definition->getName(),
|
|
|
76 |
$e->getMessage()
|
|
|
77 |
));
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
throw new InvalidDefinition(sprintf(
|
|
|
81 |
'Entry "%s" cannot be resolved: factory %s',
|
|
|
82 |
$definition->getName(),
|
|
|
83 |
$e->getMessage()
|
|
|
84 |
));
|
|
|
85 |
} catch (NotEnoughParametersException $e) {
|
|
|
86 |
throw new InvalidDefinition(sprintf(
|
|
|
87 |
'Entry "%s" cannot be resolved: %s',
|
|
|
88 |
$definition->getName(),
|
|
|
89 |
$e->getMessage()
|
|
|
90 |
));
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
public function isResolvable(Definition $definition, array $parameters = []) : bool
|
|
|
95 |
{
|
|
|
96 |
return true;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
private function resolveExtraParams(array $params) : array
|
|
|
100 |
{
|
|
|
101 |
$resolved = [];
|
|
|
102 |
foreach ($params as $key => $value) {
|
|
|
103 |
// Nested definitions
|
|
|
104 |
if ($value instanceof Definition) {
|
|
|
105 |
$value = $this->resolver->resolve($value);
|
|
|
106 |
}
|
|
|
107 |
$resolved[$key] = $value;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
return $resolved;
|
|
|
111 |
}
|
|
|
112 |
}
|