Container   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 0
loc 99
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 4 2
A remove() 0 4 1
A set() 0 4 1
A setShared() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
C get() 0 33 8
A _registerService() 0 9 3
1
<?php
2
namespace graychen\container;
3
4
/**
5
* @brief 服务容器
6
* author 陈家辉
7
 */
8
class Container implements \ArrayAccess
9
{
10
    private $_bindings = [];//服务列表
11
    private $_instances= [];//已经实例化的服务
12
13
    //获取服务
14 5
    public function get($name, $params=[])
15
    {
16
        //先从实例化的列表中查找
17 5
        if (isset($this->_instances[$name])) {
18 1
            return $this->_instances[$name];
19
        }
20
21
        //检测有没有注册该服务
22 4
        if (!isset($this->_bindings[$name])) {
23 1
            return null;
24
        }
25
26 3
        $concrete = $this->_bindings[$name]['class'];//对象具体注册内容
27
28 3
        $obj = null;
29
30 3
        if ($concrete instanceof \Closure) { //匿名函数方式
31 1
            $obj = call_user_func_array($concrete, $params);
32 3
        } elseif (is_string($concrete)) {     //字符串方式
33 2
            if (empty($params)) {
34 1
                $obj = new $concrete;
35 1
            } else {
36
                //带参数的类实例化,使用反射
37 1
                $class = new \reflectionClass($concrete);
38 1
                $obj = $class->newInstanceArgs($params);
39
            }
40 2
        }
41
        //如果是共享服务,则写入_instances列表,下次直接取回
42 3
        if ($this->_bindings[$name]['shared']==true && $obj) {
43 1
            $this->_instances[$name]=$obj;
44 1
        }
45 3
        return $obj;
46
    }
47
48
    //检测是否已经绑定
49 1
    public function has($name)
50
    {
51 1
        return isset($this->_bindings[$name]) or isset($this->_instances[$name]);
52
    }
53
54
    //卸载服务
55 4
    public function remove($name)
56
    {
57 4
        unset($this->_bindings[$name],$this->_instances[$name]);
58 4
    }
59
60
    //设置服务
61 3
    public function set($name, $class)
62
    {
63 3
        $this->_registerService($name, $class);
64 3
    }
65
66
    //设置共享服务
67 1
    public function setShared($name, $class)
68
    {
69 1
        $this->_registerService($name, $class, true);
70 1
    }
71
72
    //注册服务
73 4
    private function _registerService($name, $class, $shared=false)
74
    {
75 4
        $this->remove($name);
76 4
        if (!($class instanceof \Closure) && is_object($class)) {
77 1
            $this->_instances[$name]=$class;
78 1
        } else {
79 3
            $this->_bindings[$name]=array("class"=>$class,"shared"=>$shared);
80
        }
81 4
    }
82
83
    //ArrayAccess接口,检测服务是否存在
84 1
    public function offsetExists($offset)
85
    {
86 1
        return $this->has($offset);
87
    }
88
89
    //ArrayAccess接口,以$di[$name]方式获取服务
90 1
    public function offsetGet($offset)
91
    {
92 1
        return $this->get($offset);
93
    }
94
95
    //ArrayAccess接口,以$di[$name]方式获取服务
96 1
    public function offsetSet($offset, $value)
97
    {
98 1
        return $this->set($offset, $value);
99
    }
100
101
    //卸载服务
102 1
    public function offsetUnset($offset)
103
    {
104 1
        return $this->remove($offset);
105
    }
106
}
107