-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
Factory.php
225 lines (193 loc) · 7.78 KB
/
Factory.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
<?php
/**
* PHP-DI
*
* @link http://mnapoli.github.io/PHP-DI/
* @copyright Matthieu Napoli (http://mnapoli.fr/)
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
*/
namespace DI;
use DI\Definition\ClassDefinition;
use DI\Definition\Exception\DefinitionException;
use DI\Definition\MethodInjection;
use DI\Definition\PropertyInjection;
use Exception;
use ReflectionClass;
use ReflectionProperty;
/**
* Factory class, responsible of instantiating classes
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class Factory implements FactoryInterface
{
/**
* @var Container
*/
protected $container;
/**
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* {@inheritdoc}
* @throws DependencyException
* @throws DefinitionException
*/
public function createInstance(ClassDefinition $classDefinition)
{
$classReflection = new ReflectionClass($classDefinition->getClassName());
if (!$classReflection->isInstantiable()) {
throw new DependencyException("$classReflection->name is not instantiable");
}
try {
$instance = $this->injectConstructor($classReflection, $classDefinition->getConstructorInjection());
$this->injectMethodsAndProperties($instance, $classDefinition);
} catch (NotFoundException $e) {
throw new DependencyException("Error while injecting dependencies into $classReflection->name: " . $e->getMessage(), 0, $e);
}
return $instance;
}
/**
* {@inheritdoc}
* @throws DependencyException
* @throws DefinitionException
*/
public function injectInstance(ClassDefinition $classDefinition, $instance)
{
try {
$this->injectMethodsAndProperties($instance, $classDefinition);
} catch (NotFoundException $e) {
throw new DependencyException("Error while injecting dependencies into " . $classDefinition->getClassName() . ": " . $e->getMessage(), 0, $e);
}
return $instance;
}
/**
* Creates an instance and inject dependencies through the constructor
*
* @param ReflectionClass $classReflection
* @param MethodInjection|null $constructorInjection
*
* @throws DefinitionException
* @return object
*/
private function injectConstructor(ReflectionClass $classReflection, MethodInjection $constructorInjection = null)
{
$constructorReflection = $classReflection->getConstructor();
// No constructor
if (!$constructorReflection) {
return $classReflection->newInstance();
}
// Check the definition and the class parameter number match
$nbRequiredParameters = $constructorReflection->getNumberOfRequiredParameters();
$parameterInjections = $constructorInjection ? $constructorInjection->getParameterInjections() : array();
if (count($parameterInjections) < $nbRequiredParameters) {
throw new DefinitionException("The constructor of {$classReflection->name} takes "
. "$nbRequiredParameters parameters, " . count($parameterInjections) . " defined or guessed");
}
if (count($parameterInjections) === 0) {
return $classReflection->newInstance();
}
$args = array();
foreach ($parameterInjections as $parameterInjection) {
$entryName = $parameterInjection->getEntryName();
if ($entryName === null) {
throw new DefinitionException("The parameter '" . $parameterInjection->getParameterName()
. "' of the constructor of '{$classReflection->name}' has no type defined or guessable");
}
// TODO handle lazy injections!
$args[] = $this->container->get($entryName);
}
return $classReflection->newInstanceArgs($args);
}
/**
* @param object $instance
* @param ClassDefinition $classDefinition
*/
private function injectMethodsAndProperties($instance, ClassDefinition $classDefinition)
{
// Property injections
foreach ($classDefinition->getPropertyInjections() as $propertyInjection) {
$this->injectProperty($instance, $propertyInjection);
}
// Method injections
foreach ($classDefinition->getMethodInjections() as $methodInjection) {
$this->injectMethod($instance, $methodInjection);
}
}
/**
* Inject dependencies through methods
*
* @param object $object Object to inject dependencies into
* @param MethodInjection $methodInjection
*
* @throws DependencyException
* @throws DefinitionException
*/
private function injectMethod($object, MethodInjection $methodInjection)
{
$methodName = $methodInjection->getMethodName();
$classReflection = new ReflectionClass($object);
$methodReflection = $classReflection->getMethod($methodName);
// Check the definition and the class parameter number match
$nbRequiredParameters = $methodReflection->getNumberOfRequiredParameters();
$parameterInjections = $methodInjection ? $methodInjection->getParameterInjections() : array();
if (count($parameterInjections) < $nbRequiredParameters) {
throw new DefinitionException("{$classReflection->name}::$methodName takes "
. "$nbRequiredParameters parameters, " . count($parameterInjections) . " defined or guessed");
}
// No parameters
if (count($parameterInjections) === 0) {
$methodReflection->invoke($object);
return;
}
$args = array();
foreach ($parameterInjections as $parameterInjection) {
$entryName = $parameterInjection->getEntryName();
if ($entryName === null) {
throw new DefinitionException("The parameter '" . $parameterInjection->getParameterName()
. "' of {$classReflection->name}::$methodName has no type defined or guessable");
}
if ($parameterInjection->isLazy()) {
$args[] = $this->container->get($entryName, true);
} else {
$args[] = $this->container->get($entryName);
}
}
$methodReflection->invokeArgs($object, $args);
}
/**
* Inject dependencies into properties
*
* @param object $object Object to inject dependencies into
* @param PropertyInjection $propertyInjection Property injection definition
*
* @throws DependencyException
* @throws DefinitionException
*/
private function injectProperty($object, PropertyInjection $propertyInjection)
{
$propertyName = $propertyInjection->getPropertyName();
$property = new ReflectionProperty(get_class($object), $propertyName);
$entryName = $propertyInjection->getEntryName();
if ($entryName === null) {
throw new DefinitionException(get_class($object) . "::$propertyName has no type defined or guessable");
}
// Get the dependency
try {
$value = $this->container->get($propertyInjection->getEntryName(), $propertyInjection->isLazy());
} catch (DependencyException $e) {
throw $e;
} catch (Exception $e) {
throw new DependencyException("Error while injecting '" . $propertyInjection->getEntryName() . "' in "
. get_class($object) . "::$propertyName. " . $e->getMessage(), 0, $e);
}
// Allow access to protected and private properties
$property->setAccessible(true);
// Inject the dependency
$property->setValue($object, $value);
}
}