Total Complexity | 53 |
Total Lines | 415 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Container often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Container implements ContainerInterface |
||
20 | { |
||
21 | /** |
||
22 | * Config. |
||
23 | * |
||
24 | * @var Config |
||
25 | */ |
||
26 | protected $config; |
||
27 | |||
28 | /** |
||
29 | * Service registry. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $service = []; |
||
34 | |||
35 | /** |
||
36 | * Registered but not initialized service registry. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $registry = []; |
||
41 | |||
42 | /** |
||
43 | * Parent container. |
||
44 | * |
||
45 | * @var ContainerInterface |
||
46 | */ |
||
47 | protected $parent; |
||
48 | |||
49 | /** |
||
50 | * Children container. |
||
51 | * |
||
52 | * @var ContainerInterface[] |
||
53 | */ |
||
54 | protected $children = []; |
||
55 | |||
56 | /** |
||
57 | * Parent service. |
||
58 | * |
||
59 | * @var mixed |
||
60 | */ |
||
61 | protected $parent_service; |
||
62 | |||
63 | /** |
||
64 | * Create container. |
||
65 | * |
||
66 | * @param iterable $config |
||
67 | * @param ContainerInterface $parent |
||
68 | */ |
||
69 | public function __construct(Iterable $config = [], ?ContainerInterface $parent = null) |
||
70 | { |
||
71 | $this->config = new Config($config); |
||
72 | $this->parent = $parent; |
||
73 | $this->add(ContainerInterface::class, $this); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Get service. |
||
78 | * |
||
79 | * @param string $name |
||
80 | * |
||
81 | * @return mixed |
||
82 | */ |
||
83 | public function get($name) |
||
84 | { |
||
85 | $service = $this->resolve($name); |
||
86 | if (null !== $service) { |
||
87 | return $service; |
||
88 | } |
||
89 | |||
90 | try { |
||
91 | return $this->lookupService($name); |
||
92 | } catch (Exception\ServiceNotFound $e) { |
||
93 | return $this->autoWireClass($name); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Traverse tree up and look for service. |
||
99 | * |
||
100 | * @param string $name |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function lookupService(string $name) |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Add service. |
||
120 | * |
||
121 | * @param string $name |
||
122 | * @param mixed $service |
||
123 | * |
||
124 | * @return Container |
||
125 | */ |
||
126 | public function add(string $name, $service): self |
||
127 | { |
||
128 | if ($this->has($name)) { |
||
129 | throw new Exception\ServiceAlreadyExists('service '.$name.' is already registered'); |
||
130 | } |
||
131 | |||
132 | $this->registry[$name] = $service; |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Check if service is registered. |
||
139 | * |
||
140 | * @param mixed $name |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function has($name): bool |
||
145 | { |
||
146 | return isset($this->service[$name]); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Set parent service on container |
||
151 | * (Used internally, there is no point to call this method directly). |
||
152 | * |
||
153 | * @param mixed $service |
||
154 | * |
||
155 | * @return ContainerInterface |
||
156 | */ |
||
157 | public function setParentService($service): ContainerInterface |
||
158 | { |
||
159 | $this->parent_service = $service; |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Resolve service. |
||
166 | * |
||
167 | * @param string $name |
||
168 | * |
||
169 | * @return mixed |
||
170 | */ |
||
171 | protected function resolve(string $name) |
||
172 | { |
||
173 | if ($this->has($name)) { |
||
174 | return $this->service[$name]; |
||
175 | } |
||
176 | |||
177 | if (isset($this->registry[$name])) { |
||
178 | return $this->addStaticService($name); |
||
179 | } |
||
180 | |||
181 | if ($this->config->has($name)) { |
||
182 | return $this->autoWireClass($name); |
||
183 | } |
||
184 | |||
185 | if (null !== $this->parent_service) { |
||
186 | $parents = array_merge([$name], class_implements($this->parent_service), class_parents($this->parent_service)); |
||
187 | |||
188 | if (in_array($name, $parents, true) && $this->parent_service instanceof $name) { |
||
189 | return $this->parent_service; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | return null; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Check for static injections. |
||
198 | * |
||
199 | * @param string $name |
||
200 | * |
||
201 | * @return mixed |
||
202 | */ |
||
203 | protected function addStaticService(string $name) |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Auto wire. |
||
218 | * |
||
219 | * @param string $name |
||
220 | * @param array $config |
||
221 | * @param array $parents |
||
222 | * |
||
223 | * @return mixed |
||
224 | */ |
||
225 | protected function autoWireClass(string $name) |
||
226 | { |
||
227 | $config = $this->config->get($name); |
||
228 | $class = $config['use']; |
||
229 | |||
230 | if (preg_match('#^\{(.*)\}$#', $class, $match)) { |
||
231 | return $this->wireReference($name, $match[1], $config); |
||
232 | } |
||
233 | |||
234 | try { |
||
235 | $reflection = new ReflectionClass($class); |
||
236 | } catch (\Exception $e) { |
||
237 | throw new Exception\ServiceNotFound($class.' can not be resolved to an existing class for service '.$name); |
||
238 | } |
||
239 | |||
240 | $constructor = $reflection->getConstructor(); |
||
241 | |||
242 | if (null === $constructor) { |
||
243 | return new $class(); |
||
244 | } |
||
245 | |||
246 | $args = $this->autoWireMethod($name, $constructor, $config); |
||
247 | |||
248 | return $this->createInstance($name, $reflection, $args, $config); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Wire named referenced service. |
||
253 | * |
||
254 | * @param string $name |
||
255 | * @param string $refrence |
||
256 | * @param array $config |
||
257 | * |
||
258 | * @return mixed |
||
259 | */ |
||
260 | protected function wireReference(string $name, string $reference, array $config) |
||
261 | { |
||
262 | $service = $this->get($reference); |
||
263 | |||
264 | if (isset($config['selects'])) { |
||
265 | $reflection = new ReflectionClass(get_class($service)); |
||
266 | |||
267 | foreach ($config['selects'] as $select) { |
||
268 | $args = $this->autoWireMethod($name, $reflection->getMethod($select['method']), $select); |
||
269 | $service = call_user_func_array([&$service, $select['method']], $args); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | return $this->storeService($name, $config, $service); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Store service. |
||
278 | * |
||
279 | * @param param string $name |
||
280 | * @param array $config |
||
281 | * @param mixed $service |
||
282 | * |
||
283 | * @return mixed |
||
284 | */ |
||
285 | protected function storeService(string $name, array $config, $service) |
||
286 | { |
||
287 | if (isset($config['singleton']) && true === $config['singleton']) { |
||
288 | return $service; |
||
289 | } |
||
290 | |||
291 | $this->service[$name] = $service; |
||
292 | |||
293 | if (isset($this->children[$name])) { |
||
294 | $this->children[$name]->setParentService($service); |
||
295 | } |
||
296 | |||
297 | return $service; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Create instance. |
||
302 | * |
||
303 | * @param string $name |
||
304 | * @param ReflectionClass $class |
||
305 | * @param array $arguments |
||
306 | * @param array $config |
||
307 | * |
||
308 | * @return mixed |
||
309 | */ |
||
310 | protected function createInstance(string $name, ReflectionClass $class, array $arguments, array $config) |
||
311 | { |
||
312 | $instance = $class->newInstanceArgs($arguments); |
||
313 | $this->storeService($name, $config, $instance); |
||
314 | $config = $this->config->get($name); |
||
315 | |||
316 | if (!isset($config['calls'])) { |
||
317 | return $instance; |
||
318 | } |
||
319 | |||
320 | foreach ($config['calls'] as $call) { |
||
321 | if (!isset($call['method'])) { |
||
322 | throw new Exception\InvalidConfiguration('method is required for setter injection in service '.$name); |
||
323 | } |
||
324 | |||
325 | $arguments = []; |
||
|
|||
326 | |||
327 | try { |
||
328 | $method = $class->getMethod($call['method']); |
||
329 | } catch (\ReflectionException $e) { |
||
330 | throw new Exception\InvalidConfiguration('method '.$call['method'].' is not callable in class '.$class->getName().' for service '.$name); |
||
331 | } |
||
332 | |||
333 | $arguments = $this->autoWireMethod($name, $method, $call); |
||
334 | call_user_func_array([&$instance, $call['method']], $arguments); |
||
335 | } |
||
336 | |||
337 | return $instance; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Autowire method. |
||
342 | * |
||
343 | * @param string $name |
||
344 | * @param ReflectionMethod $method |
||
345 | * @param array $config |
||
346 | * |
||
347 | * @return array |
||
348 | */ |
||
349 | protected function autoWireMethod(string $name, ReflectionMethod $method, array $config): array |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Parse param value. |
||
382 | * |
||
383 | * @param mixed $param |
||
384 | * @param string $name |
||
385 | * |
||
386 | * @return mixed |
||
387 | */ |
||
388 | protected function parseParam($param, string $name) |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Locate service. |
||
416 | * |
||
417 | * @param string $current_service |
||
418 | * @param string $service |
||
419 | */ |
||
420 | protected function findService(string $current_service, string $service) |
||
434 | } |
||
435 | } |
||
436 |