/
container.php
360 lines (316 loc) · 8.8 KB
/
container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
include_once(dirname(__FILE__).'/promise.php');
include_once(dirname(__FILE__).'/proto.php');
class Metrodi_Container {
public $thingList = array();
public $thingArgList = array();
public $varList = array();
public $objectCache = array();
public $searchDirs = array('src', 'local', '.');
public $rootDir = '';
static $container = NULL;
public function __construct($root='.', $dirs=NULL) {
// register shutdown functions execute in a different directory.
// we need to set include path. for exception lifecycle
// root/vendor/metrophp/metrodi/container.php
set_include_path(
get_include_path().':'.realpath($root)
);
$this->rootDir = $root;
if (!empty($dirs)) {
$this->searchDirs = (array)$dirs;
}
self::$container = $this;
}
static public function &getContainer($root='.', $dirs=NULL) {
if (self::$container == NULL) {
self::$container = new Metrodi_Container($root, $dirs);
}
return self::$container;
}
/**
* Define a file as a thing.
* Any extra arguments are saved and used as constructor arguments
*/
public function didef($thing, $file) {
$this->thingList[$thing] = $file;
$args = func_get_args();
//remove 2 known params
array_shift($args);
array_shift($args);
if (count($args)) {
$this->thingArgList[$thing] = $args;
}
}
public function isdidef($thing) {
return array_key_exists($thing, $this->thingList);
}
/**
* Return a Metrodi_Promise, which returns
* the desired instance when __invoked()
*/
public function makePromise($thing) {
$args = func_get_args();
array_shift($args);
return new Metrodi_Promise($thing, $args);
}
/**
* Return a defined thing or an empty object (Metrodi_Proto)
* @return object defined thing or empty object (Metrodi_Proto)
*/
public function & make($thing, $singleton=TRUE) {
if (!isset($this->thingList[$thing])) {
$this->thingList[$thing] = $thing;
}
//closures and anon funcs are objects of type/class Closure
if (is_object($this->thingList[$thing]) && !is_callable($this->thingList[$thing])) {
return $this->thingList[$thing];
}
$args = func_get_args();
array_shift($args);
array_shift($args);
if (!count($args) && isset($this->thingArgList[$thing])) {
$args = $this->thingArgList[$thing];
}
if (!count($args)) {
$args = NULL;
}
if ($singleton) {
if (!@count($args)) {
$args = NULL;
$cachekey = $thing;
} else {
$cachekey = $thing.':'.sha1(serialize($args));
}
if ( $singleton && isset($this->objectCache[$cachekey]) ) {
return $this->objectCache[$cachekey];
}
}
$result = NULL;
if (is_callable($this->thingList[$thing])) {
if ($args === NULL) {
$result = $this->thingList[$thing]->__invoke();
} else {
$result = call_user_func_array( array($this->thingList[$thing], '__invoke'), $args);
}
}
//TODO: refactor into file loading, class loading, and invokables
//this should be a filename that resolves to a classname
$file = $this->thingList[$thing];
if (!$result) {
if ($singleton) {
$result = $this->loadAndCache($file, $cachekey, $args);
} else {
//always make new object when not $singleton
$result = $this->load($file, $args);
}
}
//we got FALSE from load or loadAndCache
if (!$result) {
$result = new Metrodi_Proto($thing);
}
if ($singleton && !isset($this->objectCache[$cachekey])) {
$this->objectCache[$cachekey] = $result;
}
return $result;
}
/**
* Return a clone (shallow copy) of a defined thing or an empty object (Metrodi_Proto)
* @return object clone of a defined thing or empty object (Metrodi_Proto)
*/
public function makeNew($thing) {
$args = func_get_args();
if (count($args) <= 1) {
return $this->make($thing, FALSE);
} else {
$thing = array_shift($args);
array_unshift($args, FALSE);
array_unshift($args, $thing);
return call_user_func_array(array($this, 'make'), $args);
}
}
/**
* load a file from local/$file or src/$file.
* Save object to $this->objectCache[$cachekey]
*
* @return Boolean True if file was loaded and saved
*/
public function loadAndCache($file, $cachekey, $args=NULL) {
if (isset($this->objectCache[$cachekey])) {
return $this->objectCache[$cachekey];
}
$_x = $this->load($file, $args);
if ($_x !== FALSE) {
$this->objectCache[$cachekey] = $_x;
}
return $_x;
}
/**
* This class tries to load something defined by $file
* If $file is a filename, a className will be generated.
* If $file is a className, it will be created with reflection
* If $file is not a filename, and not a classname, it will be
* loaded as a Metrodi_Proto class.
*/
public function load($file, $args=NULL) {
//try file loading only if it looks like a file.
//if $file is actually a classname (no dots)
//then it will just be returned into $className
$className = $this->tryFileLoading($file);
//$file looked like a file, but couldn't include it
if ($className === FALSE) {
return FALSE;
}
if (is_array($args) && class_exists('ReflectionClass', false)) {
try {
$refl = new ReflectionClass($className);
//invoke lazy loading promises
foreach ($args as $_argk => $_argv) {
if (is_object($_argv) && method_exists($_argv, '__invoke')) {
$args[ $_argk ] = $_argv();
}
}
$_x = $refl->newInstanceArgs($args);
} catch (ReflectionException $e) {
if (!class_exists($className)) {
$_x = new Metrodi_Proto($file);
} else {
$_x = $refl->newInstance();
}
}
} else {
if (!class_exists($className)) {
$_x = new Metrodi_Proto($file);
} else {
$_x = new $className;
}
}
$this->attachServices($_x);
return $_x;
}
/**
* If the locator looks like a file (contains a dot)
* then load it and return the filename fromatted as
* a Class_Name.
* Otherwise return the same string because it is
* already a Class_Name or Class\Name
*/
public function tryFileLoading($locator) {
//file is actually a class name or namespace
if (strrpos($locator, '.') === FALSE) {
return $locator;
}
$className = $this->formatClassName($locator);
if (class_exists($className)) {
return $className;
}
$filesep = '/';
$loaded = FALSE;
foreach ($this->searchDirs as $_dir) {
$file = $this->rootDir.$filesep.$_dir.$filesep.$locator;
if(file_exists($file)) {
if(include_once($file)) {
$loaded = TRUE;
break;
}
}
}
if (!$loaded) {
return FALSE;
}
return $className;
}
/**
* Set a DI promise object on every
* class var that ends with 'Service'
*/
public function attachServices($obj) {
$args = get_class_vars( get_class($obj) );
foreach ($args as $_k=>$_v) {
if (substr($_k, -7) == 'Service') {
$obj->$_k = _makePromise($_k);
}
}
}
public function isThingDefined($thing) {
return array_key_exists($thing, $this->thingList);
}
public function set($key, $val) {
$this->varList[$key] = $val;
}
public function get($key, $default=NULL) {
if (!isset($this->varList[$key]))
return $default;
return $this->varList[$key];
}
public function formatClassName($filedidef) {
$filesep = '/';
$className = substr($filedidef, 0, strrpos($filedidef, '.'));
$didefList = explode($filesep, $className);
$className = '';
foreach ($didefList as $_n) {
if ($className) $className .= '_';
$className .= ucfirst($_n);
}
$className = str_replace('-', '', $className);
return $className;
}
}
function _didef($thing, $file) {
$a = Metrodi_Container::getContainer();
$args = func_get_args();
if (count($args) <= 2) {
return $a->didef($thing, $file);
} else {
return call_user_func_array(array($a, 'didef'), $args);
}
}
function _isdidef($thing) {
$a = Metrodi_Container::getContainer();
$args = func_get_args();
if (count($args) <= 1) {
return $a->isdidef($thing);
} else {
return call_user_func_array(array($a, 'isdidef'), $args);
}
}
function _make($thing) {
$a = Metrodi_Container::getContainer();
$args = func_get_args();
if (count($args) <= 1) {
return $a->make($thing);
} else {
$thing = array_shift($args);
array_unshift($args, TRUE);
array_unshift($args, $thing);
return call_user_func_array(array($a, 'make'), $args);
}
return $a->make($thing);
}
function _makeNew($thing) {
$a = Metrodi_Container::getContainer();
$args = func_get_args();
if (count($args) <= 1) {
return $a->makeNew($thing);
} else {
return call_user_func_array(array($a, 'makeNew'), $args);
}
}
function _makePromise($thing) {
$a = Metrodi_Container::getContainer();
$args = func_get_args();
if (count($args) <= 1) {
return $a->makePromise($thing);
} else {
return call_user_func_array(array($a, 'makePromise'), $args);
}
return $a->makePromise($thing);
}
function _set($key, $val) {
$a = Metrodi_Container::getContainer();
return $a->set($key, $val);
}
function _get($key, $def=NULL) {
$a = Metrodi_Container::getContainer();
return $a->get($key, $def);
}