3 namespace DI\Definition\Resolver;
5 use DI\Definition\Definition;
6 use DI\Definition\Exception\DefinitionException;
7 use DI\Proxy\ProxyFactory;
8 use Interop\Container\ContainerInterface;
11 * Dispatches to more specific resolvers.
13 * Dynamic dispatch pattern.
16 * @author Matthieu Napoli <matthieu@mnapoli.fr>
18 class ResolverDispatcher implements DefinitionResolver
21 * @var ContainerInterface
28 private $proxyFactory;
30 private $valueResolver;
31 private $arrayResolver;
32 private $factoryResolver;
33 private $decoratorResolver;
34 private $aliasResolver;
35 private $objectResolver;
36 private $instanceResolver;
37 private $envVariableResolver;
38 private $stringResolver;
40 public function __construct(ContainerInterface $container, ProxyFactory $proxyFactory)
42 $this->container = $container;
43 $this->proxyFactory = $proxyFactory;
47 * Resolve a definition to a value.
49 * @param Definition $definition Object that defines how the value should be obtained.
50 * @param array $parameters Optional parameters to use to build the entry.
52 * @throws DefinitionException If the definition cannot be resolved.
54 * @return mixed Value obtained from the definition.
56 public function resolve(Definition $definition, array $parameters = [])
58 $definitionResolver = $this->getDefinitionResolver($definition);
60 return $definitionResolver->resolve($definition, $parameters);
64 * Check if a definition can be resolved.
66 * @param Definition $definition Object that defines how the value should be obtained.
67 * @param array $parameters Optional parameters to use to build the entry.
71 public function isResolvable(Definition $definition, array $parameters = [])
73 $definitionResolver = $this->getDefinitionResolver($definition);
75 return $definitionResolver->isResolvable($definition, $parameters);
79 * Returns a resolver capable of handling the given definition.
81 * @param Definition $definition
83 * @throws \RuntimeException No definition resolver was found for this type of definition.
84 * @return DefinitionResolver
86 private function getDefinitionResolver(Definition $definition)
89 case ($definition instanceof \DI\Definition\ObjectDefinition):
90 if (! $this->objectResolver) {
91 $this->objectResolver = new ObjectCreator($this, $this->proxyFactory);
93 return $this->objectResolver;
94 case ($definition instanceof \DI\Definition\ValueDefinition):
95 if (! $this->valueResolver) {
96 $this->valueResolver = new ValueResolver();
98 return $this->valueResolver;
99 case ($definition instanceof \DI\Definition\AliasDefinition):
100 if (! $this->aliasResolver) {
101 $this->aliasResolver = new AliasResolver($this->container);
103 return $this->aliasResolver;
104 case ($definition instanceof \DI\Definition\DecoratorDefinition):
105 if (! $this->decoratorResolver) {
106 $this->decoratorResolver = new DecoratorResolver($this->container, $this);
108 return $this->decoratorResolver;
109 case ($definition instanceof \DI\Definition\FactoryDefinition):
110 if (! $this->factoryResolver) {
111 $this->factoryResolver = new FactoryResolver($this->container);
113 return $this->factoryResolver;
114 case ($definition instanceof \DI\Definition\ArrayDefinition):
115 if (! $this->arrayResolver) {
116 $this->arrayResolver = new ArrayResolver($this);
118 return $this->arrayResolver;
119 case ($definition instanceof \DI\Definition\EnvironmentVariableDefinition):
120 if (! $this->envVariableResolver) {
121 $this->envVariableResolver = new EnvironmentVariableResolver($this);
123 return $this->envVariableResolver;
124 case ($definition instanceof \DI\Definition\StringDefinition):
125 if (! $this->stringResolver) {
126 $this->stringResolver = new StringResolver($this->container);
128 return $this->stringResolver;
129 case ($definition instanceof \DI\Definition\InstanceDefinition):
130 if (! $this->instanceResolver) {
131 $this->instanceResolver = new InstanceInjector($this, $this->proxyFactory);
133 return $this->instanceResolver;
135 throw new \RuntimeException('No definition resolver was configured for definition of type ' . get_class($definition));