1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CapMousse\ReactRestify\Container; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionMethod; |
7
|
|
|
use ReflectionFunction; |
8
|
|
|
use ReflectionFunctionAbstract; |
9
|
|
|
|
10
|
|
|
class Container |
11
|
|
|
{ |
12
|
|
|
private static $instance; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Resolved types |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $definitions = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Globally available container |
22
|
|
|
* |
23
|
|
|
* @return static |
24
|
|
|
*/ |
25
|
|
|
public static function getInstance() |
26
|
|
|
{ |
27
|
|
|
if (is_null(self::$instance)) { |
28
|
|
|
static::$instance = new static; |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return static::$instance; |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Add item to the container |
36
|
|
|
* @param string $alias |
37
|
|
|
* @param mixed|null $concrete |
38
|
|
|
*/ |
39
|
|
|
public function add($alias, $concrete = null) |
40
|
|
|
{ |
41
|
|
|
if (is_null($concrete)) { |
42
|
|
|
$concrete = $alias; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$instance = $this->build($concrete); |
46
|
|
|
|
47
|
|
|
if (!$instance) { |
48
|
|
|
$this->definitions[$alias] = $instance; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check if item is available in container |
54
|
|
|
* @param string $alias |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
|
|
public function has($alias) |
58
|
|
|
{ |
59
|
|
|
if (array_key_exists($alias, $this->definitions)) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get an item of the container |
68
|
|
|
* @param string $alias |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
public function get($alias) |
72
|
|
|
{ |
73
|
|
|
return $this->definitions[$alias]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Call method with given parameters |
78
|
|
|
* @param string|callable $action |
79
|
|
|
* @param array $args |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function call ($action, array $args = []) |
83
|
|
|
{ |
84
|
|
|
if (is_string($action)) { |
85
|
|
|
list($class, $method) = explode('@', $action); |
86
|
|
|
$action = [$this->build($class), $method]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$reflection = $this->getActionReflection($action); |
90
|
|
|
|
91
|
|
|
return call_user_func_array($action, $this->getParameters($reflection, $args)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get reflection from an action |
96
|
|
|
* @param array|callable $action |
97
|
|
|
* @return ReflectionFunctionAbstract |
98
|
|
|
*/ |
99
|
|
|
public function getActionReflection($action) |
100
|
|
|
{ |
101
|
|
|
if(is_array($action)) { |
102
|
|
|
list($class, $method) = $action; |
103
|
|
|
return new ReflectionMethod($class, $method); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return new ReflectionFunction($action); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Create a new container for asked class |
111
|
|
|
* @param string $concrete |
|
|
|
|
112
|
|
|
* @return Container |
113
|
|
|
*/ |
114
|
|
|
public function build ($class) |
115
|
|
|
{ |
116
|
|
|
$reflection = new ReflectionClass($class); |
117
|
|
|
$parameters = []; |
118
|
|
|
|
119
|
|
|
if (!is_null($reflection->getConstructor())) { |
120
|
|
|
$parameters = $this->getParameters($reflection->getConstructor()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return new $class(...$parameters); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get reflection parameters |
128
|
|
|
* @param ReflectionFunctionAbstract $reflection |
129
|
|
|
* @param array $args |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getParameters(ReflectionFunctionAbstract $reflection, $args = []) |
133
|
|
|
{ |
134
|
|
|
$dependencies = $reflection->getParameters(); |
135
|
|
|
$parameters = []; |
136
|
|
|
|
137
|
|
|
foreach ($dependencies as $parameter) { |
138
|
|
|
$class = $parameter->getClass(); |
139
|
|
|
|
140
|
|
|
if ($class && $this->has($class->getName())) { |
141
|
|
|
$parameters[] = $this->get($class->getName()); |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($class && array_key_exists($class->getName(), $args)) { |
146
|
|
|
$parameters[] = $args[$class->getName()]; |
147
|
|
|
continue; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (array_key_exists($parameter->getName(), $args)) { |
|
|
|
|
151
|
|
|
$parameters[] = $args[$parameter->getName()]; |
|
|
|
|
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $parameters; |
156
|
|
|
} |
157
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: