1 | <?php |
||
18 | class Container implements ArrayAccess |
||
19 | { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $bindings = []; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $instances = []; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $shares = []; |
||
34 | |||
35 | /** |
||
36 | * @param $name |
||
37 | * @param null $concrete |
||
38 | * @param bool $share |
||
39 | */ |
||
40 | 8 | public function bind($name, $concrete = null, $share = false) |
|
50 | |||
51 | /** |
||
52 | * @param string $name |
||
53 | * |
||
54 | * @throws ReflectionException |
||
55 | * |
||
56 | * @return object |
||
57 | */ |
||
58 | 12 | public function make($name) |
|
72 | |||
73 | /** |
||
74 | * @param $name |
||
75 | * |
||
76 | * @throws ReflectionException |
||
77 | * |
||
78 | * @return mixed|object |
||
79 | */ |
||
80 | 11 | protected function build($name) |
|
81 | { |
||
82 | 11 | $concrete = $this->getConcrete($name); |
|
83 | |||
84 | 11 | if ($concrete instanceof Closure) { |
|
85 | 4 | return $concrete($this); |
|
86 | } |
||
87 | |||
88 | 10 | $reflector = new ReflectionClass($concrete); |
|
89 | |||
90 | 9 | if (!$reflector->isInstantiable()) { |
|
91 | 2 | throw new BindingResolutionException(sprintf('%s is not instantiable', $name)); |
|
92 | } |
||
93 | |||
94 | 8 | $constructor = $reflector->getConstructor(); |
|
95 | |||
96 | 8 | if (!$constructor || !$constructor->getParameters()) { |
|
97 | 8 | return $reflector->newInstance(); |
|
98 | } |
||
99 | |||
100 | 3 | return $reflector->newInstanceArgs($this->getDependencies($constructor->getParameters())); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param ReflectionParameter[] $reflectionParameters |
||
105 | * |
||
106 | * @throws |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | 3 | protected function getDependencies(array $reflectionParameters) |
|
111 | { |
||
112 | 3 | $result = []; |
|
113 | 3 | foreach ($reflectionParameters as $parameter) { |
|
114 | 3 | if (!is_null($parameter->getClass())) { |
|
115 | try { |
||
116 | 3 | $result[] = $this->make($parameter->getClass()->getName()); |
|
117 | 2 | } catch (Exception $exception) { |
|
118 | 2 | if (!$parameter->isOptional()) { |
|
119 | 1 | throw $exception; |
|
120 | } |
||
121 | 2 | $result[] = $parameter->getDefaultValue(); |
|
122 | } |
||
123 | } else { |
||
124 | 1 | if (!$parameter->isDefaultValueAvailable()) { |
|
125 | 1 | throw new BindingResolutionException( |
|
126 | 1 | sprintf( |
|
127 | 1 | 'parameter %s has no default value in %s', |
|
128 | 1 | $parameter->getName(), |
|
129 | 1 | $parameter->getDeclaringClass()->getName() |
|
130 | ) |
||
131 | ); |
||
132 | } |
||
133 | 1 | $result[] = $parameter->getDefaultValue(); |
|
134 | } |
||
135 | } |
||
136 | |||
137 | 2 | return $result; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param $name |
||
142 | * |
||
143 | * @return string|Closure |
||
144 | */ |
||
145 | 11 | protected function getConcrete($name) |
|
157 | |||
158 | /** |
||
159 | * @param mixed $offset |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | 2 | public function offsetExists($offset) |
|
167 | |||
168 | /** |
||
169 | * @param mixed $offset |
||
170 | * |
||
171 | * @throws ReflectionException |
||
172 | * |
||
173 | * @return mixed|object |
||
174 | */ |
||
175 | 1 | public function offsetGet($offset) |
|
179 | |||
180 | /** |
||
181 | * @param mixed $offset |
||
182 | * @param mixed $value |
||
183 | */ |
||
184 | 2 | public function offsetSet($offset, $value) |
|
188 | |||
189 | /** |
||
190 | * @param mixed $offset |
||
191 | */ |
||
192 | 1 | public function offsetUnset($offset) |
|
196 | } |
||
197 |