1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Laganica\Di; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use Laganica\Di\Definition\DefinitionFactory; |
7
|
|
|
use Laganica\Di\Exception\ContainerException; |
8
|
|
|
use Laganica\Di\Resolver\ResolverFactory; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ContainerBuilder |
12
|
|
|
* |
13
|
|
|
* @package Laganica\Di |
14
|
|
|
*/ |
15
|
|
|
class ContainerBuilder |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ArrayObject |
19
|
|
|
*/ |
20
|
|
|
private $definitions; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private $useAutowiring; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $useAnnotations = false; |
31
|
|
|
|
32
|
21 |
|
public function __construct() |
33
|
|
|
{ |
34
|
21 |
|
$this->init(); |
35
|
21 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param bool $useAutowiring |
39
|
|
|
*/ |
40
|
21 |
|
public function useAutowiring(bool $useAutowiring): void |
41
|
|
|
{ |
42
|
21 |
|
$this->useAutowiring = $useAutowiring; |
43
|
21 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
20 |
|
private function hasAutowiringEnabled(): bool |
49
|
|
|
{ |
50
|
20 |
|
return $this->useAutowiring; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param bool $useAnnotations |
55
|
|
|
*/ |
56
|
1 |
|
public function useAnnotations(bool $useAnnotations): void |
57
|
|
|
{ |
58
|
1 |
|
$this->useAnnotations = $useAnnotations; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
20 |
|
private function hasAnnotationsEnabled(): bool |
65
|
|
|
{ |
66
|
20 |
|
return $this->useAnnotations; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $definitions |
71
|
|
|
* |
72
|
|
|
* @throws ContainerException |
73
|
|
|
*/ |
74
|
15 |
|
public function addDefinitions(array $definitions): void |
75
|
|
|
{ |
76
|
15 |
|
foreach ($definitions as $id => $definition) { |
77
|
15 |
|
if ($this->definitions->offsetExists($id)) { |
78
|
1 |
|
throw new ContainerException("More than one definition is found for entry or class $id"); |
79
|
|
|
} |
80
|
|
|
|
81
|
15 |
|
$this->definitions->offsetSet($id, $definition); |
82
|
|
|
} |
83
|
15 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return ArrayObject |
87
|
|
|
*/ |
88
|
20 |
|
private function getDefinitions(): ArrayObject |
89
|
|
|
{ |
90
|
20 |
|
return $this->definitions; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return Container |
95
|
|
|
*/ |
96
|
20 |
|
public function build(): Container |
97
|
|
|
{ |
98
|
20 |
|
$container = new Container(new DefinitionFactory(), new ResolverFactory()); |
99
|
20 |
|
$container->setAutowire($this->hasAutowiringEnabled()); |
100
|
20 |
|
$container->setAnnotations($this->hasAnnotationsEnabled()); |
101
|
20 |
|
$container->setDefinitions($this->getDefinitions()); |
102
|
|
|
|
103
|
20 |
|
$this->reset(); |
104
|
|
|
|
105
|
20 |
|
return $container; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
21 |
|
private function init(): void |
112
|
|
|
{ |
113
|
21 |
|
$this->useAutowiring(true); |
114
|
21 |
|
$this->definitions = new ArrayObject(); |
115
|
21 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
20 |
|
private function reset(): void |
121
|
|
|
{ |
122
|
20 |
|
$this->init(); |
123
|
20 |
|
} |
124
|
|
|
} |
125
|
|
|
|