1 | <?php |
||
25 | class Container implements ContainerInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var ArrayObject |
||
29 | */ |
||
30 | private $definitions; |
||
31 | |||
32 | /** |
||
33 | * @var ArrayObject |
||
34 | */ |
||
35 | private $entries; |
||
36 | |||
37 | /** |
||
38 | * @var ArrayObject |
||
39 | */ |
||
40 | private $resolving; |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | private $autowire = true; |
||
46 | |||
47 | /** |
||
48 | * @var ResolverFactoryInterface |
||
49 | */ |
||
50 | private $resolverFactory; |
||
51 | |||
52 | /** |
||
53 | * @var DefinitionFactoryInterface |
||
54 | */ |
||
55 | private $definitionFactory; |
||
56 | |||
57 | /** |
||
58 | * @param DefinitionFactoryInterface $definitionFactory |
||
59 | * @param ResolverFactoryInterface $resolverFactory |
||
60 | */ |
||
61 | 10 | public function __construct(DefinitionFactoryInterface $definitionFactory, ResolverFactoryInterface $resolverFactory) |
|
62 | { |
||
63 | 10 | $this->definitions = new ArrayObject(); |
|
64 | 10 | $this->entries = new ArrayObject(); |
|
65 | 10 | $this->resolving = new ArrayObject(); |
|
66 | 10 | $this->definitionFactory = $definitionFactory; |
|
67 | 10 | $this->resolverFactory = $resolverFactory; |
|
68 | 10 | $this->resolverFactory->setContainer($this); |
|
69 | 10 | } |
|
70 | |||
71 | /** |
||
72 | * @inheritDoc |
||
73 | */ |
||
74 | 10 | public function get($id) |
|
75 | { |
||
76 | 10 | if (!is_string($id)) { |
|
77 | 2 | $type = is_object($id) ? get_class($id) : gettype($id); |
|
78 | 2 | throw new InvalidArgumentException("Argument \$id must be string, $type given"); |
|
79 | } |
||
80 | |||
81 | 8 | if ($this->entries->offsetExists($id)) { |
|
82 | return $this->entries->offsetGet($id); |
||
83 | } |
||
84 | |||
85 | 8 | $entry = $this->resolveEntry($id); |
|
86 | 7 | $this->entries->offsetSet($id, $entry); |
|
87 | |||
88 | 7 | return $entry; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @inheritDoc |
||
93 | */ |
||
94 | public function has($id): bool |
||
106 | |||
107 | /** |
||
108 | * @param string $id |
||
109 | * |
||
110 | * @throws ContainerException |
||
111 | * @throws DefinitionNotFoundException |
||
112 | * @throws InvalidDefinitionException |
||
113 | * @throws NotFoundException |
||
114 | * |
||
115 | * @return mixed |
||
116 | */ |
||
117 | 8 | private function resolveEntry(string $id) |
|
127 | |||
128 | /** |
||
129 | * @param bool $autowire |
||
130 | */ |
||
131 | 10 | public function setAutowire(bool $autowire): void |
|
135 | |||
136 | 7 | private function isAutowire(): bool |
|
140 | |||
141 | /** |
||
142 | * @param array $definitions |
||
143 | * |
||
144 | * @throws ContainerException |
||
145 | */ |
||
146 | 8 | public function addDefinitions(array $definitions): void |
|
152 | |||
153 | /** |
||
154 | * @param string $id |
||
155 | * @param $definition |
||
156 | * |
||
157 | * @throws ContainerException |
||
158 | */ |
||
159 | 8 | public function addDefinition(string $id, $definition): void |
|
167 | |||
168 | /** |
||
169 | * @param string $id |
||
170 | * |
||
171 | * @throws DefinitionNotFoundException |
||
172 | * @throws InvalidDefinitionException |
||
173 | * |
||
174 | * @return DefinitionInterface |
||
175 | */ |
||
176 | 8 | private function getDefinition(string $id): DefinitionInterface |
|
192 | |||
193 | /** |
||
194 | * @param string $id |
||
195 | * |
||
196 | * @throws CircularDependencyFoundException |
||
197 | * |
||
198 | * @return void |
||
199 | */ |
||
200 | 8 | private function startResolving(string $id): void |
|
208 | |||
209 | /** |
||
210 | * @param string $id |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | 7 | private function endResolving(string $id): void |
|
220 | } |
||
221 |