-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
ClassDefinition.php
273 lines (241 loc) · 6.48 KB
/
ClassDefinition.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
<?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\Definition;
use DI\Definition\ClassInjection\MethodInjection;
use DI\Definition\ClassInjection\PropertyInjection;
use DI\Definition\Exception\DefinitionException;
use DI\Scope;
/**
* Definition of a class for dependency injection
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class ClassDefinition implements MergeableDefinition
{
/**
* Entry name (most of the time, same as $classname)
* @var string
*/
private $name;
/**
* Class name (if null, then the class name is $name)
* @var string|null
*/
private $className;
/**
* Constructor injection
* @var MethodInjection|null
*/
private $constructorInjection;
/**
* Property injections
* @var PropertyInjection[]
*/
private $propertyInjections = array();
/**
* Method injections indexed by the method name
* @var MethodInjection[]
*/
private $methodInjections = array();
/**
* @var Scope|null
*/
private $scope;
/**
* @var boolean|null
*/
private $lazy;
/**
* @param string $name Class name
* @param string $className
*/
public function __construct($name, $className = null)
{
$this->name = (string) $name;
$this->className = $className;
}
/**
* @return string Entry name
*/
public function getName()
{
return $this->name;
}
/**
* @param string $className
*/
public function setClassName($className)
{
$this->className = $className;
}
/**
* @return string Class name
*/
public function getClassName()
{
if ($this->className !== null) {
return $this->className;
}
return $this->name;
}
/**
* @return bool True if the definition is an alias to another class
*/
public function isAlias()
{
return $this->className !== null && $this->className !== $this->name;
}
/**
* @return MethodInjection|null
*/
public function getConstructorInjection()
{
return $this->constructorInjection;
}
/**
* @param MethodInjection|null $constructorInjection
*/
public function setConstructorInjection(MethodInjection $constructorInjection = null)
{
$this->constructorInjection = $constructorInjection;
}
/**
* @return PropertyInjection[] Property injections
*/
public function getPropertyInjections()
{
return $this->propertyInjections;
}
/**
* @param string $propertyName
* @return PropertyInjection
*/
public function getPropertyInjection($propertyName)
{
return isset($this->propertyInjections[$propertyName]) ? $this->propertyInjections[$propertyName] : null;
}
/**
* @param PropertyInjection $propertyInjection
*/
public function addPropertyInjection(PropertyInjection $propertyInjection)
{
$this->propertyInjections[$propertyInjection->getPropertyName()] = $propertyInjection;
}
/**
* @return MethodInjection[] Method injections
*/
public function getMethodInjections()
{
return $this->methodInjections;
}
/**
* @param string $methodName
* @return MethodInjection|null
*/
public function getMethodInjection($methodName)
{
return isset($this->methodInjections[$methodName]) ? $this->methodInjections[$methodName] : null;
}
/**
* @param MethodInjection $methodInjection
*/
public function addMethodInjection(MethodInjection $methodInjection)
{
$this->methodInjections[$methodInjection->getMethodName()] = $methodInjection;
}
/**
* @param Scope $scope
*/
public function setScope(Scope $scope)
{
$this->scope = $scope;
}
/**
* @return Scope
*/
public function getScope()
{
return $this->scope ?: Scope::SINGLETON();
}
/**
* @param boolean|null $lazy
*/
public function setLazy($lazy)
{
$this->lazy = $lazy;
}
/**
* @return bool
*/
public function isLazy()
{
if ($this->lazy !== null) {
return $this->lazy;
} else {
// Default value
return false;
}
}
/**
* {@inheritdoc}
*/
public function merge(MergeableDefinition $definition)
{
if (!$definition instanceof ClassDefinition) {
throw new DefinitionException(
"DI definition conflict: there are 2 different definitions for '" . $this->getName()
. "' that are incompatible, they are not of the same type"
);
}
// The current prevails
if ($this->className === null) {
$this->className = $definition->className;
}
if ($this->scope === null) {
$this->scope = $definition->scope;
}
if ($this->lazy === null) {
$this->lazy = $definition->lazy;
}
// Merge constructor injection
if ($definition->getConstructorInjection() !== null) {
if ($this->constructorInjection !== null) {
// Merge
$this->constructorInjection->merge($definition->getConstructorInjection());
} else {
// Set
$this->constructorInjection = $definition->getConstructorInjection();
}
}
// Merge property injections
foreach ($definition->getPropertyInjections() as $propertyName => $propertyInjection) {
if (! array_key_exists($propertyName, $this->propertyInjections)) {
// Add
$this->propertyInjections[$propertyName] = $propertyInjection;
}
}
// Merge method injections
foreach ($definition->getMethodInjections() as $methodName => $methodInjection) {
if (array_key_exists($methodName, $this->methodInjections)) {
// Merge
$this->methodInjections[$methodName]->merge($methodInjection);
} else {
// Add
$this->methodInjections[$methodName] = $methodInjection;
}
}
}
/**
* {@inheritdoc}
*/
public function isCacheable()
{
return true;
}
}