Total Complexity | 71 |
Total Lines | 468 |
Duplicated Lines | 0 % |
Coverage | 98.05% |
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 |
||
49 | class Container implements ContainerInterface, ArrayAccess, IteratorAggregate, Countable |
||
50 | { |
||
51 | /** |
||
52 | * 容器对象实例 |
||
53 | * @var Container|Closure |
||
54 | */ |
||
55 | protected static $instance; |
||
56 | |||
57 | /** |
||
58 | * 容器中的对象实例 |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $instances = []; |
||
62 | |||
63 | /** |
||
64 | * 容器绑定标识 |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $bind = [ |
||
68 | 'app' => App::class, |
||
69 | 'cache' => Cache::class, |
||
70 | 'config' => Config::class, |
||
71 | 'console' => Console::class, |
||
72 | 'cookie' => Cookie::class, |
||
73 | 'db' => Db::class, |
||
74 | 'debug' => Debug::class, |
||
75 | 'env' => Env::class, |
||
76 | 'event' => Event::class, |
||
77 | 'http' => Http::class, |
||
78 | 'lang' => Lang::class, |
||
79 | 'log' => Log::class, |
||
80 | 'middleware' => Middleware::class, |
||
81 | 'request' => Request::class, |
||
82 | 'response' => Response::class, |
||
83 | 'route' => Route::class, |
||
84 | 'session' => Session::class, |
||
85 | 'validate' => Validate::class, |
||
86 | 'view' => View::class, |
||
87 | |||
88 | // 接口依赖注入 |
||
89 | 'Psr\Log\LoggerInterface' => Log::class, |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * 获取当前容器的实例(单例) |
||
94 | * @access public |
||
95 | * @return static |
||
96 | */ |
||
97 | 4 | public static function getInstance() |
|
98 | { |
||
99 | 4 | if (static::$instance === null) { |
|
100 | 2 | static::$instance = new static; |
|
101 | } |
||
102 | |||
103 | 4 | if (static::$instance instanceof Closure) { |
|
104 | 1 | return (static::$instance)(); |
|
105 | } |
||
106 | |||
107 | 4 | return static::$instance; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * 设置当前容器的实例 |
||
112 | * @access public |
||
113 | * @param object|Closure $instance |
||
1 ignored issue
–
show
|
|||
114 | * @return void |
||
115 | */ |
||
116 | 29 | public static function setInstance($instance): void |
|
119 | 29 | } |
|
120 | |||
121 | /** |
||
122 | * 获取容器中的对象实例 不存在则创建 |
||
123 | * @access public |
||
124 | * @param string $abstract 类名或者标识 |
||
1 ignored issue
–
show
|
|||
125 | * @param array|true $vars 变量 |
||
1 ignored issue
–
show
|
|||
126 | * @param bool $newInstance 是否每次创建新的实例 |
||
1 ignored issue
–
show
|
|||
127 | * @return object |
||
128 | */ |
||
129 | 1 | public static function pull(string $abstract, array $vars = [], bool $newInstance = false) |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * 获取容器中的对象实例 |
||
136 | * @access public |
||
137 | * @param string $abstract 类名或者标识 |
||
1 ignored issue
–
show
|
|||
138 | * @return object |
||
139 | */ |
||
140 | 12 | public function get($abstract) |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * 绑定一个类、闭包、实例、接口实现到容器 |
||
151 | * @access public |
||
152 | * @param string|array $abstract 类标识、接口 |
||
1 ignored issue
–
show
|
|||
153 | * @param mixed $concrete 要绑定的类、闭包或者实例 |
||
1 ignored issue
–
show
|
|||
154 | * @return $this |
||
155 | */ |
||
156 | 9 | public function bind($abstract, $concrete = null) |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * 绑定一个类实例到容器 |
||
173 | * @access public |
||
174 | * @param string $abstract 类名或者标识 |
||
1 ignored issue
–
show
|
|||
175 | * @param object $instance 类的实例 |
||
1 ignored issue
–
show
|
|||
176 | * @return $this |
||
177 | */ |
||
178 | 22 | public function instance(string $abstract, $instance) |
|
179 | { |
||
180 | 22 | if (isset($this->bind[$abstract])) { |
|
181 | 19 | $bind = $this->bind[$abstract]; |
|
182 | |||
183 | 19 | if (is_string($bind)) { |
|
184 | 19 | return $this->instance($bind, $instance); |
|
185 | } |
||
186 | } |
||
187 | |||
188 | 22 | $this->instances[$abstract] = $instance; |
|
189 | |||
190 | 22 | return $this; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * 判断容器中是否存在类及标识 |
||
195 | * @access public |
||
196 | * @param string $abstract 类名或者标识 |
||
1 ignored issue
–
show
|
|||
197 | * @return bool |
||
198 | */ |
||
199 | 12 | public function bound(string $abstract): bool |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * 判断容器中是否存在类及标识 |
||
206 | * @access public |
||
207 | * @param string $name 类名或者标识 |
||
1 ignored issue
–
show
|
|||
208 | * @return bool |
||
209 | */ |
||
210 | 12 | public function has($name): bool |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * 判断容器中是否存在对象实例 |
||
217 | * @access public |
||
218 | * @param string $abstract 类名或者标识 |
||
1 ignored issue
–
show
|
|||
219 | * @return bool |
||
220 | */ |
||
221 | 4 | public function exists(string $abstract): bool |
|
222 | { |
||
223 | 4 | if (isset($this->bind[$abstract])) { |
|
224 | 3 | $bind = $this->bind[$abstract]; |
|
225 | |||
226 | 3 | if (is_string($bind)) { |
|
227 | 2 | return $this->exists($bind); |
|
228 | } |
||
229 | } |
||
230 | |||
231 | 4 | return isset($this->instances[$abstract]); |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * 创建类的实例 已经存在则直接获取 |
||
236 | * @access public |
||
237 | * @param string $abstract 类名或者标识 |
||
1 ignored issue
–
show
|
|||
238 | * @param array $vars 变量 |
||
1 ignored issue
–
show
|
|||
239 | * @param bool $newInstance 是否每次创建新的实例 |
||
1 ignored issue
–
show
|
|||
240 | * @return mixed |
||
241 | */ |
||
242 | 16 | public function make(string $abstract, array $vars = [], bool $newInstance = false) |
|
265 | } |
||
266 | |||
267 | /** |
||
268 | * 删除容器中的对象实例 |
||
269 | * @access public |
||
270 | * @param string $name 类名或者标识 |
||
1 ignored issue
–
show
|
|||
271 | * @return void |
||
272 | */ |
||
273 | 2 | public function delete($name) |
|
285 | } |
||
286 | 2 | } |
|
287 | |||
288 | /** |
||
289 | * 执行函数或者闭包方法 支持参数调用 |
||
290 | * @access public |
||
291 | * @param mixed $function 函数或者闭包 |
||
1 ignored issue
–
show
|
|||
292 | * @param array $vars 参数 |
||
1 ignored issue
–
show
|
|||
293 | * @return mixed |
||
294 | */ |
||
295 | 5 | public function invokeFunction($function, array $vars = []) |
|
296 | { |
||
297 | try { |
||
298 | 5 | $reflect = new ReflectionFunction($function); |
|
299 | |||
300 | 4 | $args = $this->bindParams($reflect, $vars); |
|
301 | |||
302 | 4 | return call_user_func_array($function, $args); |
|
303 | 1 | } catch (ReflectionException $e) { |
|
304 | 1 | throw new Exception('function not exists: ' . $function . '()'); |
|
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * 调用反射执行类的方法 支持参数绑定 |
||
310 | * @access public |
||
311 | * @param mixed $method 方法 |
||
1 ignored issue
–
show
|
|||
312 | * @param array $vars 参数 |
||
1 ignored issue
–
show
|
|||
313 | * @return mixed |
||
314 | */ |
||
315 | 3 | public function invokeMethod($method, array $vars = []) |
|
316 | { |
||
317 | try { |
||
318 | 3 | if (is_array($method)) { |
|
319 | 3 | $class = is_object($method[0]) ? $method[0] : $this->invokeClass($method[0]); |
|
320 | 3 | $reflect = new ReflectionMethod($class, $method[1]); |
|
321 | } else { |
||
322 | // 静态方法 |
||
323 | 1 | $reflect = new ReflectionMethod($method); |
|
324 | } |
||
325 | |||
326 | 2 | $args = $this->bindParams($reflect, $vars); |
|
327 | |||
328 | 2 | return $reflect->invokeArgs($class ?? null, $args); |
|
329 | 1 | } catch (ReflectionException $e) { |
|
330 | 1 | if (is_array($method)) { |
|
331 | 1 | $class = is_object($method[0]) ? get_class($method[0]) : $method[0]; |
|
332 | 1 | $callback = $class . '::' . $method[1]; |
|
333 | } else { |
||
334 | $callback = $method; |
||
335 | } |
||
336 | |||
337 | 1 | throw new Exception('method not exists: ' . $callback . '()'); |
|
338 | } |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * 调用反射执行类的方法 支持参数绑定 |
||
343 | * @access public |
||
344 | * @param object $instance 对象实例 |
||
1 ignored issue
–
show
|
|||
345 | * @param mixed $reflect 反射类 |
||
1 ignored issue
–
show
|
|||
346 | * @param array $vars 参数 |
||
1 ignored issue
–
show
|
|||
347 | * @return mixed |
||
348 | */ |
||
349 | 1 | public function invokeReflectMethod($instance, $reflect, array $vars = []) |
|
350 | { |
||
351 | 1 | $args = $this->bindParams($reflect, $vars); |
|
352 | |||
353 | 1 | return $reflect->invokeArgs($instance, $args); |
|
354 | } |
||
355 | |||
356 | /** |
||
357 | * 调用反射执行callable 支持参数绑定 |
||
358 | * @access public |
||
359 | * @param mixed $callable |
||
1 ignored issue
–
show
|
|||
360 | * @param array $vars 参数 |
||
1 ignored issue
–
show
|
|||
361 | * @return mixed |
||
362 | */ |
||
363 | 2 | public function invoke($callable, array $vars = []) |
|
364 | { |
||
365 | 2 | if ($callable instanceof Closure) { |
|
366 | 1 | return $this->invokeFunction($callable, $vars); |
|
367 | } |
||
368 | |||
369 | 2 | return $this->invokeMethod($callable, $vars); |
|
370 | } |
||
371 | |||
372 | /** |
||
373 | * 调用反射执行类的实例化 支持依赖注入 |
||
374 | * @access public |
||
375 | * @param string $class 类名 |
||
1 ignored issue
–
show
|
|||
376 | * @param array $vars 参数 |
||
1 ignored issue
–
show
|
|||
377 | * @return mixed |
||
378 | */ |
||
379 | 16 | public function invokeClass(string $class, array $vars = []) |
|
380 | { |
||
381 | try { |
||
382 | 16 | $reflect = new ReflectionClass($class); |
|
383 | |||
384 | 15 | if ($reflect->hasMethod('__make')) { |
|
385 | 11 | $method = new ReflectionMethod($class, '__make'); |
|
386 | |||
387 | 11 | if ($method->isPublic() && $method->isStatic()) { |
|
388 | 11 | $args = $this->bindParams($method, $vars); |
|
389 | 11 | return $method->invokeArgs(null, $args); |
|
390 | } |
||
391 | } |
||
392 | |||
393 | 12 | $constructor = $reflect->getConstructor(); |
|
394 | |||
395 | 12 | $args = $constructor ? $this->bindParams($constructor, $vars) : []; |
|
396 | |||
397 | 12 | return $reflect->newInstanceArgs($args); |
|
398 | 1 | } catch (ReflectionException $e) { |
|
399 | 1 | throw new ClassNotFoundException('class not exists: ' . $class, $class); |
|
400 | } |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * 绑定参数 |
||
405 | * @access protected |
||
406 | * @param \ReflectionMethod|\ReflectionFunction $reflect 反射类 |
||
1 ignored issue
–
show
|
|||
407 | * @param array $vars 参数 |
||
1 ignored issue
–
show
|
|||
408 | * @return array |
||
409 | */ |
||
410 | 18 | protected function bindParams($reflect, array $vars = []): array |
|
411 | { |
||
412 | 18 | if ($reflect->getNumberOfParameters() == 0) { |
|
413 | 7 | return []; |
|
414 | } |
||
415 | |||
416 | // 判断数组类型 数字数组时按顺序绑定参数 |
||
417 | 11 | reset($vars); |
|
418 | 11 | $type = key($vars) === 0 ? 1 : 0; |
|
419 | 11 | $params = $reflect->getParameters(); |
|
420 | 11 | $args = []; |
|
421 | |||
422 | 11 | foreach ($params as $param) { |
|
423 | 11 | $name = $param->getName(); |
|
424 | 11 | $lowerName = App::parseName($name); |
|
425 | 11 | $class = $param->getClass(); |
|
426 | |||
427 | 11 | if ($class) { |
|
428 | 11 | $args[] = $this->getObjectParam($class->getName(), $vars); |
|
429 | 9 | } elseif (1 == $type && !empty($vars)) { |
|
430 | 1 | $args[] = array_shift($vars); |
|
431 | 9 | } elseif (0 == $type && isset($vars[$name])) { |
|
432 | 1 | $args[] = $vars[$name]; |
|
433 | 9 | } elseif (0 == $type && isset($vars[$lowerName])) { |
|
434 | 1 | $args[] = $vars[$lowerName]; |
|
435 | 9 | } elseif ($param->isDefaultValueAvailable()) { |
|
436 | 9 | $args[] = $param->getDefaultValue(); |
|
437 | } else { |
||
438 | 11 | throw new InvalidArgumentException('method param miss:' . $name); |
|
439 | } |
||
440 | } |
||
441 | |||
442 | 11 | return $args; |
|
443 | } |
||
444 | |||
445 | /** |
||
446 | * 获取对象类型的参数值 |
||
447 | * @access protected |
||
448 | * @param string $className 类名 |
||
1 ignored issue
–
show
|
|||
449 | * @param array $vars 参数 |
||
1 ignored issue
–
show
|
|||
450 | * @return mixed |
||
451 | */ |
||
452 | 11 | protected function getObjectParam(string $className, array &$vars) |
|
453 | { |
||
454 | 11 | $array = $vars; |
|
455 | 11 | $value = array_shift($array); |
|
456 | |||
457 | 11 | if ($value instanceof $className) { |
|
458 | 1 | $result = $value; |
|
459 | 1 | array_shift($vars); |
|
460 | } else { |
||
461 | 11 | $result = $this->make($className); |
|
462 | } |
||
463 | |||
464 | 11 | return $result; |
|
465 | } |
||
466 | |||
467 | 1 | public function __set($name, $value) |
|
468 | { |
||
469 | 1 | $this->bind($name, $value); |
|
470 | 1 | } |
|
471 | |||
472 | 11 | public function __get($name) |
|
473 | { |
||
474 | 11 | return $this->get($name); |
|
475 | } |
||
476 | |||
477 | 1 | public function __isset($name): bool |
|
480 | } |
||
481 | |||
482 | 2 | public function __unset($name) |
|
483 | { |
||
484 | 2 | $this->delete($name); |
|
485 | 2 | } |
|
486 | |||
487 | 1 | public function offsetExists($key) |
|
488 | { |
||
489 | 1 | return $this->exists($key); |
|
490 | } |
||
491 | |||
492 | 1 | public function offsetGet($key) |
|
495 | } |
||
496 | |||
497 | 1 | public function offsetSet($key, $value) |
|
498 | { |
||
499 | 1 | $this->bind($key, $value); |
|
500 | 1 | } |
|
501 | |||
502 | 1 | public function offsetUnset($key) |
|
503 | { |
||
504 | 1 | $this->delete($key); |
|
505 | 1 | } |
|
506 | |||
507 | //Countable |
||
508 | public function count() |
||
509 | { |
||
510 | return count($this->instances); |
||
511 | } |
||
512 | |||
513 | //IteratorAggregate |
||
514 | 1 | public function getIterator() |
|
517 | } |
||
518 | } |
||
519 |