This repository has been archived by the owner on Nov 7, 2021. It is now read-only.
/
Container.php
202 lines (179 loc) · 5.77 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
<?php
namespace Yuloh\Container;
use Interop\Container\ContainerInterface;
use ReflectionClass;
use ReflectionParameter;
class Container implements ContainerInterface, \ArrayAccess
{
/**
* @var []
*/
private $definitions = [];
/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws \Interop\Container\Exception\NotFoundException No entry was found for this identifier.
* @throws \Interop\Container\Exception\ContainerException Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get($id)
{
// If we have a binding for it, then it's a closure.
// We can just invoke it and return the resolved instance.
if ($this->has($id)) {
return $this->definitions[$id]($this);
}
// Otherwise we are going to try and use reflection to "autowire"
// the dependencies and instantiate this entry if it's a class.
if (!class_exists($id)) {
throw NotFoundException::create($id);
}
$reflector = new ReflectionClass($id);
// If the reflector is not instantiable, it's probably an interface.
// In that case the user should register a factory, since we can't possibly know what
// concrete class they want. It could also be an abstract class, which we can't build either.
if (!$reflector->isInstantiable()) {
throw NotFoundException::create($id);
}
/** @var \ReflectionMethod|null */
$constructor = $reflector->getConstructor();
// If there isn't a constructor, there aren't any dependencies.
// We can just instantiate the class and return it without doing anything.
if (is_null($constructor)) {
return new $id();
}
// Otherwise we need to go through and recursively build all of the dependencies.
$dependencies = $constructor->getParameters();
$dependencies = array_map(function (ReflectionParameter $dependency) use ($id) {
if (is_null($dependency->getClass())) {
throw NotFoundException::create($id);
}
return $this->get($dependency->getClass()->name);
}, $dependencies);
return $reflector->newInstanceArgs($dependencies);
}
/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* @param string $id Identifier of the entry to look for.
*
* @return boolean
*/
public function has($id)
{
return array_key_exists($id, $this->definitions);
}
/**
* Adds an entry to the container.
*
* @param string $id Identifier of the entry.
* @param \Closure $value The closure to invoke when this entry is resolved.
* The closure will be given this container as the only
* argument when invoked.
*/
public function set($id, \Closure $value)
{
$this->definitions[$id] = $value;
}
/**
* Adds a shared (singleton) entry to the container.
*
* @param string $id Identifier of the entry.
* @param \Closure $value The closure to invoke when this entry is resolved.
* The closure will be given this container as the only
* argument when invoked.
*/
public function share($id, \Closure $value)
{
$this->definitions[$id] = function ($container) use ($value) {
static $object;
if (is_null($object)) {
$object = $value($container);
}
return $object;
};
}
/**
* Removes an entry from the container.
*
* @param string $id Identifier of the entry to remove.
*
* @return void
*/
public function remove($id)
{
if ($this->has($id)) {
unset($this->definitions[$id]);
}
}
/**
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
*
* @param mixed $offset <p>
* An offset to check for.
* </p>
*
* @return boolean true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
* @since 5.0.0
*/
public function offsetExists($offset)
{
return $this->has($offset);
}
/**
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
*
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
*
* @return mixed Can return all value types.
* @since 5.0.0
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Offset to set
* @link http://php.net/manual/en/arrayaccess.offsetset.php
*
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
*
* @return void
* @since 5.0.0
*/
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
*
* @param mixed $offset <p>
* The offset to unset.
* </p>
*
* @return void
* @since 5.0.0
*/
public function offsetUnset($offset)
{
$this->remove($offset);
}
}