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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
11 | class Container |
||
12 | { |
||
13 | /** |
||
14 | * 所有需要分享的类及其实例 |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $shares = []; |
||
18 | |||
19 | /** |
||
20 | * 预定义的依赖,支持instance、callable、Definition、class |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $definitions = []; |
||
24 | |||
25 | /** |
||
26 | * 全局接口与类绑定关系 |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $contextBindings = []; |
||
30 | |||
31 | /** |
||
32 | * 参数集合 |
||
33 | * @var ParameterStore |
||
34 | */ |
||
35 | protected $parameterStore; |
||
36 | |||
37 | public function __construct() |
||
42 | |||
43 | /** |
||
44 | * 给指定类或者别名指向类设置实例化向导 |
||
45 | * @param string $name |
||
46 | * @param string $class 类名 |
||
47 | * @param array $arguments 构造函数 |
||
48 | * @param array $methodCalls setter注入 |
||
49 | * @param array $properties 属性注入 |
||
50 | * @return Definition |
||
51 | */ |
||
52 | public function define($name, $class, array $arguments, array $methodCalls = [], array $properties = []) |
||
58 | |||
59 | /** |
||
60 | * 设置指定类的实例化代理 |
||
61 | * @param string $name |
||
62 | * @param mixed $creation 闭包及其它合法可调用的语法结构 |
||
63 | * @throws ConfigException |
||
64 | * @return $this |
||
65 | */ |
||
66 | public function delegate($name, $creation) |
||
74 | |||
75 | /** |
||
76 | * 绑定实例 |
||
77 | * ``` |
||
78 | * $container->instance('user', $user); |
||
79 | * //或者直接提供实例 |
||
80 | * $container->instance($user); |
||
81 | * |
||
82 | * ``` |
||
83 | * @param $name |
||
84 | * @param $instance |
||
85 | * @throws ConfigException |
||
86 | * @return $this |
||
87 | */ |
||
88 | public function instance($name, $instance) |
||
101 | |||
102 | /** |
||
103 | * 将name直接绑定到某个指定存在的类(可用来绑定接口或者抽象类与实现类) |
||
104 | * @param string $name |
||
105 | * @param string $class 一个可被实例化的类名 |
||
106 | * @param string|array $context 为指定的上下文设置绑定指令 |
||
107 | * @throws ConfigException |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function bind($name, $class, $context = null) |
||
127 | |||
128 | /** |
||
129 | * 给指定类或者类别名设置实例化指令 |
||
130 | * ``` |
||
131 | * //直接绑定实例 |
||
132 | * $container->set('student', $student); |
||
133 | * |
||
134 | * //绑定闭包或者其它可调用结构 |
||
135 | * $container->set('student', 'StudentFactory::create'); |
||
136 | * $container->set('student', function(){ |
||
137 | * return new Student(); |
||
138 | * }); |
||
139 | * |
||
140 | * //绑定预定义 |
||
141 | * $container->set('student', new Definition('Foo\Bar\StudentClass', [ |
||
142 | * 'gender' => 'boy', |
||
143 | * 'school' => new Reference('school') |
||
144 | * ], [ |
||
145 | * 'setAge' => [18] |
||
146 | * ], [ |
||
147 | * 'father' => 'James', |
||
148 | * 'mather' => 'Sophie' |
||
149 | * ])); |
||
150 | * |
||
151 | * //绑定到指定类 |
||
152 | * $container->set('student', Foo\Bar\StudentClass); |
||
153 | * ``` |
||
154 | * @param string $name |
||
155 | * @param mixed $definition |
||
156 | * @param boolean $share |
||
157 | * @throws ConfigException |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function set($name, $definition, $share = false) |
||
177 | |||
178 | /** |
||
179 | * 如果不能简单获取,则使用设置定义的方式 |
||
180 | * @param string $name |
||
181 | * @param Definition $definition |
||
182 | * @param boolean $share |
||
183 | * @return Definition |
||
184 | * @deprecated The method will be protected, Use define & share or set instead. |
||
185 | */ |
||
186 | public function setDefinition($name, Definition $definition, $share = false) |
||
190 | |||
191 | /** |
||
192 | * sets a definition |
||
193 | * @param string $name |
||
194 | * @param Definition $definition |
||
195 | * @param boolean $share |
||
196 | * @return Definition |
||
197 | */ |
||
198 | protected function bindDefinition($name, Definition $definition, $share = false) |
||
204 | |||
205 | /** |
||
206 | * 设置类或者类实例的分享 |
||
207 | * @param string $name |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function share($name) |
||
222 | |||
223 | /** |
||
224 | * 为指定类设置一个别名 |
||
225 | * @param string $alias |
||
226 | * @param string $original |
||
227 | * @return $this |
||
228 | * @deprecated duplication,use bind instead |
||
229 | */ |
||
230 | public function alias($alias, $original) |
||
234 | |||
235 | /** |
||
236 | * 以获取一个实例 |
||
237 | * @param string $name |
||
238 | * @param array $arguments 传递给类的构造参数,会覆盖预先定义的同名参数 |
||
239 | * @return object |
||
240 | */ |
||
241 | public function get($name, $arguments = []) |
||
275 | |||
276 | /** |
||
277 | * 分析类结构并自动解决依赖生成一个类实例 |
||
278 | * @param string $name 指定类或者类别名 |
||
279 | * @param array $arguments 构造参数,覆盖预定义参数 |
||
280 | * @throws DependencyInjectionException |
||
281 | * @return object |
||
282 | * @deprecated |
||
283 | */ |
||
284 | public function create($name, $arguments = []) |
||
289 | |||
290 | /** |
||
291 | * 获取所有预定义参数 |
||
292 | * @return array |
||
293 | */ |
||
294 | public function getParameters() |
||
298 | |||
299 | /** |
||
300 | * 设置预定义参数 |
||
301 | * @param array $parameterStore |
||
302 | */ |
||
303 | public function setParameters(array $parameterStore) |
||
307 | |||
308 | /** |
||
309 | * 添加预定义参数 |
||
310 | * @param array $parameters |
||
311 | */ |
||
312 | public function addParameters(array $parameters) |
||
316 | |||
317 | /** |
||
318 | * 设置参数 |
||
319 | * @param $name |
||
320 | * @param mixed $value |
||
321 | */ |
||
322 | public function setParameter($name, $value) |
||
326 | |||
327 | /** |
||
328 | * 获取参数 |
||
329 | * @param $name |
||
330 | * @param null $default |
||
331 | * @return mixed|null |
||
332 | */ |
||
333 | public function getParameter($name, $default = null) |
||
337 | |||
338 | /** |
||
339 | * 根据definition创建实例 |
||
340 | * @param Definition $definition |
||
341 | * @param array $arguments |
||
342 | * @throws DependencyInjectionException |
||
343 | * @return object |
||
344 | */ |
||
345 | protected function createFromDefinition(Definition $definition, array $arguments) |
||
352 | |||
353 | /** |
||
354 | * 构建实例 |
||
355 | * @param string $class |
||
356 | * @param array $arguments |
||
357 | * @throws DependencyInjectionException |
||
358 | * @return array |
||
359 | */ |
||
360 | protected function createReflectionAndInstance($class, array $arguments) |
||
379 | |||
380 | protected function prepareInstance(\ReflectionClass $reflection, $instance, Definition $definition) |
||
413 | |||
414 | /** |
||
415 | * 处理方法所需要的参数 |
||
416 | * @param \ReflectionFunctionAbstract $method |
||
417 | * @param array $arguments |
||
418 | * @param array $contextBindings 该方法定义的所有依赖绑定 |
||
419 | * @throws DependencyInjectionException |
||
420 | * @return array |
||
421 | */ |
||
422 | protected function resolveFunctionArguments(\ReflectionFunctionAbstract $method, array $arguments, array $contextBindings = []) |
||
458 | |||
459 | /** |
||
460 | * 获取指定类的绑定关系 |
||
461 | * [ |
||
462 | * 'User' => [ |
||
463 | * 'original' => 'SchoolInterface' |
||
464 | * 'bind' => 'MagicSchool', |
||
465 | * ] |
||
466 | * ] |
||
467 | * @param string $contextClass |
||
468 | * @param string $contextMethod |
||
469 | * @return mixed |
||
470 | */ |
||
471 | protected function getContextBindings($contextClass, $contextMethod) |
||
483 | |||
484 | /** |
||
485 | * 预处理参数 |
||
486 | * @param $parameters |
||
487 | * @return array |
||
488 | */ |
||
489 | protected function resolveParameters($parameters) |
||
503 | |||
504 | /** |
||
505 | * 处理字符串 |
||
506 | * @param $value |
||
507 | * @return mixed |
||
508 | * @throws DependencyInjectionException |
||
509 | */ |
||
510 | protected function resolveString($value) |
||
529 | |||
530 | /** |
||
531 | * 获取类的反射对象 |
||
532 | * @param string $className |
||
533 | * @throws DependencyInjectionException |
||
534 | * @return \ReflectionClass |
||
535 | */ |
||
536 | protected function reflectClass($className) |
||
545 | } |
||
546 |