|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/di package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Di\Definition; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Common\Inspector; |
|
13
|
|
|
use Slick\Di\Definition\Resolver\Object as Resolver; |
|
14
|
|
|
use Slick\Di\Definition\Resolver\ObjectResolver; |
|
15
|
|
|
use Slick\Di\Exception\InvalidArgumentException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Object definition class |
|
19
|
|
|
* |
|
20
|
|
|
* @package Slick\Di\Definition |
|
21
|
|
|
* @author Filipe Silva <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* @property string $name Definition name or key |
|
24
|
|
|
* @property string $className |
|
25
|
|
|
* @property object $instance |
|
26
|
|
|
* @property array $constructArgs |
|
27
|
|
|
* @property array $properties |
|
28
|
|
|
* @property array $methods |
|
29
|
|
|
*/ |
|
30
|
|
|
class Object extends AbstractDefinition implements ObjectDefinitionInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @readwrite |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $className; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @readwrite |
|
40
|
|
|
* @var object |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $instance; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @readwrite |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $constructArgs = []; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @readwrite |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $properties = []; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @readwrite |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $methods = []; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var Inspector |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $classMetaData; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @readwrite |
|
69
|
|
|
* @var ObjectResolver |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $resolver; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Gets class meta data (Inspector) |
|
75
|
|
|
* |
|
76
|
|
|
* @return Inspector |
|
77
|
|
|
*/ |
|
78
|
56 |
|
protected function getClassMetaData() |
|
79
|
|
|
{ |
|
80
|
56 |
|
if (is_null($this->classMetaData)) { |
|
81
|
56 |
|
$this->classMetaData = Inspector::forClass($this->getClassName()); |
|
82
|
56 |
|
} |
|
83
|
56 |
|
return $this->classMetaData; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets definition class name |
|
88
|
|
|
* |
|
89
|
|
|
* If class name is not set and there is an instance set the class name |
|
90
|
|
|
* will be retrieved from instance object. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
72 |
|
public function getClassName() |
|
95
|
|
|
{ |
|
96
|
72 |
|
if (is_null($this->className) && is_object($this->instance)) { |
|
97
|
48 |
|
$this->className = get_class($this->instance); |
|
98
|
48 |
|
} |
|
99
|
72 |
|
return $this->className; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Gets the instance object for current definition |
|
104
|
|
|
* |
|
105
|
|
|
* If instance is not defined yet and the class name is set and |
|
106
|
|
|
* is an existing class, a new instance will be created and the |
|
107
|
|
|
* constructor arguments will be used. |
|
108
|
|
|
* |
|
109
|
|
|
* @return object |
|
110
|
|
|
*/ |
|
111
|
58 |
|
public function getInstance() |
|
112
|
|
|
{ |
|
113
|
58 |
|
if (is_null($this->instance) && class_exists($this->className)) { |
|
114
|
22 |
|
$reflection = $this->getClassMetaData()->getReflection(); |
|
115
|
22 |
|
$this->instance = (!empty($this->constructArgs)) |
|
116
|
22 |
|
? $reflection->newInstanceArgs( |
|
117
|
10 |
|
$this->getResolver()->checkValues($this->constructArgs) |
|
118
|
10 |
|
) |
|
119
|
22 |
|
: new $this->className(); |
|
120
|
22 |
|
} |
|
121
|
58 |
|
return $this->instance; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Sets constructor arguments used on instance instantiation |
|
126
|
|
|
* |
|
127
|
|
|
* @param array $arguments |
|
128
|
|
|
* @return $this|self |
|
129
|
|
|
*/ |
|
130
|
26 |
|
public function setConstructArgs(array $arguments) |
|
131
|
|
|
{ |
|
132
|
26 |
|
$this->constructArgs = $arguments; |
|
133
|
26 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Set a method to be called when resolving this definition |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $name Method name |
|
140
|
|
|
* @param array $arguments Method parameters |
|
141
|
|
|
* |
|
142
|
|
|
* @return $this|self |
|
143
|
|
|
*/ |
|
144
|
34 |
|
public function setMethod($name, array $arguments = []) |
|
145
|
|
|
{ |
|
146
|
34 |
|
if (!$this->getClassMetaData()->hasMethod($name)) { |
|
147
|
2 |
|
throw new InvalidArgumentException( |
|
148
|
2 |
|
"The method {$name} does not exists in class ". |
|
149
|
2 |
|
"{$this->getClassName()}" |
|
150
|
2 |
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
32 |
|
$this->methods[] = compact('name', 'arguments'); |
|
154
|
32 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Sets property value when resolving this definition |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $name The property name |
|
161
|
|
|
* @param mixed $value The property value |
|
162
|
|
|
* |
|
163
|
|
|
* @return $this|self |
|
164
|
|
|
*/ |
|
165
|
30 |
|
public function setProperty($name, $value) |
|
166
|
|
|
{ |
|
167
|
30 |
|
if (!$this->getClassMetaData()->hasProperty($name)) { |
|
168
|
2 |
|
throw new InvalidArgumentException( |
|
169
|
2 |
|
"The property {$name} does not exists in class ". |
|
170
|
2 |
|
"{$this->getClassName()}" |
|
171
|
2 |
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
28 |
|
$this->properties[$name] = $value; |
|
175
|
28 |
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Resolves current definition and returns its value |
|
180
|
|
|
* |
|
181
|
|
|
* @return mixed |
|
182
|
|
|
*/ |
|
183
|
58 |
|
public function resolve() |
|
184
|
|
|
{ |
|
185
|
58 |
|
return $this->getResolver()->resolve(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Gets property values |
|
190
|
|
|
* |
|
191
|
|
|
* @return array |
|
192
|
|
|
*/ |
|
193
|
58 |
|
public function getProperties() |
|
194
|
|
|
{ |
|
195
|
58 |
|
return $this->properties; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Returns the list of methods to call |
|
200
|
|
|
* |
|
201
|
|
|
* @return mixed |
|
202
|
|
|
*/ |
|
203
|
58 |
|
public function getMethods() |
|
204
|
|
|
{ |
|
205
|
58 |
|
return $this->methods; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Returns resolver for this definition |
|
210
|
|
|
* |
|
211
|
|
|
* @return Resolver|ObjectResolver |
|
212
|
|
|
*/ |
|
213
|
58 |
|
protected function getResolver() |
|
214
|
|
|
{ |
|
215
|
58 |
|
if (is_null($this->resolver)) { |
|
216
|
58 |
|
$this->resolver = new Resolver( |
|
217
|
|
|
[ |
|
218
|
|
|
'definition' => $this |
|
219
|
58 |
|
] |
|
220
|
58 |
|
); |
|
221
|
58 |
|
} |
|
222
|
58 |
|
return $this->resolver; |
|
223
|
|
|
} |
|
224
|
|
|
} |