|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Laganica\Di\Resolver; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Laganica\Di\Definition\AliasDefinition; |
|
8
|
|
|
use Laganica\Di\Definition\BindDefinition; |
|
9
|
|
|
use Laganica\Di\Definition\DefinitionInterface; |
|
10
|
|
|
use Laganica\Di\Definition\FactoryDefinition; |
|
11
|
|
|
use Laganica\Di\Definition\ValueDefinition; |
|
12
|
|
|
use Psr\Container\ContainerInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class ResolverFactory |
|
16
|
|
|
* |
|
17
|
|
|
* @package Laganica\Di\Resolver |
|
18
|
|
|
*/ |
|
19
|
|
|
class ResolverFactory implements ResolverFactoryInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ContainerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $container; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param ContainerInterface $container |
|
28
|
|
|
*/ |
|
29
|
6 |
|
public function setContainer(ContainerInterface $container): void |
|
30
|
|
|
{ |
|
31
|
6 |
|
$this->container = $container; |
|
32
|
6 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritDoc |
|
36
|
|
|
*/ |
|
37
|
6 |
|
public function create($definition): ResolverInterface |
|
38
|
|
|
{ |
|
39
|
6 |
|
if ($definition instanceof DefinitionInterface) { |
|
40
|
4 |
|
return $this->resolveDefinition($definition); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
if ($definition instanceof Closure) { |
|
44
|
1 |
|
return new ClosureResolver($this->container); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
if (is_string($definition)) { |
|
48
|
1 |
|
return new ClassResolver($this->container); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
throw new InvalidArgumentException('Invalid definition found. Allowed definitions are bind(), value(), factory(), alias(), Closure or string'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param DefinitionInterface $definition |
|
56
|
|
|
* |
|
57
|
|
|
* @return ResolverInterface |
|
58
|
|
|
*/ |
|
59
|
4 |
|
private function resolveDefinition(DefinitionInterface $definition): ResolverInterface |
|
60
|
|
|
{ |
|
61
|
|
|
$classMap = [ |
|
62
|
4 |
|
BindDefinition::class => BindResolver::class, |
|
63
|
|
|
FactoryDefinition::class => FactoryResolver::class, |
|
64
|
|
|
ValueDefinition::class => ValueResolver::class, |
|
65
|
|
|
AliasDefinition::class => AliasResolver::class, |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
4 |
|
if (!$resolverClass = $classMap[get_class($definition)] ?? null) { |
|
69
|
|
|
throw new InvalidArgumentException('Invalid definition found. Allowed definitions are bind(), value(), factory(), alias(), Closure or string'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
return new $resolverClass($this->container); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|