|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
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 Symfony\Component\DependencyInjection; |
|
|
13 |
|
|
|
14 |
/** |
|
|
15 |
* Definition represents a service definition. |
|
|
16 |
* |
|
|
17 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
18 |
* |
|
|
19 |
* @api |
|
|
20 |
*/ |
|
|
21 |
class Definition |
|
|
22 |
{ |
|
|
23 |
private $class; |
|
|
24 |
private $file; |
|
|
25 |
private $factoryClass; |
|
|
26 |
private $factoryMethod; |
|
|
27 |
private $factoryService; |
|
|
28 |
private $scope; |
|
|
29 |
private $properties; |
|
|
30 |
private $calls; |
|
|
31 |
private $configurator; |
|
|
32 |
private $tags; |
|
|
33 |
private $public; |
|
|
34 |
private $synthetic; |
|
|
35 |
private $abstract; |
|
|
36 |
|
|
|
37 |
protected $arguments; |
|
|
38 |
|
|
|
39 |
/** |
|
|
40 |
* Constructor. |
|
|
41 |
* |
|
|
42 |
* @param string $class The service class |
|
|
43 |
* @param array $arguments An array of arguments to pass to the service constructor |
|
|
44 |
* |
|
|
45 |
* @api |
|
|
46 |
*/ |
|
|
47 |
public function __construct($class = null, array $arguments = array()) |
|
|
48 |
{ |
|
|
49 |
$this->class = $class; |
|
|
50 |
$this->arguments = $arguments; |
|
|
51 |
$this->calls = array(); |
|
|
52 |
$this->scope = ContainerInterface::SCOPE_CONTAINER; |
|
|
53 |
$this->tags = array(); |
|
|
54 |
$this->public = true; |
|
|
55 |
$this->synthetic = false; |
|
|
56 |
$this->abstract = false; |
|
|
57 |
$this->properties = array(); |
|
|
58 |
} |
|
|
59 |
|
|
|
60 |
/** |
|
|
61 |
* Sets the name of the class that acts as a factory using the factory method, |
|
|
62 |
* which will be invoked statically. |
|
|
63 |
* |
|
|
64 |
* @param string $factoryClass The factory class name |
|
|
65 |
* |
|
|
66 |
* @return Definition The current instance |
|
|
67 |
* |
|
|
68 |
* @api |
|
|
69 |
*/ |
|
|
70 |
public function setFactoryClass($factoryClass) |
|
|
71 |
{ |
|
|
72 |
$this->factoryClass = $factoryClass; |
|
|
73 |
|
|
|
74 |
return $this; |
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
/** |
|
|
78 |
* Gets the factory class. |
|
|
79 |
* |
|
|
80 |
* @return string The factory class name |
|
|
81 |
* |
|
|
82 |
* @api |
|
|
83 |
*/ |
|
|
84 |
public function getFactoryClass() |
|
|
85 |
{ |
|
|
86 |
return $this->factoryClass; |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
/** |
|
|
90 |
* Sets the factory method able to create an instance of this class. |
|
|
91 |
* |
|
|
92 |
* @param string $factoryMethod The factory method name |
|
|
93 |
* |
|
|
94 |
* @return Definition The current instance |
|
|
95 |
* |
|
|
96 |
* @api |
|
|
97 |
*/ |
|
|
98 |
public function setFactoryMethod($factoryMethod) |
|
|
99 |
{ |
|
|
100 |
$this->factoryMethod = $factoryMethod; |
|
|
101 |
|
|
|
102 |
return $this; |
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
/** |
|
|
106 |
* Gets the factory method. |
|
|
107 |
* |
|
|
108 |
* @return string The factory method name |
|
|
109 |
* |
|
|
110 |
* @api |
|
|
111 |
*/ |
|
|
112 |
public function getFactoryMethod() |
|
|
113 |
{ |
|
|
114 |
return $this->factoryMethod; |
|
|
115 |
} |
|
|
116 |
|
|
|
117 |
/** |
|
|
118 |
* Sets the name of the service that acts as a factory using the factory method. |
|
|
119 |
* |
|
|
120 |
* @param string $factoryService The factory service id |
|
|
121 |
* |
|
|
122 |
* @return Definition The current instance |
|
|
123 |
* |
|
|
124 |
* @api |
|
|
125 |
*/ |
|
|
126 |
public function setFactoryService($factoryService) |
|
|
127 |
{ |
|
|
128 |
$this->factoryService = $factoryService; |
|
|
129 |
|
|
|
130 |
return $this; |
|
|
131 |
} |
|
|
132 |
|
|
|
133 |
/** |
|
|
134 |
* Gets the factory service id. |
|
|
135 |
* |
|
|
136 |
* @return string The factory service id |
|
|
137 |
* |
|
|
138 |
* @api |
|
|
139 |
*/ |
|
|
140 |
public function getFactoryService() |
|
|
141 |
{ |
|
|
142 |
return $this->factoryService; |
|
|
143 |
} |
|
|
144 |
|
|
|
145 |
/** |
|
|
146 |
* Sets the service class. |
|
|
147 |
* |
|
|
148 |
* @param string $class The service class |
|
|
149 |
* |
|
|
150 |
* @return Definition The current instance |
|
|
151 |
* |
|
|
152 |
* @api |
|
|
153 |
*/ |
|
|
154 |
public function setClass($class) |
|
|
155 |
{ |
|
|
156 |
$this->class = $class; |
|
|
157 |
|
|
|
158 |
return $this; |
|
|
159 |
} |
|
|
160 |
|
|
|
161 |
/** |
|
|
162 |
* Sets the service class. |
|
|
163 |
* |
|
|
164 |
* @return string The service class |
|
|
165 |
* |
|
|
166 |
* @api |
|
|
167 |
*/ |
|
|
168 |
public function getClass() |
|
|
169 |
{ |
|
|
170 |
return $this->class; |
|
|
171 |
} |
|
|
172 |
|
|
|
173 |
/** |
|
|
174 |
* Sets the arguments to pass to the service constructor/factory method. |
|
|
175 |
* |
|
|
176 |
* @param array $arguments An array of arguments |
|
|
177 |
* |
|
|
178 |
* @return Definition The current instance |
|
|
179 |
* |
|
|
180 |
* @api |
|
|
181 |
*/ |
|
|
182 |
public function setArguments(array $arguments) |
|
|
183 |
{ |
|
|
184 |
$this->arguments = $arguments; |
|
|
185 |
|
|
|
186 |
return $this; |
|
|
187 |
} |
|
|
188 |
|
|
|
189 |
/** |
|
|
190 |
* @api |
|
|
191 |
*/ |
|
|
192 |
public function setProperties(array $properties) |
|
|
193 |
{ |
|
|
194 |
$this->properties = $properties; |
|
|
195 |
|
|
|
196 |
return $this; |
|
|
197 |
} |
|
|
198 |
|
|
|
199 |
/** |
|
|
200 |
* @api |
|
|
201 |
*/ |
|
|
202 |
public function getProperties() |
|
|
203 |
{ |
|
|
204 |
return $this->properties; |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
/** |
|
|
208 |
* @api |
|
|
209 |
*/ |
|
|
210 |
public function setProperty($name, $value) |
|
|
211 |
{ |
|
|
212 |
$this->properties[$name] = $value; |
|
|
213 |
|
|
|
214 |
return $this; |
|
|
215 |
} |
|
|
216 |
|
|
|
217 |
/** |
|
|
218 |
* Adds an argument to pass to the service constructor/factory method. |
|
|
219 |
* |
|
|
220 |
* @param mixed $argument An argument |
|
|
221 |
* |
|
|
222 |
* @return Definition The current instance |
|
|
223 |
* |
|
|
224 |
* @api |
|
|
225 |
*/ |
|
|
226 |
public function addArgument($argument) |
|
|
227 |
{ |
|
|
228 |
$this->arguments[] = $argument; |
|
|
229 |
|
|
|
230 |
return $this; |
|
|
231 |
} |
|
|
232 |
|
|
|
233 |
/** |
|
|
234 |
* Sets a specific argument |
|
|
235 |
* |
|
|
236 |
* @param integer $index |
|
|
237 |
* @param mixed $argument |
|
|
238 |
* |
|
|
239 |
* @return Definition The current instance |
|
|
240 |
* |
|
|
241 |
* @api |
|
|
242 |
*/ |
|
|
243 |
public function replaceArgument($index, $argument) |
|
|
244 |
{ |
|
|
245 |
if ($index < 0 || $index > count($this->arguments) - 1) { |
|
|
246 |
throw new \OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1)); |
|
|
247 |
} |
|
|
248 |
|
|
|
249 |
$this->arguments[$index] = $argument; |
|
|
250 |
|
|
|
251 |
return $this; |
|
|
252 |
} |
|
|
253 |
|
|
|
254 |
/** |
|
|
255 |
* Gets the arguments to pass to the service constructor/factory method. |
|
|
256 |
* |
|
|
257 |
* @return array The array of arguments |
|
|
258 |
* |
|
|
259 |
* @api |
|
|
260 |
*/ |
|
|
261 |
public function getArguments() |
|
|
262 |
{ |
|
|
263 |
return $this->arguments; |
|
|
264 |
} |
|
|
265 |
|
|
|
266 |
/** |
|
|
267 |
* Gets an argument to pass to the service constructor/factory method. |
|
|
268 |
* |
|
|
269 |
* @param integer $index |
|
|
270 |
* |
|
|
271 |
* @return mixed The argument value |
|
|
272 |
* |
|
|
273 |
* @api |
|
|
274 |
*/ |
|
|
275 |
public function getArgument($index) |
|
|
276 |
{ |
|
|
277 |
if ($index < 0 || $index > count($this->arguments) - 1) { |
|
|
278 |
throw new \OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1)); |
|
|
279 |
} |
|
|
280 |
|
|
|
281 |
return $this->arguments[$index]; |
|
|
282 |
} |
|
|
283 |
|
|
|
284 |
/** |
|
|
285 |
* Sets the methods to call after service initialization. |
|
|
286 |
* |
|
|
287 |
* @param array $calls An array of method calls |
|
|
288 |
* |
|
|
289 |
* @return Definition The current instance |
|
|
290 |
* |
|
|
291 |
* @api |
|
|
292 |
*/ |
|
|
293 |
public function setMethodCalls(array $calls = array()) |
|
|
294 |
{ |
|
|
295 |
$this->calls = array(); |
|
|
296 |
foreach ($calls as $call) { |
|
|
297 |
$this->addMethodCall($call[0], $call[1]); |
|
|
298 |
} |
|
|
299 |
|
|
|
300 |
return $this; |
|
|
301 |
} |
|
|
302 |
|
|
|
303 |
/** |
|
|
304 |
* Adds a method to call after service initialization. |
|
|
305 |
* |
|
|
306 |
* @param string $method The method name to call |
|
|
307 |
* @param array $arguments An array of arguments to pass to the method call |
|
|
308 |
* |
|
|
309 |
* @return Definition The current instance |
|
|
310 |
* |
|
|
311 |
* @api |
|
|
312 |
*/ |
|
|
313 |
public function addMethodCall($method, array $arguments = array()) |
|
|
314 |
{ |
|
|
315 |
$this->calls[] = array($method, $arguments); |
|
|
316 |
|
|
|
317 |
return $this; |
|
|
318 |
} |
|
|
319 |
|
|
|
320 |
/** |
|
|
321 |
* Removes a method to call after service initialization. |
|
|
322 |
* |
|
|
323 |
* @param string $method The method name to remove |
|
|
324 |
* |
|
|
325 |
* @return Definition The current instance |
|
|
326 |
* |
|
|
327 |
* @api |
|
|
328 |
*/ |
|
|
329 |
public function removeMethodCall($method) |
|
|
330 |
{ |
|
|
331 |
foreach ($this->calls as $i => $call) { |
|
|
332 |
if ($call[0] === $method) { |
|
|
333 |
unset($this->calls[$i]); |
|
|
334 |
break; |
|
|
335 |
} |
|
|
336 |
} |
|
|
337 |
|
|
|
338 |
return $this; |
|
|
339 |
} |
|
|
340 |
|
|
|
341 |
/** |
|
|
342 |
* Check if the current definition has a given method to call after service initialization. |
|
|
343 |
* |
|
|
344 |
* @param string $method The method name to search for |
|
|
345 |
* |
|
|
346 |
* @return Boolean |
|
|
347 |
* |
|
|
348 |
* @api |
|
|
349 |
*/ |
|
|
350 |
public function hasMethodCall($method) |
|
|
351 |
{ |
|
|
352 |
foreach ($this->calls as $call) { |
|
|
353 |
if ($call[0] === $method) { |
|
|
354 |
return true; |
|
|
355 |
} |
|
|
356 |
} |
|
|
357 |
|
|
|
358 |
return false; |
|
|
359 |
} |
|
|
360 |
|
|
|
361 |
/** |
|
|
362 |
* Gets the methods to call after service initialization. |
|
|
363 |
* |
|
|
364 |
* @return array An array of method calls |
|
|
365 |
* |
|
|
366 |
* @api |
|
|
367 |
*/ |
|
|
368 |
public function getMethodCalls() |
|
|
369 |
{ |
|
|
370 |
return $this->calls; |
|
|
371 |
} |
|
|
372 |
|
|
|
373 |
/** |
|
|
374 |
* Sets tags for this definition |
|
|
375 |
* |
|
|
376 |
* @param array $tags |
|
|
377 |
* |
|
|
378 |
* @return Definition the current instance |
|
|
379 |
* |
|
|
380 |
* @api |
|
|
381 |
*/ |
|
|
382 |
public function setTags(array $tags) |
|
|
383 |
{ |
|
|
384 |
$this->tags = $tags; |
|
|
385 |
|
|
|
386 |
return $this; |
|
|
387 |
} |
|
|
388 |
|
|
|
389 |
/** |
|
|
390 |
* Returns all tags. |
|
|
391 |
* |
|
|
392 |
* @return array An array of tags |
|
|
393 |
* |
|
|
394 |
* @api |
|
|
395 |
*/ |
|
|
396 |
public function getTags() |
|
|
397 |
{ |
|
|
398 |
return $this->tags; |
|
|
399 |
} |
|
|
400 |
|
|
|
401 |
/** |
|
|
402 |
* Gets a tag by name. |
|
|
403 |
* |
|
|
404 |
* @param string $name The tag name |
|
|
405 |
* |
|
|
406 |
* @return array An array of attributes |
|
|
407 |
* |
|
|
408 |
* @api |
|
|
409 |
*/ |
|
|
410 |
public function getTag($name) |
|
|
411 |
{ |
|
|
412 |
return isset($this->tags[$name]) ? $this->tags[$name] : array(); |
|
|
413 |
} |
|
|
414 |
|
|
|
415 |
/** |
|
|
416 |
* Adds a tag for this definition. |
|
|
417 |
* |
|
|
418 |
* @param string $name The tag name |
|
|
419 |
* @param array $attributes An array of attributes |
|
|
420 |
* |
|
|
421 |
* @return Definition The current instance |
|
|
422 |
* |
|
|
423 |
* @api |
|
|
424 |
*/ |
|
|
425 |
public function addTag($name, array $attributes = array()) |
|
|
426 |
{ |
|
|
427 |
$this->tags[$name][] = $attributes; |
|
|
428 |
|
|
|
429 |
return $this; |
|
|
430 |
} |
|
|
431 |
|
|
|
432 |
/** |
|
|
433 |
* Whether this definition has a tag with the given name |
|
|
434 |
* |
|
|
435 |
* @param string $name |
|
|
436 |
* |
|
|
437 |
* @return Boolean |
|
|
438 |
* |
|
|
439 |
* @api |
|
|
440 |
*/ |
|
|
441 |
public function hasTag($name) |
|
|
442 |
{ |
|
|
443 |
return isset($this->tags[$name]); |
|
|
444 |
} |
|
|
445 |
|
|
|
446 |
/** |
|
|
447 |
* Clears the tags for this definition. |
|
|
448 |
* |
|
|
449 |
* @return Definition The current instance |
|
|
450 |
* |
|
|
451 |
* @api |
|
|
452 |
*/ |
|
|
453 |
public function clearTags() |
|
|
454 |
{ |
|
|
455 |
$this->tags = array(); |
|
|
456 |
|
|
|
457 |
return $this; |
|
|
458 |
} |
|
|
459 |
|
|
|
460 |
/** |
|
|
461 |
* Sets a file to require before creating the service. |
|
|
462 |
* |
|
|
463 |
* @param string $file A full pathname to include |
|
|
464 |
* |
|
|
465 |
* @return Definition The current instance |
|
|
466 |
* |
|
|
467 |
* @api |
|
|
468 |
*/ |
|
|
469 |
public function setFile($file) |
|
|
470 |
{ |
|
|
471 |
$this->file = $file; |
|
|
472 |
|
|
|
473 |
return $this; |
|
|
474 |
} |
|
|
475 |
|
|
|
476 |
/** |
|
|
477 |
* Gets the file to require before creating the service. |
|
|
478 |
* |
|
|
479 |
* @return string The full pathname to include |
|
|
480 |
* |
|
|
481 |
* @api |
|
|
482 |
*/ |
|
|
483 |
public function getFile() |
|
|
484 |
{ |
|
|
485 |
return $this->file; |
|
|
486 |
} |
|
|
487 |
|
|
|
488 |
/** |
|
|
489 |
* Sets the scope of the service |
|
|
490 |
* |
|
|
491 |
* @param string $scope Whether the service must be shared or not |
|
|
492 |
* |
|
|
493 |
* @return Definition The current instance |
|
|
494 |
* |
|
|
495 |
* @api |
|
|
496 |
*/ |
|
|
497 |
public function setScope($scope) |
|
|
498 |
{ |
|
|
499 |
$this->scope = $scope; |
|
|
500 |
|
|
|
501 |
return $this; |
|
|
502 |
} |
|
|
503 |
|
|
|
504 |
/** |
|
|
505 |
* Returns the scope of the service |
|
|
506 |
* |
|
|
507 |
* @return string |
|
|
508 |
* |
|
|
509 |
* @api |
|
|
510 |
*/ |
|
|
511 |
public function getScope() |
|
|
512 |
{ |
|
|
513 |
return $this->scope; |
|
|
514 |
} |
|
|
515 |
|
|
|
516 |
/** |
|
|
517 |
* Sets the visibility of this service. |
|
|
518 |
* |
|
|
519 |
* @param Boolean $boolean |
|
|
520 |
* @return Definition The current instance |
|
|
521 |
* |
|
|
522 |
* @api |
|
|
523 |
*/ |
|
|
524 |
public function setPublic($boolean) |
|
|
525 |
{ |
|
|
526 |
$this->public = (Boolean) $boolean; |
|
|
527 |
|
|
|
528 |
return $this; |
|
|
529 |
} |
|
|
530 |
|
|
|
531 |
/** |
|
|
532 |
* Whether this service is public facing |
|
|
533 |
* |
|
|
534 |
* @return Boolean |
|
|
535 |
* |
|
|
536 |
* @api |
|
|
537 |
*/ |
|
|
538 |
public function isPublic() |
|
|
539 |
{ |
|
|
540 |
return $this->public; |
|
|
541 |
} |
|
|
542 |
|
|
|
543 |
/** |
|
|
544 |
* Sets whether this definition is synthetic, that is not constructed by the |
|
|
545 |
* container, but dynamically injected. |
|
|
546 |
* |
|
|
547 |
* @param Boolean $boolean |
|
|
548 |
* |
|
|
549 |
* @return Definition the current instance |
|
|
550 |
* |
|
|
551 |
* @api |
|
|
552 |
*/ |
|
|
553 |
public function setSynthetic($boolean) |
|
|
554 |
{ |
|
|
555 |
$this->synthetic = (Boolean) $boolean; |
|
|
556 |
|
|
|
557 |
return $this; |
|
|
558 |
} |
|
|
559 |
|
|
|
560 |
/** |
|
|
561 |
* Whether this definition is synthetic, that is not constructed by the |
|
|
562 |
* container, but dynamically injected. |
|
|
563 |
* |
|
|
564 |
* @return Boolean |
|
|
565 |
* |
|
|
566 |
* @api |
|
|
567 |
*/ |
|
|
568 |
public function isSynthetic() |
|
|
569 |
{ |
|
|
570 |
return $this->synthetic; |
|
|
571 |
} |
|
|
572 |
|
|
|
573 |
/** |
|
|
574 |
* Whether this definition is abstract, that means it merely serves as a |
|
|
575 |
* template for other definitions. |
|
|
576 |
* |
|
|
577 |
* @param Boolean $boolean |
|
|
578 |
* |
|
|
579 |
* @return Definition the current instance |
|
|
580 |
* |
|
|
581 |
* @api |
|
|
582 |
*/ |
|
|
583 |
public function setAbstract($boolean) |
|
|
584 |
{ |
|
|
585 |
$this->abstract = (Boolean) $boolean; |
|
|
586 |
|
|
|
587 |
return $this; |
|
|
588 |
} |
|
|
589 |
|
|
|
590 |
/** |
|
|
591 |
* Whether this definition is abstract, that means it merely serves as a |
|
|
592 |
* template for other definitions. |
|
|
593 |
* |
|
|
594 |
* @return Boolean |
|
|
595 |
* |
|
|
596 |
* @api |
|
|
597 |
*/ |
|
|
598 |
public function isAbstract() |
|
|
599 |
{ |
|
|
600 |
return $this->abstract; |
|
|
601 |
} |
|
|
602 |
|
|
|
603 |
/** |
|
|
604 |
* Sets a configurator to call after the service is fully initialized. |
|
|
605 |
* |
|
|
606 |
* @param mixed $callable A PHP callable |
|
|
607 |
* |
|
|
608 |
* @return Definition The current instance |
|
|
609 |
* |
|
|
610 |
* @api |
|
|
611 |
*/ |
|
|
612 |
public function setConfigurator($callable) |
|
|
613 |
{ |
|
|
614 |
$this->configurator = $callable; |
|
|
615 |
|
|
|
616 |
return $this; |
|
|
617 |
} |
|
|
618 |
|
|
|
619 |
/** |
|
|
620 |
* Gets the configurator to call after the service is fully initialized. |
|
|
621 |
* |
|
|
622 |
* @return mixed The PHP callable to call |
|
|
623 |
* |
|
|
624 |
* @api |
|
|
625 |
*/ |
|
|
626 |
public function getConfigurator() |
|
|
627 |
{ |
|
|
628 |
return $this->configurator; |
|
|
629 |
} |
|
|
630 |
} |