|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magium\Configuration\Container; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
|
|
7
|
|
|
class GenericContainer implements ContainerInterface |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
protected $container = []; |
|
11
|
|
|
|
|
12
|
18 |
|
public function __construct(array $defaults = []) |
|
13
|
|
|
{ |
|
14
|
18 |
|
$this->container = $defaults; |
|
15
|
18 |
|
$this->set($this); |
|
16
|
18 |
|
} |
|
17
|
|
|
|
|
18
|
18 |
|
public function set($value) |
|
19
|
|
|
{ |
|
20
|
18 |
|
if (!is_object($value)) { |
|
21
|
1 |
|
throw new InvalidObjectException('The GenericContainer can only accept objects'); |
|
22
|
|
|
} |
|
23
|
18 |
|
$class = new \ReflectionClass($value); |
|
24
|
18 |
|
$interfaces = $class->getInterfaces(); |
|
25
|
18 |
|
foreach ($interfaces as $interface) { |
|
26
|
18 |
|
if (!$interface->isInternal()) { |
|
27
|
18 |
|
$this->container[$interface->getName()] = $value; |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
do { |
|
31
|
18 |
|
if ($class->isInternal()) { |
|
32
|
6 |
|
return; // Our work is done here. |
|
33
|
|
|
} |
|
34
|
18 |
|
$this->container[$class->getName()] = $value; |
|
|
|
|
|
|
35
|
18 |
|
} while (($class = $class->getParentClass()) instanceof \ReflectionClass); |
|
36
|
18 |
|
} |
|
37
|
|
|
|
|
38
|
14 |
|
public function get($id) |
|
39
|
|
|
{ |
|
40
|
14 |
|
if (!isset($this->container[$id])) { |
|
41
|
13 |
|
$this->set($this->newInstance($id)); |
|
42
|
|
|
} |
|
43
|
11 |
|
return $this->container[$id]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
13 |
|
public function newInstance($type) |
|
47
|
|
|
{ |
|
48
|
13 |
|
$reflection = new \ReflectionClass($type); |
|
49
|
12 |
|
$constructor = $reflection->getConstructor(); |
|
50
|
12 |
|
$constructorParams = $this->getParams($constructor); |
|
51
|
10 |
|
if ($constructorParams) { |
|
|
|
|
|
|
52
|
4 |
|
$requestedInstance = $reflection->newInstanceArgs($constructorParams); |
|
53
|
|
|
} else { |
|
54
|
10 |
|
$requestedInstance = $reflection->newInstance(); |
|
55
|
|
|
} |
|
56
|
10 |
|
return $requestedInstance; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
12 |
|
protected function getParams(\ReflectionMethod $method = null) |
|
60
|
|
|
{ |
|
61
|
12 |
|
if (!$method instanceof \ReflectionMethod) { |
|
62
|
9 |
|
return []; |
|
63
|
|
|
} |
|
64
|
7 |
|
$constructorParams = []; |
|
65
|
7 |
|
$params = $method->getParameters(); |
|
66
|
7 |
|
foreach ($params as $param) { |
|
67
|
7 |
|
if ($param->getClass() instanceof \ReflectionClass) { |
|
68
|
4 |
|
$class = $param->getClass()->getName(); |
|
69
|
4 |
|
$instance = $this->get($class); |
|
70
|
4 |
|
$constructorParams[] = $instance; |
|
71
|
3 |
|
} else if (!$param->isOptional()) { |
|
72
|
2 |
|
throw new InvalidObjectException( |
|
73
|
2 |
|
'The generic container will only manage constructor arguments that are objects' |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
5 |
|
return $constructorParams; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
public function has($id) |
|
81
|
|
|
{ |
|
82
|
3 |
|
return isset($this->container[$id]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|