forked from ray-di/Ray.Di
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Injector.php
330 lines (286 loc) · 7.73 KB
/
Injector.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* This file is part of the Ray package.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*/
namespace Ray\Di;
use Aura\Di\ContainerInterface;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Ray\Aop\BindInterface;
use Ray\Aop\Compiler;
use Ray\Aop\CompilerInterface;
use Ray\Aop\Matcher;
use ReflectionClass;
use SplObjectStorage;
use Serializable;
use Ray\Di\Di\Inject;
class Injector implements InjectorInterface, \Serializable
{
/**
* Container
*
* @var \Ray\Di\Container
*/
protected $container;
/**
* Binding module
*
* @var AbstractModule
*/
protected $module;
/**
* @var BindInterface
*/
protected $bind;
/**
* Pre-destroy objects
*
* @var SplObjectStorage
*/
private $preDestroyObjects;
/**
* Logger
*
* @var LoggerInterface
*/
private $logger;
/**
* Compiler(Aspect Weaver)
*
* @var Compiler
*/
private $compiler;
/**
* @var BoundInstance
*/
public $boundInstance;
/**
* @param ContainerInterface $container
* @param AbstractModule $module
* @param BindInterface $bind
* @param CompilerInterface $compiler
* @param LoggerInterface $logger
* @param BoundInstanceInterface $boundInstance
*
* @Inject
*/
public function __construct(
ContainerInterface $container,
AbstractModule $module,
BindInterface $bind,
CompilerInterface $compiler,
LoggerInterface $logger = null,
BoundInstanceInterface $boundInstance = null
) {
$this->container = $container;
$this->module = $module;
$this->bind = $bind;
$this->compiler = $compiler;
$this->logger = $logger;
$this->preDestroyObjects = new SplObjectStorage;
$this->boundInstance = $boundInstance ?: new BoundInstance($this, $container, $module, $logger);
AnnotationRegistry::registerFile(__DIR__ . '/DiAnnotation.php');
}
/**
* Notify pre-destroy
*/
public function __destruct()
{
$this->preDestroyObjects->rewind();
while ($this->preDestroyObjects->valid()) {
$object = $this->preDestroyObjects->current();
$method = $this->preDestroyObjects->getInfo();
$object->$method();
$this->preDestroyObjects->next();
}
}
/**
* {@inheritdoc}
*/
public static function create(array $modules = [], Cache $cache = null, $tmdDir = null)
{
$locator = new Locator;
$cache = $cache ?: new ArrayCache;
$locator->setCache($cache);
$tmdDir = $tmdDir ?: sys_get_temp_dir();
$annotationReader = $locator->getAnnotationReader();
Matcher::setAnnotationReader($annotationReader);
$injector = (new InjectorFactory)->newInstance($modules, $cache, $tmdDir);
return $injector;
}
/**
* {@inheritdoc}
*/
public function getModule()
{
return $this->module;
}
/**
* {@inheritdoc}
*/
public function setModule(AbstractModule $module)
{
$module->activate($this);
$this->module = $module;
return $this;
}
/**
* {@inheritdoc}
*/
public function getContainer()
{
return $this->container;
}
/**
* {@inheritdoc}
*/
public function getLogger()
{
return $this->logger;
}
/**
* {@inheritdoc}
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Return aop generated file path
*
* @return string
*/
public function getAopClassDir()
{
return $this->compiler->getAopClassDir();
}
public function __clone()
{
$this->container = clone $this->container;
}
/**
* @param AbstractModule $module
*
* @return self
*/
public function __invoke(AbstractModule $module)
{
$this->module = $module;
return $this;
}
/**
* {@inheritdoc}
*/
public function getInstance($class)
{
// module activation
$this->module->activate($this);
if ($this->boundInstance->hasBound($class, $this->module)) {
return $this->boundInstance->getBound();
}
// get bound config
$definition = $this->boundInstance->getDefinition();
// be all parameters ready
$params = $this->boundInstance->bindConstruct($class, $definition->params, $this->module);
$refClass = new \ReflectionClass($definition->class);
if ($refClass->isInterface()) {
return $this->getInstance($definition->class);
}
// weave aspect
$module = $this->module;
$bind = $module($definition->class, new $this->bind);
/* @var $bind \Ray\Aop\Bind */
$instance = $bind->hasBinding() ? $this->compiler->newInstance($definition->class, $params, $bind) : $refClass->newInstanceArgs($params);
// do not call constructor twice. ever.
unset($definition->setter['__construct']);
// call setter methods
foreach ($definition->setter as $method => $value) {
call_user_func_array([$instance, $method], $value);
}
// logger inject info
if ($this->logger) {
$this->logger->log($definition->class, $params, $definition->setter, $instance, $bind);
}
// object life cycle, store singleton instance.
$this->postInject($instance, $definition);
return $instance;
}
/**
* Post inject procedure
*
* @param object $object
* @param Definition $definition
*/
private function postInject($object, BoundDefinition $definition)
{
// set life cycle
if ($definition) {
$this->setLifeCycle($object, $definition);
}
// set singleton object
if ($definition->isSingleton) {
$this->container->set($definition->interface, $object);
}
}
/**
* Set object life cycle
*
* @param object $instance
* @param BoundDefinition $definition
*/
private function setLifeCycle($instance, BoundDefinition $definition)
{
$postConstructMethod = $definition->postConstruct;
if ($postConstructMethod) {
call_user_func(array($instance, $postConstructMethod));
}
if (!is_null($definition->preDestroy)) {
$this->preDestroyObjects->attach($instance, $definition->preDestroy);
}
}
/**
* Return module information as string
*
* @return string
*/
public function __toString()
{
return (string) ($this->module);
}
public function serialize()
{
$data = serialize(
[
$this->container,
$this->module,
$this->bind,
$this->compiler,
$this->logger,
$this->preDestroyObjects,
$this->boundInstance
]
);
return $data;
}
public function unserialize($data)
{
list(
$this->container,
$this->module,
$this->bind,
$this->compiler,
$this->logger,
$this->preDestroyObjects,
$this->boundInstance
) = unserialize($data);
AnnotationRegistry::registerFile(__DIR__ . '/DiAnnotation.php');
register_shutdown_function(function () {
// @codeCoverageIgnoreStart
$this->__destruct();
// @codeCoverageIgnoreEnd
});
}
}