|
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
|
|
|
*/ |
|
80
|
|
|
public function setClass($class) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->class = $class; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Gets the class |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getClass() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->class; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param callable $factory |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setFactory($factory) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->factory = $factory; |
|
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return callable |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getFactory() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->factory; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array $properties |
|
114
|
|
|
*/ |
|
115
|
|
|
public function setProperties($properties) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->properties = $properties; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Gets all properties |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getProperties() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->properties; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Adds a property |
|
131
|
|
|
* @param int|string $name |
|
132
|
|
|
* @param mixed $value |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
|
|
public function setProperty($name, $value) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->properties[$name] = $value; |
|
138
|
|
|
|
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Gets the property by given name |
|
144
|
|
|
* @param string $name |
|
145
|
|
|
* @return mixed |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getProperty($name) |
|
148
|
|
|
{ |
|
149
|
|
|
return isset($this->properties[$name]) ? $this->properties[$name] : null; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* add an argument |
|
154
|
|
|
* |
|
155
|
|
|
* @param mixed $value |
|
156
|
|
|
* |
|
157
|
|
|
* @return $this |
|
158
|
|
|
*/ |
|
159
|
|
|
public function addArgument($value) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->arguments[] = $value; |
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Sets a specific argument. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $key |
|
169
|
|
|
* @param mixed $value |
|
170
|
|
|
* @return $this |
|
171
|
|
|
*/ |
|
172
|
|
|
public function setArgument($key, $value) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->arguments[$key] = $value; |
|
175
|
|
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Sets the arguments to pass to the service constructor/factory method. |
|
180
|
|
|
* |
|
181
|
|
|
* @param array $arguments |
|
182
|
|
|
* @return $this |
|
183
|
|
|
*/ |
|
184
|
|
|
public function setArguments(array $arguments) |
|
185
|
|
|
{ |
|
186
|
|
|
$this->arguments = $arguments; |
|
187
|
|
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Gets all arguments of constructor |
|
192
|
|
|
* |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getArguments() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->arguments; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Gets the argument at the specified position of constructor |
|
202
|
|
|
* |
|
203
|
|
|
* @param int|string $index |
|
204
|
|
|
* @return mixed |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getArgument($index) |
|
207
|
|
|
{ |
|
208
|
|
|
return isset($this->arguments[$index]) ? $this->arguments[$index] : null; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Adds a method |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $method |
|
215
|
|
|
* @param string|array $arguments |
|
216
|
|
|
* @return $this |
|
217
|
|
|
*/ |
|
218
|
|
|
public function addMethodCall($method, $arguments) |
|
219
|
|
|
{ |
|
220
|
|
|
$this->calls[] = [ |
|
221
|
|
|
$method, |
|
222
|
|
|
(array)$arguments |
|
223
|
|
|
]; |
|
224
|
|
|
return $this; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Sets the methods to call after service initialization. |
|
229
|
|
|
* |
|
230
|
|
|
* @param array methods |
|
231
|
|
|
* @return $this |
|
232
|
|
|
*/ |
|
233
|
|
|
public function setMethodCalls(array $methods) |
|
234
|
|
|
{ |
|
235
|
|
|
$this->calls = array(); |
|
236
|
|
|
foreach ($methods as $call) { |
|
237
|
|
|
$this->addMethodCall($call[0], $call[1]); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $this; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Gets all methods |
|
245
|
|
|
* |
|
246
|
|
|
* @return array |
|
247
|
|
|
*/ |
|
248
|
|
|
public function getMethodCalls() |
|
249
|
|
|
{ |
|
250
|
|
|
return $this->calls; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Check if the current definition has a given method to call after service initialization. |
|
255
|
|
|
* |
|
256
|
|
|
* @param string $method The method name to search for |
|
257
|
|
|
* |
|
258
|
|
|
* @return bool |
|
259
|
|
|
*/ |
|
260
|
|
|
public function hasMethodCall($method) |
|
261
|
|
|
{ |
|
262
|
|
|
foreach ($this->calls as $call) { |
|
263
|
|
|
if ($call[0] === $method) { |
|
264
|
|
|
return true; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return false; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Sets tags for this definition. |
|
273
|
|
|
* |
|
274
|
|
|
* @param array $tags |
|
275
|
|
|
* @return $this |
|
276
|
|
|
*/ |
|
277
|
|
|
public function setTags(array $tags) |
|
278
|
|
|
{ |
|
279
|
|
|
$this->tags = $tags; |
|
280
|
|
|
|
|
281
|
|
|
return $this; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Returns all tags. |
|
286
|
|
|
* |
|
287
|
|
|
* @return array An array of tags |
|
288
|
|
|
*/ |
|
289
|
|
|
public function getTags() |
|
290
|
|
|
{ |
|
291
|
|
|
return $this->tags; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Gets a tag by name. |
|
296
|
|
|
* |
|
297
|
|
|
* @param string $name The tag name |
|
298
|
|
|
* |
|
299
|
|
|
* @return array An array of attributes |
|
300
|
|
|
*/ |
|
301
|
|
|
public function getTag($name) |
|
302
|
|
|
{ |
|
303
|
|
|
return isset($this->tags[$name]) ? $this->tags[$name] : array(); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Adds a tag for this definition. |
|
308
|
|
|
* |
|
309
|
|
|
* @param string $name The tag name |
|
310
|
|
|
* @param array $attributes An array of attributes |
|
311
|
|
|
* |
|
312
|
|
|
* @return $this |
|
313
|
|
|
*/ |
|
314
|
|
|
public function addTag($name, array $attributes = array()) |
|
315
|
|
|
{ |
|
316
|
|
|
$this->tags[$name][] = $attributes; |
|
317
|
|
|
|
|
318
|
|
|
return $this; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Whether this definition has a tag with the given name. |
|
323
|
|
|
* |
|
324
|
|
|
* @param string $name |
|
325
|
|
|
* |
|
326
|
|
|
* @return bool |
|
327
|
|
|
*/ |
|
328
|
|
|
public function hasTag($name) |
|
329
|
|
|
{ |
|
330
|
|
|
return isset($this->tags[$name]); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Clears all tags for a given name. |
|
335
|
|
|
* |
|
336
|
|
|
* @param string $name The tag name |
|
337
|
|
|
* |
|
338
|
|
|
* @return $this |
|
339
|
|
|
*/ |
|
340
|
|
|
public function clearTag($name) |
|
341
|
|
|
{ |
|
342
|
|
|
unset($this->tags[$name]); |
|
343
|
|
|
|
|
344
|
|
|
return $this; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Clears the tags for this definition. |
|
349
|
|
|
* |
|
350
|
|
|
* @return $this |
|
351
|
|
|
*/ |
|
352
|
|
|
public function clearTags() |
|
353
|
|
|
{ |
|
354
|
|
|
$this->tags = array(); |
|
355
|
|
|
|
|
356
|
|
|
return $this; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* Is the definition autowired? |
|
361
|
|
|
* |
|
362
|
|
|
* @return bool |
|
363
|
|
|
*/ |
|
364
|
|
|
public function isAutowired() |
|
365
|
|
|
{ |
|
366
|
|
|
return $this->autowired; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* Enables/disables autowiring. |
|
371
|
|
|
* |
|
372
|
|
|
* @param bool $autowired |
|
373
|
|
|
* |
|
374
|
|
|
* @return $this |
|
375
|
|
|
*/ |
|
376
|
|
|
public function setAutowired($autowired) |
|
377
|
|
|
{ |
|
378
|
|
|
$this->autowired = (bool) $autowired; |
|
379
|
|
|
|
|
380
|
|
|
return $this; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* Sets if the service must be shared or not. |
|
385
|
|
|
* |
|
386
|
|
|
* @param bool $shared Whether the service must be shared or not |
|
387
|
|
|
* |
|
388
|
|
|
* @return $this |
|
389
|
|
|
*/ |
|
390
|
|
|
public function setShared($shared) |
|
391
|
|
|
{ |
|
392
|
|
|
$this->changes['shared'] = true; |
|
|
|
|
|
|
393
|
|
|
|
|
394
|
|
|
$this->shared = (bool) $shared; |
|
395
|
|
|
|
|
396
|
|
|
return $this; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* Whether this service is shared. |
|
401
|
|
|
* |
|
402
|
|
|
* @return bool |
|
403
|
|
|
*/ |
|
404
|
|
|
public function isShared() |
|
405
|
|
|
{ |
|
406
|
|
|
return $this->shared; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* Sets the visibility of this service. |
|
411
|
|
|
* |
|
412
|
|
|
* @param bool $boolean |
|
413
|
|
|
* |
|
414
|
|
|
* @return $this |
|
415
|
|
|
*/ |
|
416
|
|
|
public function setPublic($boolean) |
|
417
|
|
|
{ |
|
418
|
|
|
$this->public = (bool) $boolean; |
|
419
|
|
|
|
|
420
|
|
|
return $this; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
/** |
|
424
|
|
|
* Whether this service is public facing. |
|
425
|
|
|
* |
|
426
|
|
|
* @return bool |
|
427
|
|
|
*/ |
|
428
|
|
|
public function isPublic() |
|
429
|
|
|
{ |
|
430
|
|
|
return $this->public; |
|
431
|
|
|
} |
|
432
|
|
|
} |
|
433
|
|
|
|
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..