1 | <?php |
||
23 | class Container implements ContainerInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $definitions = []; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $entries = []; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | private $autowire = true; |
||
39 | |||
40 | /** |
||
41 | * @var ResolverFactoryInterface |
||
42 | */ |
||
43 | private $resolverFactory; |
||
44 | |||
45 | /** |
||
46 | * @var DefinitionFactoryInterface |
||
47 | */ |
||
48 | private $definitionFactory; |
||
49 | |||
50 | /** |
||
51 | * @param DefinitionFactoryInterface $definitionFactory |
||
52 | * @param ResolverFactoryInterface $resolverFactory |
||
53 | */ |
||
54 | 7 | public function __construct(DefinitionFactoryInterface $definitionFactory, ResolverFactoryInterface $resolverFactory) |
|
55 | { |
||
56 | 7 | $this->definitionFactory = $definitionFactory; |
|
57 | 7 | $this->resolverFactory = $resolverFactory; |
|
58 | 7 | $this->resolverFactory->setContainer($this); |
|
59 | 7 | } |
|
60 | |||
61 | /** |
||
62 | * @inheritDoc |
||
63 | */ |
||
64 | 7 | public function get($id) |
|
65 | { |
||
66 | 7 | if (!is_string($id)) { |
|
67 | $type = get_class($id) ?: gettype($id); |
||
68 | throw new InvalidArgumentException("Argument \$id must be string, $type given"); |
||
69 | } |
||
70 | |||
71 | 7 | if ($entry = $this->getEntry($id)) { |
|
72 | return $entry; |
||
73 | } |
||
74 | |||
75 | 7 | return $this->addEntry($id, $this->resolveEntry($id)); |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * @inheritDoc |
||
80 | */ |
||
81 | public function has($id): bool |
||
93 | |||
94 | /** |
||
95 | * @param string $id |
||
96 | * |
||
97 | * @throws ContainerException |
||
98 | * @throws DefinitionNotFoundException |
||
99 | * @throws InvalidDefinitionException |
||
100 | * @throws NotFoundException |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | 7 | private function resolveEntry(string $id) |
|
113 | |||
114 | /** |
||
115 | * @param bool $autowire |
||
116 | */ |
||
117 | 7 | public function setAutowire(bool $autowire): void |
|
121 | |||
122 | 6 | public function isAutowire(): bool |
|
126 | |||
127 | /** |
||
128 | * @param array $definitions |
||
129 | * |
||
130 | * @throws ContainerException |
||
131 | */ |
||
132 | 7 | public function addDefinitions(array $definitions): void |
|
138 | |||
139 | /** |
||
140 | * @param string $id |
||
141 | * @param $definition |
||
142 | * |
||
143 | * @throws ContainerException |
||
144 | */ |
||
145 | 7 | public function addDefinition(string $id, $definition): void |
|
153 | |||
154 | /** |
||
155 | * @param string $id |
||
156 | * |
||
157 | * @throws DefinitionNotFoundException |
||
158 | * @throws InvalidDefinitionException |
||
159 | * |
||
160 | * @return DefinitionInterface |
||
161 | */ |
||
162 | 7 | public function getDefinition(string $id): DefinitionInterface |
|
176 | |||
177 | /** |
||
178 | * @param string $id |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | 7 | public function hasDefinition(string $id): bool |
|
186 | |||
187 | /** |
||
188 | * @param string $id |
||
189 | * @param mixed $entry |
||
190 | * |
||
191 | * @return mixed |
||
192 | */ |
||
193 | 7 | private function addEntry(string $id, $entry) |
|
197 | |||
198 | /** |
||
199 | * @param string $id |
||
200 | * |
||
201 | * @return null|mixed |
||
202 | */ |
||
203 | 7 | public function getEntry(string $id) |
|
207 | } |
||
208 |