1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slince/di package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Slince\Di; |
13
|
|
|
|
14
|
|
|
class Definition |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $class; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Array of arguments |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $arguments = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Array of setters |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $calls = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Array of properties |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $properties = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ['@Foo\Bar', 'createBaz'] |
41
|
|
|
* or |
42
|
|
|
* ['Foo\Bar', 'createBaz'] |
43
|
|
|
* @var \callable |
44
|
|
|
*/ |
45
|
|
|
protected $factory; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $tags; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var boolean |
54
|
|
|
*/ |
55
|
|
|
protected $autowired; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var boolean |
59
|
|
|
*/ |
60
|
|
|
protected $shared; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var boolean |
64
|
|
|
*/ |
65
|
|
|
protected $public; |
66
|
|
|
|
67
|
|
|
public function __construct( |
68
|
|
|
$class = null, |
69
|
|
|
array $arguments = [] |
70
|
|
|
) { |
71
|
|
|
$this->class = $class; |
72
|
|
|
$this->arguments = $arguments; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set class for the definition. |
77
|
|
|
* |
78
|
|
|
* @param string $class |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function setClass($class) |
82
|
|
|
{ |
83
|
|
|
$this->class = $class; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets the class |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getClass() |
93
|
|
|
{ |
94
|
|
|
return $this->class; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param callable $factory |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setFactory($factory) |
102
|
|
|
{ |
103
|
|
|
$this->factory = $factory; |
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return callable |
109
|
|
|
*/ |
110
|
|
|
public function getFactory() |
111
|
|
|
{ |
112
|
|
|
return $this->factory; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $properties |
117
|
|
|
*/ |
118
|
|
|
public function setProperties($properties) |
119
|
|
|
{ |
120
|
|
|
$this->properties = $properties; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Gets all properties |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getProperties() |
128
|
|
|
{ |
129
|
|
|
return $this->properties; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Adds a property |
134
|
|
|
* @param int|string $name |
135
|
|
|
* @param mixed $value |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function setProperty($name, $value) |
139
|
|
|
{ |
140
|
|
|
$this->properties[$name] = $value; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets the property by given name |
147
|
|
|
* @param string $name |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
|
|
public function getProperty($name) |
151
|
|
|
{ |
152
|
|
|
return isset($this->properties[$name]) ? $this->properties[$name] : null; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* add an argument |
157
|
|
|
* |
158
|
|
|
* @param mixed $value |
159
|
|
|
* |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
|
|
public function addArgument($value) |
163
|
|
|
{ |
164
|
|
|
$this->arguments[] = $value; |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Sets a specific argument. |
170
|
|
|
* |
171
|
|
|
* @param string $key |
172
|
|
|
* @param mixed $value |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function setArgument($key, $value) |
176
|
|
|
{ |
177
|
|
|
$this->arguments[$key] = $value; |
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Sets the arguments to pass to the service constructor/factory method. |
183
|
|
|
* |
184
|
|
|
* @param array $arguments |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function setArguments(array $arguments) |
188
|
|
|
{ |
189
|
|
|
$this->arguments = $arguments; |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Gets all arguments of constructor |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public function getArguments() |
199
|
|
|
{ |
200
|
|
|
return $this->arguments; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Gets the argument at the specified position of constructor |
205
|
|
|
* |
206
|
|
|
* @param int|string $index |
207
|
|
|
* @return mixed |
208
|
|
|
*/ |
209
|
|
|
public function getArgument($index) |
210
|
|
|
{ |
211
|
|
|
return isset($this->arguments[$index]) ? $this->arguments[$index] : null; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Adds a method |
216
|
|
|
* |
217
|
|
|
* @param string $method |
218
|
|
|
* @param string|array $arguments |
219
|
|
|
* @return $this |
220
|
|
|
*/ |
221
|
|
|
public function addMethodCall($method, $arguments) |
222
|
|
|
{ |
223
|
|
|
$this->calls[] = [ |
224
|
|
|
$method, |
225
|
|
|
(array)$arguments |
226
|
|
|
]; |
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Sets the methods to call after service initialization. |
232
|
|
|
* |
233
|
|
|
* @param array methods |
234
|
|
|
* @return $this |
235
|
|
|
*/ |
236
|
|
|
public function setMethodCalls(array $methods) |
237
|
|
|
{ |
238
|
|
|
$this->calls = array(); |
239
|
|
|
foreach ($methods as $call) { |
240
|
|
|
$this->addMethodCall($call[0], $call[1]); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Gets all methods |
248
|
|
|
* |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
|
|
public function getMethodCalls() |
252
|
|
|
{ |
253
|
|
|
return $this->calls; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Check if the current definition has a given method to call after service initialization. |
258
|
|
|
* |
259
|
|
|
* @param string $method The method name to search for |
260
|
|
|
* |
261
|
|
|
* @return bool |
262
|
|
|
*/ |
263
|
|
|
public function hasMethodCall($method) |
264
|
|
|
{ |
265
|
|
|
foreach ($this->calls as $call) { |
266
|
|
|
if ($call[0] === $method) { |
267
|
|
|
return true; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return false; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Sets tags for this definition. |
276
|
|
|
* |
277
|
|
|
* @param array $tags |
278
|
|
|
* @return $this |
279
|
|
|
*/ |
280
|
|
|
public function setTags(array $tags) |
281
|
|
|
{ |
282
|
|
|
$this->tags = $tags; |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Returns all tags. |
289
|
|
|
* |
290
|
|
|
* @return array An array of tags |
291
|
|
|
*/ |
292
|
|
|
public function getTags() |
293
|
|
|
{ |
294
|
|
|
return $this->tags; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Gets a tag by name. |
299
|
|
|
* |
300
|
|
|
* @param string $name The tag name |
301
|
|
|
* |
302
|
|
|
* @return array An array of attributes |
303
|
|
|
*/ |
304
|
|
|
public function getTag($name) |
305
|
|
|
{ |
306
|
|
|
return isset($this->tags[$name]) ? $this->tags[$name] : array(); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Adds a tag for this definition. |
311
|
|
|
* |
312
|
|
|
* @param string $name The tag name |
313
|
|
|
* @param array $attributes An array of attributes |
314
|
|
|
* |
315
|
|
|
* @return $this |
316
|
|
|
*/ |
317
|
|
|
public function addTag($name, array $attributes = array()) |
318
|
|
|
{ |
319
|
|
|
$this->tags[$name][] = $attributes; |
320
|
|
|
|
321
|
|
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Whether this definition has a tag with the given name. |
326
|
|
|
* |
327
|
|
|
* @param string $name |
328
|
|
|
* |
329
|
|
|
* @return bool |
330
|
|
|
*/ |
331
|
|
|
public function hasTag($name) |
332
|
|
|
{ |
333
|
|
|
return isset($this->tags[$name]); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Clears all tags for a given name. |
338
|
|
|
* |
339
|
|
|
* @param string $name The tag name |
340
|
|
|
* |
341
|
|
|
* @return $this |
342
|
|
|
*/ |
343
|
|
|
public function clearTag($name) |
344
|
|
|
{ |
345
|
|
|
unset($this->tags[$name]); |
346
|
|
|
|
347
|
|
|
return $this; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Clears the tags for this definition. |
352
|
|
|
* |
353
|
|
|
* @return $this |
354
|
|
|
*/ |
355
|
|
|
public function clearTags() |
356
|
|
|
{ |
357
|
|
|
$this->tags = array(); |
358
|
|
|
|
359
|
|
|
return $this; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Is the definition autowired? |
364
|
|
|
* |
365
|
|
|
* @return bool |
366
|
|
|
*/ |
367
|
|
|
public function isAutowired() |
368
|
|
|
{ |
369
|
|
|
return $this->autowired; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Enables/disables autowiring. |
374
|
|
|
* |
375
|
|
|
* @param bool $autowired |
376
|
|
|
* |
377
|
|
|
* @return $this |
378
|
|
|
*/ |
379
|
|
|
public function setAutowired($autowired) |
380
|
|
|
{ |
381
|
|
|
$this->autowired = (bool) $autowired; |
382
|
|
|
|
383
|
|
|
return $this; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Sets if the service must be shared or not. |
388
|
|
|
* |
389
|
|
|
* @param bool $shared Whether the service must be shared or not |
390
|
|
|
* |
391
|
|
|
* @return $this |
392
|
|
|
*/ |
393
|
|
|
public function setShared($shared) |
394
|
|
|
{ |
395
|
|
|
$this->shared = (bool) $shared; |
396
|
|
|
|
397
|
|
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Whether this service is shared. |
402
|
|
|
* |
403
|
|
|
* @return bool |
404
|
|
|
*/ |
405
|
|
|
public function isShared() |
406
|
|
|
{ |
407
|
|
|
return $this->shared; |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..