1 | <?php namespace SimpleDI; |
||
17 | class SimpleDI |
||
18 | { |
||
19 | /** |
||
20 | * Closure array |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $closures = array(); |
||
25 | |||
26 | /** |
||
27 | * Getter list |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $getterList = array(); |
||
32 | |||
33 | /** |
||
34 | * Object storage |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $objectList = array(); |
||
39 | |||
40 | /** |
||
41 | * Processflag to store the dependency objects |
||
42 | * |
||
43 | * @var boolean |
||
44 | */ |
||
45 | protected $getStored = false; |
||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | */ |
||
50 | 9 | public function __construct() |
|
53 | |||
54 | /** |
||
55 | * Sets the closure and the getter mapping |
||
56 | * |
||
57 | * @param string $name Class name |
||
58 | * @param Closure $closure Closure function: Code to create |
||
59 | * the object instance. |
||
60 | * @return SimpleDI |
||
61 | */ |
||
62 | 7 | public function add($name, Closure $closure) |
|
73 | |||
74 | /** |
||
75 | * Removes the closure and getter mapping. |
||
76 | * |
||
77 | * @param string $name Class name |
||
78 | * @return SimpleDI |
||
79 | */ |
||
80 | 3 | public function remove($name) |
|
90 | |||
91 | /** |
||
92 | * Checks if one entry exists |
||
93 | * |
||
94 | * @param string $name Class name |
||
95 | * @return boolean |
||
96 | */ |
||
97 | 5 | public function exists($name) |
|
101 | |||
102 | /** |
||
103 | * Checks if the current container is empty |
||
104 | * |
||
105 | * @return boolean |
||
106 | */ |
||
107 | 1 | public function isEmpty() |
|
111 | |||
112 | /** |
||
113 | * Sets the getStored flag to true. |
||
114 | * The next closure call will first check the local object list. |
||
115 | * If no object is stored the result will be saved in the object list. |
||
116 | * |
||
117 | * @return SimpleDI |
||
118 | */ |
||
119 | 2 | public function getStored() |
|
125 | |||
126 | /** |
||
127 | * Calls the container __call method. |
||
128 | * This methods is the central closure call. |
||
129 | * |
||
130 | * @param string $name Getter function name |
||
131 | * @param array $args Construction arguments |
||
132 | * @return false|mixed |
||
133 | * @throws SimpleDIException |
||
134 | */ |
||
135 | 5 | public function __call($name, $args = array()) |
|
145 | |||
146 | /** |
||
147 | * Executes the stored closure from the container. The method returns the |
||
148 | * closure result or false in the closure execution fails. |
||
149 | * If the "getStored" flag is set, the method checks the object list for a |
||
150 | * exsisting instance. Otherwise the created instance will be stored for |
||
151 | * further use. |
||
152 | * |
||
153 | * @param string $name Class name |
||
154 | * @param array $args Construction arguments |
||
155 | * @return boolean|mixed |
||
156 | */ |
||
157 | 4 | private function callClosure($name, $args) |
|
183 | |||
184 | /** |
||
185 | * Generates a hash to store the specific object |
||
186 | * |
||
187 | * @param string $name Class name |
||
188 | * @param array $args Arguments |
||
189 | * @return string |
||
190 | */ |
||
191 | 4 | private function generateStorageHash($name, $args) { |
|
196 | |||
197 | /** |
||
198 | * Checks if the current requested object is stored in the object list. |
||
199 | * |
||
200 | * @param string $hash Class name |
||
201 | * @return mixed |
||
202 | */ |
||
203 | 2 | private function isStored($hash) |
|
211 | |||
212 | /** |
||
213 | * Resets the stored flag. |
||
214 | * This should only enabled for the next operation! |
||
215 | * |
||
216 | * @return void |
||
217 | */ |
||
218 | 2 | private function resetStored() |
|
222 | |||
223 | /** |
||
224 | * Returns the getter name |
||
225 | * |
||
226 | * @param string $name Class name |
||
227 | * @return string |
||
228 | */ |
||
229 | 7 | private function buildGetterName($name) |
|
233 | |||
234 | /** |
||
235 | * Adds the getter function mapping to the getter list. |
||
236 | * |
||
237 | * @param string $name Class name |
||
238 | * @return void |
||
239 | */ |
||
240 | 7 | private function addGetter($name) |
|
244 | |||
245 | /** |
||
246 | * Removes the getter from the mapping list. |
||
247 | * |
||
248 | * @param string $name Class name |
||
249 | * @return void |
||
250 | */ |
||
251 | 3 | private function removeGetter($name) |
|
259 | |||
260 | /** |
||
261 | * Returns the mapped container method name. |
||
262 | * |
||
263 | * @param string $getter Getter function name |
||
264 | * @return string |
||
265 | * @throws SimpleDIException |
||
266 | */ |
||
267 | 4 | private function dispatchGetter($getter) |
|
275 | } |
||
276 |