WpaeDi.php
1.44 KB
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
<?php
namespace Wpae\Di;
class WpaeDi
{
private $config;
private $cache;
private $services;
public function __construct($config)
{
$this->config = $config;
$this->services = [
'c' => [
'value' => 10
],
'a' => [
'class' => '\Wpae\ClassA',
'arguments' => [
'@a',
'@b'
]
],
'b' => [
'class' => '\Wpae\ClassB',
'arguments' => [
'@a',
'%c'
]
]
];
}
public function get($name) {
if(isset($this->cache[$name])) {
return $this->cache[$name];
}
if(isset($this->services[$name])) {
$arguments = [];
if(count($this->services[$name]['arguments'])){
foreach($this->services[$name]['arguments'] as $argument) {
$arguments[] = $this->get($argument);
}
} else {
$serviceClass = $this->services[$name]['class'];
$this->cache[$name] = new $serviceClass();
return $this->cache[$name];
}
$r = new \ReflectionClass($this->services[$name]['class']);
$this->cache[$name] = $r->newInstanceArgs($arguments);
}
return $this->cache[$name];
}
}