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