|
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 $autowire = true; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
private $annotations = false; |
|
31
|
|
|
|
|
32
|
20 |
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
20 |
|
$this->init(); |
|
35
|
20 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param bool $autowire |
|
39
|
|
|
*/ |
|
40
|
20 |
|
public function setAutowire(bool $autowire): void |
|
41
|
|
|
{ |
|
42
|
20 |
|
$this->autowire = $autowire; |
|
43
|
20 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
19 |
|
private function isAutowireEnabled(): bool |
|
49
|
|
|
{ |
|
50
|
19 |
|
return $this->autowire; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param bool $annotations |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function setAnnotations(bool $annotations): void |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->annotations = $annotations; |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
19 |
|
private function areAnnotationsEnabled(): bool |
|
65
|
|
|
{ |
|
66
|
19 |
|
return $this->annotations; |
|
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
|
19 |
|
private function getDefinitions(): ArrayObject |
|
89
|
|
|
{ |
|
90
|
19 |
|
return $this->definitions; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return Container |
|
95
|
|
|
*/ |
|
96
|
19 |
|
public function build(): Container |
|
97
|
|
|
{ |
|
98
|
19 |
|
$container = new Container(new DefinitionFactory(), new ResolverFactory()); |
|
99
|
19 |
|
$container->setAutowire($this->isAutowireEnabled()); |
|
100
|
19 |
|
$container->setAnnotations($this->areAnnotationsEnabled()); |
|
101
|
19 |
|
$container->setDefinitions($this->getDefinitions()); |
|
102
|
19 |
|
$this->init(); |
|
103
|
|
|
|
|
104
|
19 |
|
return $container; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
20 |
|
private function init(): void |
|
111
|
|
|
{ |
|
112
|
20 |
|
$this->setAutowire(true); |
|
113
|
20 |
|
$this->definitions = new ArrayObject(); |
|
114
|
20 |
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|