|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Habemus\Definition; |
|
5
|
|
|
|
|
6
|
|
|
use Habemus\Autowiring\Attributes\AttributesInjection; |
|
7
|
|
|
use Habemus\Container; |
|
8
|
|
|
use Habemus\Definition\Build\RawDefinition; |
|
9
|
|
|
use Habemus\ResolvedList; |
|
10
|
|
|
use Habemus\Definition\MethodCall\CallableMethod; |
|
11
|
|
|
use Habemus\Definition\Sharing\Shareable; |
|
12
|
|
|
|
|
13
|
|
|
class DefinitionResolver implements DefinitionResolverInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Container |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $container; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ResolvedList |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $resolved; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var AttributesInjection |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $attributesInjection; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(Container $container, ResolvedList $resolved, AttributesInjection $attributesInjection) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->container = $container; |
|
33
|
|
|
$this->resolved = $resolved; |
|
34
|
|
|
$this->attributesInjection = $attributesInjection; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritDoc |
|
39
|
|
|
*/ |
|
40
|
|
|
public function resolve(Definition $definition) |
|
41
|
|
|
{ |
|
42
|
|
|
$instance = $definition->getConcrete($this->container); |
|
43
|
|
|
|
|
44
|
|
|
if ($definition instanceof CallableMethod) { |
|
45
|
|
|
($definition->getMethodCall())($instance, $this->container); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($this->shouldShare($definition)) { |
|
49
|
|
|
$this->resolved->share($definition->getIdentity(), $instance); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if ($this->shouldInjectPropertyDependencies($instance, $definition)) { |
|
53
|
|
|
$this->attributesInjection->inject($instance); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $instance; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritDoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public function resolveMany(Definition ...$definitions): array |
|
63
|
|
|
{ |
|
64
|
|
|
return array_map( |
|
65
|
|
|
function (Definition $definition) { |
|
66
|
|
|
return $this->resolve($definition); |
|
67
|
|
|
}, |
|
68
|
|
|
$definitions |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Definition $definition |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function shouldShare(Definition $definition): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return |
|
79
|
|
|
$definition instanceof RawDefinition || |
|
80
|
|
|
($definition instanceof Shareable && $definition->isShared() && $definition->getIdentity() !== null); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param mixed $instance |
|
85
|
|
|
* @param Definition $definition |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function shouldInjectPropertyDependencies($instance, Definition $definition): bool |
|
89
|
|
|
{ |
|
90
|
|
|
return |
|
91
|
|
|
$this->container->attributesEnabled() && |
|
92
|
|
|
is_object($instance) && |
|
93
|
|
|
!($definition instanceof RawDefinition); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|