| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition\Resolver; |
| 6 |
|
| 7 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition; |
| 8 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Exception\InvalidDefinition; |
| 9 |
use WPDeveloper\BetterDocs\Dependencies\DI\Proxy\ProxyFactory; |
| 10 |
use WPDeveloper\BetterDocs\Dependencies\Psr\Container\ContainerInterface; |
| 11 |
|
| 12 |
/** |
| 13 |
* Dispatches to more specific resolvers. |
| 14 |
* |
| 15 |
* Dynamic dispatch pattern. |
| 16 |
* |
| 17 |
* @since 5.0 |
| 18 |
* @author Matthieu Napoli <matthieu@mnapoli.fr> |
| 19 |
*/ |
| 20 |
class ResolverDispatcher implements DefinitionResolver |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var ContainerInterface |
| 24 |
*/ |
| 25 |
private $container; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var ProxyFactory |
| 29 |
*/ |
| 30 |
private $proxyFactory; |
| 31 |
|
| 32 |
private $arrayResolver; |
| 33 |
private $factoryResolver; |
| 34 |
private $decoratorResolver; |
| 35 |
private $objectResolver; |
| 36 |
private $instanceResolver; |
| 37 |
private $envVariableResolver; |
| 38 |
|
| 39 |
public function __construct(ContainerInterface $container, ProxyFactory $proxyFactory) |
| 40 |
{ |
| 41 |
$this->container = $container; |
| 42 |
$this->proxyFactory = $proxyFactory; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Resolve a definition to a value. |
| 47 |
* |
| 48 |
* @param Definition $definition Object that defines how the value should be obtained. |
| 49 |
* @param array $parameters Optional parameters to use to build the entry. |
| 50 |
* |
| 51 |
* @throws InvalidDefinition If the definition cannot be resolved. |
| 52 |
* |
| 53 |
* @return mixed Value obtained from the definition. |
| 54 |
*/ |
| 55 |
public function resolve(Definition $definition, array $parameters = []) |
| 56 |
{ |
| 57 |
// Special case, tested early for speed |
| 58 |
if ($definition instanceof \WPDeveloper\BetterDocs\Dependencies\DI\Definition\SelfResolvingDefinition) { |
| 59 |
return $definition->resolve($this->container); |
| 60 |
} |
| 61 |
|
| 62 |
$definitionResolver = $this->getDefinitionResolver($definition); |
| 63 |
|
| 64 |
return $definitionResolver->resolve($definition, $parameters); |
| 65 |
} |
| 66 |
|
| 67 |
public function isResolvable(Definition $definition, array $parameters = []) : bool |
| 68 |
{ |
| 69 |
// Special case, tested early for speed |
| 70 |
if ($definition instanceof \WPDeveloper\BetterDocs\Dependencies\DI\Definition\SelfResolvingDefinition) { |
| 71 |
return $definition->isResolvable($this->container); |
| 72 |
} |
| 73 |
|
| 74 |
$definitionResolver = $this->getDefinitionResolver($definition); |
| 75 |
|
| 76 |
return $definitionResolver->isResolvable($definition, $parameters); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Returns a resolver capable of handling the given definition. |
| 81 |
* |
| 82 |
* @throws \RuntimeException No definition resolver was found for this type of definition. |
| 83 |
*/ |
| 84 |
private function getDefinitionResolver(Definition $definition) : DefinitionResolver |
| 85 |
{ |
| 86 |
switch (true) { |
| 87 |
case $definition instanceof \WPDeveloper\BetterDocs\Dependencies\DI\Definition\ObjectDefinition: |
| 88 |
if (! $this->objectResolver) { |
| 89 |
$this->objectResolver = new ObjectCreator($this, $this->proxyFactory); |
| 90 |
} |
| 91 |
|
| 92 |
return $this->objectResolver; |
| 93 |
case $definition instanceof \WPDeveloper\BetterDocs\Dependencies\DI\Definition\DecoratorDefinition: |
| 94 |
if (! $this->decoratorResolver) { |
| 95 |
$this->decoratorResolver = new DecoratorResolver($this->container, $this); |
| 96 |
} |
| 97 |
|
| 98 |
return $this->decoratorResolver; |
| 99 |
case $definition instanceof \WPDeveloper\BetterDocs\Dependencies\DI\Definition\FactoryDefinition: |
| 100 |
if (! $this->factoryResolver) { |
| 101 |
$this->factoryResolver = new FactoryResolver($this->container, $this); |
| 102 |
} |
| 103 |
|
| 104 |
return $this->factoryResolver; |
| 105 |
case $definition instanceof \WPDeveloper\BetterDocs\Dependencies\DI\Definition\ArrayDefinition: |
| 106 |
if (! $this->arrayResolver) { |
| 107 |
$this->arrayResolver = new ArrayResolver($this); |
| 108 |
} |
| 109 |
|
| 110 |
return $this->arrayResolver; |
| 111 |
case $definition instanceof \WPDeveloper\BetterDocs\Dependencies\DI\Definition\EnvironmentVariableDefinition: |
| 112 |
if (! $this->envVariableResolver) { |
| 113 |
$this->envVariableResolver = new EnvironmentVariableResolver($this); |
| 114 |
} |
| 115 |
|
| 116 |
return $this->envVariableResolver; |
| 117 |
case $definition instanceof \WPDeveloper\BetterDocs\Dependencies\DI\Definition\InstanceDefinition: |
| 118 |
if (! $this->instanceResolver) { |
| 119 |
$this->instanceResolver = new InstanceInjector($this, $this->proxyFactory); |
| 120 |
} |
| 121 |
|
| 122 |
return $this->instanceResolver; |
| 123 |
default: |
| 124 |
throw new \RuntimeException('No definition resolver was configured for definition of type ' . get_class($definition)); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|