| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cybot\Dependencies\DI\Definition\Resolver; |
| 4 |
|
| 5 |
use Cybot\Dependencies\DI\Definition\Definition; |
| 6 |
use Cybot\Dependencies\DI\Definition\Exception\DefinitionException; |
| 7 |
use Cybot\Dependencies\DI\Proxy\ProxyFactory; |
| 8 |
use Cybot\Dependencies\Interop\Container\ContainerInterface; |
| 9 |
|
| 10 |
/** |
| 11 |
* Dispatches to more specific resolvers. |
| 12 |
* |
| 13 |
* Dynamic dispatch pattern. |
| 14 |
* |
| 15 |
* @since 5.0 |
| 16 |
* @author Matthieu Napoli <[email protected]> |
| 17 |
*/ |
| 18 |
class ResolverDispatcher implements DefinitionResolver |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @var ContainerInterface |
| 22 |
*/ |
| 23 |
private $container; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var ProxyFactory |
| 27 |
*/ |
| 28 |
private $proxyFactory; |
| 29 |
|
| 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; |
| 39 |
|
| 40 |
public function __construct(ContainerInterface $container, ProxyFactory $proxyFactory) |
| 41 |
{ |
| 42 |
$this->container = $container; |
| 43 |
$this->proxyFactory = $proxyFactory; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Resolve a definition to a value. |
| 48 |
* |
| 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. |
| 51 |
* |
| 52 |
* @throws DefinitionException If the definition cannot be resolved. |
| 53 |
* |
| 54 |
* @return mixed Value obtained from the definition. |
| 55 |
*/ |
| 56 |
public function resolve(Definition $definition, array $parameters = []) |
| 57 |
{ |
| 58 |
$definitionResolver = $this->getDefinitionResolver($definition); |
| 59 |
|
| 60 |
return $definitionResolver->resolve($definition, $parameters); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Check if a definition can be resolved. |
| 65 |
* |
| 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. |
| 68 |
* |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
public function isResolvable(Definition $definition, array $parameters = []) |
| 72 |
{ |
| 73 |
$definitionResolver = $this->getDefinitionResolver($definition); |
| 74 |
|
| 75 |
return $definitionResolver->isResolvable($definition, $parameters); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Returns a resolver capable of handling the given definition. |
| 80 |
* |
| 81 |
* @param Definition $definition |
| 82 |
* |
| 83 |
* @throws \RuntimeException No definition resolver was found for this type of definition. |
| 84 |
* @return DefinitionResolver |
| 85 |
*/ |
| 86 |
private function getDefinitionResolver(Definition $definition) |
| 87 |
{ |
| 88 |
$definitionType = get_class($definition); |
| 89 |
|
| 90 |
switch ($definitionType) { |
| 91 |
case 'Cybot\Dependencies\DI\Definition\ValueDefinition': |
| 92 |
if (! $this->valueResolver) { |
| 93 |
$this->valueResolver = new ValueResolver(); |
| 94 |
} |
| 95 |
return $this->valueResolver; |
| 96 |
case 'Cybot\Dependencies\DI\Definition\ArrayDefinition': |
| 97 |
case 'Cybot\Dependencies\DI\Definition\ArrayDefinitionExtension': |
| 98 |
if (! $this->arrayResolver) { |
| 99 |
$this->arrayResolver = new ArrayResolver($this); |
| 100 |
} |
| 101 |
return $this->arrayResolver; |
| 102 |
case 'Cybot\Dependencies\DI\Definition\FactoryDefinition': |
| 103 |
if (! $this->factoryResolver) { |
| 104 |
$this->factoryResolver = new FactoryResolver($this->container); |
| 105 |
} |
| 106 |
return $this->factoryResolver; |
| 107 |
case 'Cybot\Dependencies\DI\Definition\DecoratorDefinition': |
| 108 |
if (! $this->decoratorResolver) { |
| 109 |
$this->decoratorResolver = new DecoratorResolver($this->container, $this); |
| 110 |
} |
| 111 |
return $this->decoratorResolver; |
| 112 |
case 'Cybot\Dependencies\DI\Definition\AliasDefinition': |
| 113 |
if (! $this->aliasResolver) { |
| 114 |
$this->aliasResolver = new AliasResolver($this->container); |
| 115 |
} |
| 116 |
return $this->aliasResolver; |
| 117 |
case 'Cybot\Dependencies\DI\Definition\ObjectDefinition': |
| 118 |
if (! $this->objectResolver) { |
| 119 |
$this->objectResolver = new ObjectCreator($this, $this->proxyFactory); |
| 120 |
} |
| 121 |
return $this->objectResolver; |
| 122 |
case 'Cybot\Dependencies\DI\Definition\InstanceDefinition': |
| 123 |
if (! $this->instanceResolver) { |
| 124 |
$this->instanceResolver = new InstanceInjector($this, $this->proxyFactory); |
| 125 |
} |
| 126 |
return $this->instanceResolver; |
| 127 |
case 'Cybot\Dependencies\DI\Definition\EnvironmentVariableDefinition': |
| 128 |
if (! $this->envVariableResolver) { |
| 129 |
$this->envVariableResolver = new EnvironmentVariableResolver($this); |
| 130 |
} |
| 131 |
return $this->envVariableResolver; |
| 132 |
case 'Cybot\Dependencies\DI\Definition\StringDefinition': |
| 133 |
if (! $this->stringResolver) { |
| 134 |
$this->stringResolver = new StringResolver($this->container); |
| 135 |
} |
| 136 |
return $this->stringResolver; |
| 137 |
default: |
| 138 |
throw new \RuntimeException("No definition resolver was configured for definition of type $definitionType"); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|