1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CapMousse\ReactRestify\Container; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
use CapMousse\ReactRestify\Errors\Container\ContainerException; |
7
|
|
|
use CapMousse\ReactRestify\Errors\Container\NotFoundException; |
8
|
|
|
|
9
|
|
|
class Container implements ContainerInterface |
10
|
|
|
{ |
11
|
|
|
private static $instance; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Resolved types |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $definitions = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Globally available container |
21
|
|
|
* |
22
|
|
|
* @return static |
23
|
|
|
*/ |
24
|
|
|
public static function getInstance() |
25
|
|
|
{ |
26
|
|
|
if (is_null(self::$instance)) { |
27
|
|
|
self::$instance = new static; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return self::$instance; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Add item to the container |
35
|
|
|
* @param string $id |
36
|
|
|
* @param mixed|null $concrete |
37
|
|
|
*/ |
38
|
|
|
public function add($id, $concrete = null) |
39
|
|
|
{ |
40
|
|
|
if (!is_string($id)) { |
41
|
|
|
throw new ContainerException('$id must be a string'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (isset($this->definitions[$id])) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (is_null($concrete)) { |
49
|
|
|
$concrete = $id; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->definitions[$id] = $this->build($concrete); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check if item is available in container |
57
|
|
|
* @param string $id |
58
|
|
|
* @return boolean |
59
|
|
|
*/ |
60
|
|
|
public function has($id) |
61
|
|
|
{ |
62
|
|
|
if (!is_string($id)) { |
63
|
|
|
throw new ContainerException('$id must be a string'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (array_key_exists($id, $this->definitions)) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get an item of the container |
75
|
|
|
* @param string $id |
76
|
|
|
* @return mixed |
77
|
|
|
* @throw NotFoundException |
78
|
|
|
*/ |
79
|
|
|
public function get($id) |
80
|
|
|
{ |
81
|
|
|
if (!is_string($id)) { |
82
|
|
|
throw new ContainerException('$id must be a string'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!isset($this->definitions[$id])) { |
86
|
|
|
throw new NotFoundException("Unresolvable ".$id); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->definitions[$id]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Call method with given parameters |
94
|
|
|
* @param string|array|callable $action |
95
|
|
|
* @param array $args |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public function call ($action, array $args = []) |
99
|
|
|
{ |
100
|
|
|
$method = $action; |
101
|
|
|
$class = null; |
102
|
|
|
|
103
|
|
|
if (is_string($action)) { |
104
|
|
|
list($class, $method) = explode('@', $action); |
105
|
|
|
} |
106
|
|
|
elseif (is_array($action)) { |
107
|
|
|
list($class, $method) = $action; |
108
|
|
|
|
109
|
|
|
if (!is_object($class)) { |
110
|
|
|
throw new ContainerException('$class must be an object'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (!is_string($method)) { |
114
|
|
|
throw new ContainerException('$action must be a string'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$reflection = $this->getActionReflection($method, $class); |
119
|
|
|
$args = $this->getParametersDictionary($args); |
120
|
|
|
$parameters = $this->getParameters($reflection, $args); |
121
|
|
|
|
122
|
|
|
if (is_callable($method)) { |
123
|
|
|
return $method(...$parameters); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if(!is_object($class)) { |
127
|
|
|
$class = $this->build($class); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $class->{$method}(...$parameters); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get reflection from an action |
135
|
|
|
* @param string $method |
136
|
|
|
* @param string|null $class |
137
|
|
|
* @return callable |
138
|
|
|
*/ |
139
|
|
|
public function getActionReflection($method, $class = null) |
140
|
|
|
{ |
141
|
|
|
if(!is_null($class)) { |
142
|
|
|
return new \ReflectionMethod($class, $method); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return new \ReflectionFunction($method); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Find object and set classname alias on argument list |
150
|
|
|
* @param array $args |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function getParametersDictionary(array $args = []) |
154
|
|
|
{ |
155
|
|
|
$dictionary = []; |
156
|
|
|
|
157
|
|
|
foreach ($args as $parameter) { |
158
|
|
|
if (!is_object($parameter)) continue; |
159
|
|
|
$dictionary[get_class($parameter)] = $parameter; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return array_merge($args, $dictionary); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get reflection parameters |
167
|
|
|
* @param \ReflectionFunctionAbstract $reflection |
168
|
|
|
* @param array $args |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
public function getParameters(\ReflectionFunctionAbstract $reflection, array $args = []) |
172
|
|
|
{ |
173
|
|
|
$dependencies = $reflection->getParameters(); |
174
|
|
|
$parameters = []; |
175
|
|
|
|
176
|
|
|
foreach ($dependencies as $parameter) { |
177
|
|
|
$parameters[] = $this->getParameter($parameter, $args); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $parameters; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get paremeter value |
185
|
|
|
* @param \ReflectionParameter $parameter |
186
|
|
|
* @param array $args |
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
|
|
public function getParameter(\ReflectionParameter $parameter, array $args = []) |
190
|
|
|
{ |
191
|
|
|
$class = $parameter->getClass(); |
192
|
|
|
|
193
|
|
|
if ($class && $this->has($class->name)) { |
194
|
|
|
return $this->get($class->name); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ($class && array_key_exists($class->name, $args)) { |
198
|
|
|
return $args[$class->name]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (array_key_exists($parameter->name, $args)) { |
202
|
|
|
return $args[$parameter->name]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
throw new NotFoundException("Unresolvable " . ($class ? $class->name : $parameter->name)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Create a new container for asked class |
210
|
|
|
* @param string $class |
211
|
|
|
* @return Container |
212
|
|
|
*/ |
213
|
|
|
public function build ($class) |
214
|
|
|
{ |
215
|
|
|
$reflection = new \ReflectionClass($class); |
216
|
|
|
$parameters = []; |
217
|
|
|
|
218
|
|
|
if (!is_null($reflection->getConstructor())) { |
219
|
|
|
$parameters = $this->getParameters($reflection->getConstructor()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return new $class(...$parameters); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|