comparison vendor/symfony/dependency-injection/Definition.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
15 use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
16
17 /**
18 * Definition represents a service definition.
19 *
20 * @author Fabien Potencier <fabien@symfony.com>
21 */
22 class Definition
23 {
24 private $class;
25 private $file;
26 private $factory;
27 private $shared = true;
28 private $deprecated = false;
29 private $deprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
30 private $properties = array();
31 private $calls = array();
32 private $configurator;
33 private $tags = array();
34 private $public = true;
35 private $synthetic = false;
36 private $abstract = false;
37 private $lazy = false;
38 private $decoratedService;
39 private $autowired = false;
40 private $autowiringTypes = array();
41
42 protected $arguments;
43
44 /**
45 * @param string|null $class The service class
46 * @param array $arguments An array of arguments to pass to the service constructor
47 */
48 public function __construct($class = null, array $arguments = array())
49 {
50 $this->class = $class;
51 $this->arguments = $arguments;
52 }
53
54 /**
55 * Sets a factory.
56 *
57 * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
58 *
59 * @return $this
60 */
61 public function setFactory($factory)
62 {
63 if (is_string($factory) && strpos($factory, '::') !== false) {
64 $factory = explode('::', $factory, 2);
65 }
66
67 $this->factory = $factory;
68
69 return $this;
70 }
71
72 /**
73 * Gets the factory.
74 *
75 * @return string|array The PHP function or an array containing a class/Reference and a method to call
76 */
77 public function getFactory()
78 {
79 return $this->factory;
80 }
81
82 /**
83 * Sets the service that this service is decorating.
84 *
85 * @param null|string $id The decorated service id, use null to remove decoration
86 * @param null|string $renamedId The new decorated service id
87 * @param int $priority The priority of decoration
88 *
89 * @return $this
90 *
91 * @throws InvalidArgumentException In case the decorated service id and the new decorated service id are equals.
92 */
93 public function setDecoratedService($id, $renamedId = null, $priority = 0)
94 {
95 if ($renamedId && $id == $renamedId) {
96 throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
97 }
98
99 if (null === $id) {
100 $this->decoratedService = null;
101 } else {
102 $this->decoratedService = array($id, $renamedId, (int) $priority);
103 }
104
105 return $this;
106 }
107
108 /**
109 * Gets the service that this service is decorating.
110 *
111 * @return null|array An array composed of the decorated service id, the new id for it and the priority of decoration, null if no service is decorated
112 */
113 public function getDecoratedService()
114 {
115 return $this->decoratedService;
116 }
117
118 /**
119 * Sets the service class.
120 *
121 * @param string $class The service class
122 *
123 * @return $this
124 */
125 public function setClass($class)
126 {
127 $this->class = $class;
128
129 return $this;
130 }
131
132 /**
133 * Gets the service class.
134 *
135 * @return string|null The service class
136 */
137 public function getClass()
138 {
139 return $this->class;
140 }
141
142 /**
143 * Sets the arguments to pass to the service constructor/factory method.
144 *
145 * @param array $arguments An array of arguments
146 *
147 * @return $this
148 */
149 public function setArguments(array $arguments)
150 {
151 $this->arguments = $arguments;
152
153 return $this;
154 }
155
156 public function setProperties(array $properties)
157 {
158 $this->properties = $properties;
159
160 return $this;
161 }
162
163 public function getProperties()
164 {
165 return $this->properties;
166 }
167
168 public function setProperty($name, $value)
169 {
170 $this->properties[$name] = $value;
171
172 return $this;
173 }
174
175 /**
176 * Adds an argument to pass to the service constructor/factory method.
177 *
178 * @param mixed $argument An argument
179 *
180 * @return $this
181 */
182 public function addArgument($argument)
183 {
184 $this->arguments[] = $argument;
185
186 return $this;
187 }
188
189 /**
190 * Sets a specific argument.
191 *
192 * @param int $index
193 * @param mixed $argument
194 *
195 * @return $this
196 *
197 * @throws OutOfBoundsException When the replaced argument does not exist
198 */
199 public function replaceArgument($index, $argument)
200 {
201 if (0 === count($this->arguments)) {
202 throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
203 }
204
205 if ($index < 0 || $index > count($this->arguments) - 1) {
206 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
207 }
208
209 $this->arguments[$index] = $argument;
210
211 return $this;
212 }
213
214 /**
215 * Gets the arguments to pass to the service constructor/factory method.
216 *
217 * @return array The array of arguments
218 */
219 public function getArguments()
220 {
221 return $this->arguments;
222 }
223
224 /**
225 * Gets an argument to pass to the service constructor/factory method.
226 *
227 * @param int $index
228 *
229 * @return mixed The argument value
230 *
231 * @throws OutOfBoundsException When the argument does not exist
232 */
233 public function getArgument($index)
234 {
235 if ($index < 0 || $index > count($this->arguments) - 1) {
236 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
237 }
238
239 return $this->arguments[$index];
240 }
241
242 /**
243 * Sets the methods to call after service initialization.
244 *
245 * @param array $calls An array of method calls
246 *
247 * @return $this
248 */
249 public function setMethodCalls(array $calls = array())
250 {
251 $this->calls = array();
252 foreach ($calls as $call) {
253 $this->addMethodCall($call[0], $call[1]);
254 }
255
256 return $this;
257 }
258
259 /**
260 * Adds a method to call after service initialization.
261 *
262 * @param string $method The method name to call
263 * @param array $arguments An array of arguments to pass to the method call
264 *
265 * @return $this
266 *
267 * @throws InvalidArgumentException on empty $method param
268 */
269 public function addMethodCall($method, array $arguments = array())
270 {
271 if (empty($method)) {
272 throw new InvalidArgumentException('Method name cannot be empty.');
273 }
274 $this->calls[] = array($method, $arguments);
275
276 return $this;
277 }
278
279 /**
280 * Removes a method to call after service initialization.
281 *
282 * @param string $method The method name to remove
283 *
284 * @return $this
285 */
286 public function removeMethodCall($method)
287 {
288 foreach ($this->calls as $i => $call) {
289 if ($call[0] === $method) {
290 unset($this->calls[$i]);
291 break;
292 }
293 }
294
295 return $this;
296 }
297
298 /**
299 * Check if the current definition has a given method to call after service initialization.
300 *
301 * @param string $method The method name to search for
302 *
303 * @return bool
304 */
305 public function hasMethodCall($method)
306 {
307 foreach ($this->calls as $call) {
308 if ($call[0] === $method) {
309 return true;
310 }
311 }
312
313 return false;
314 }
315
316 /**
317 * Gets the methods to call after service initialization.
318 *
319 * @return array An array of method calls
320 */
321 public function getMethodCalls()
322 {
323 return $this->calls;
324 }
325
326 /**
327 * Sets tags for this definition.
328 *
329 * @param array $tags
330 *
331 * @return $this
332 */
333 public function setTags(array $tags)
334 {
335 $this->tags = $tags;
336
337 return $this;
338 }
339
340 /**
341 * Returns all tags.
342 *
343 * @return array An array of tags
344 */
345 public function getTags()
346 {
347 return $this->tags;
348 }
349
350 /**
351 * Gets a tag by name.
352 *
353 * @param string $name The tag name
354 *
355 * @return array An array of attributes
356 */
357 public function getTag($name)
358 {
359 return isset($this->tags[$name]) ? $this->tags[$name] : array();
360 }
361
362 /**
363 * Adds a tag for this definition.
364 *
365 * @param string $name The tag name
366 * @param array $attributes An array of attributes
367 *
368 * @return $this
369 */
370 public function addTag($name, array $attributes = array())
371 {
372 $this->tags[$name][] = $attributes;
373
374 return $this;
375 }
376
377 /**
378 * Whether this definition has a tag with the given name.
379 *
380 * @param string $name
381 *
382 * @return bool
383 */
384 public function hasTag($name)
385 {
386 return isset($this->tags[$name]);
387 }
388
389 /**
390 * Clears all tags for a given name.
391 *
392 * @param string $name The tag name
393 *
394 * @return $this
395 */
396 public function clearTag($name)
397 {
398 unset($this->tags[$name]);
399
400 return $this;
401 }
402
403 /**
404 * Clears the tags for this definition.
405 *
406 * @return $this
407 */
408 public function clearTags()
409 {
410 $this->tags = array();
411
412 return $this;
413 }
414
415 /**
416 * Sets a file to require before creating the service.
417 *
418 * @param string $file A full pathname to include
419 *
420 * @return $this
421 */
422 public function setFile($file)
423 {
424 $this->file = $file;
425
426 return $this;
427 }
428
429 /**
430 * Gets the file to require before creating the service.
431 *
432 * @return string|null The full pathname to include
433 */
434 public function getFile()
435 {
436 return $this->file;
437 }
438
439 /**
440 * Sets if the service must be shared or not.
441 *
442 * @param bool $shared Whether the service must be shared or not
443 *
444 * @return $this
445 */
446 public function setShared($shared)
447 {
448 $this->shared = (bool) $shared;
449
450 return $this;
451 }
452
453 /**
454 * Whether this service is shared.
455 *
456 * @return bool
457 */
458 public function isShared()
459 {
460 return $this->shared;
461 }
462
463 /**
464 * Sets the visibility of this service.
465 *
466 * @param bool $boolean
467 *
468 * @return $this
469 */
470 public function setPublic($boolean)
471 {
472 $this->public = (bool) $boolean;
473
474 return $this;
475 }
476
477 /**
478 * Whether this service is public facing.
479 *
480 * @return bool
481 */
482 public function isPublic()
483 {
484 return $this->public;
485 }
486
487 /**
488 * Sets the lazy flag of this service.
489 *
490 * @param bool $lazy
491 *
492 * @return $this
493 */
494 public function setLazy($lazy)
495 {
496 $this->lazy = (bool) $lazy;
497
498 return $this;
499 }
500
501 /**
502 * Whether this service is lazy.
503 *
504 * @return bool
505 */
506 public function isLazy()
507 {
508 return $this->lazy;
509 }
510
511 /**
512 * Sets whether this definition is synthetic, that is not constructed by the
513 * container, but dynamically injected.
514 *
515 * @param bool $boolean
516 *
517 * @return $this
518 */
519 public function setSynthetic($boolean)
520 {
521 $this->synthetic = (bool) $boolean;
522
523 return $this;
524 }
525
526 /**
527 * Whether this definition is synthetic, that is not constructed by the
528 * container, but dynamically injected.
529 *
530 * @return bool
531 */
532 public function isSynthetic()
533 {
534 return $this->synthetic;
535 }
536
537 /**
538 * Whether this definition is abstract, that means it merely serves as a
539 * template for other definitions.
540 *
541 * @param bool $boolean
542 *
543 * @return $this
544 */
545 public function setAbstract($boolean)
546 {
547 $this->abstract = (bool) $boolean;
548
549 return $this;
550 }
551
552 /**
553 * Whether this definition is abstract, that means it merely serves as a
554 * template for other definitions.
555 *
556 * @return bool
557 */
558 public function isAbstract()
559 {
560 return $this->abstract;
561 }
562
563 /**
564 * Whether this definition is deprecated, that means it should not be called
565 * anymore.
566 *
567 * @param bool $status
568 * @param string $template Template message to use if the definition is deprecated
569 *
570 * @return $this
571 *
572 * @throws InvalidArgumentException When the message template is invalid.
573 */
574 public function setDeprecated($status = true, $template = null)
575 {
576 if (null !== $template) {
577 if (preg_match('#[\r\n]|\*/#', $template)) {
578 throw new InvalidArgumentException('Invalid characters found in deprecation template.');
579 }
580
581 if (false === strpos($template, '%service_id%')) {
582 throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
583 }
584
585 $this->deprecationTemplate = $template;
586 }
587
588 $this->deprecated = (bool) $status;
589
590 return $this;
591 }
592
593 /**
594 * Whether this definition is deprecated, that means it should not be called
595 * anymore.
596 *
597 * @return bool
598 */
599 public function isDeprecated()
600 {
601 return $this->deprecated;
602 }
603
604 /**
605 * Message to use if this definition is deprecated.
606 *
607 * @param string $id Service id relying on this definition
608 *
609 * @return string
610 */
611 public function getDeprecationMessage($id)
612 {
613 return str_replace('%service_id%', $id, $this->deprecationTemplate);
614 }
615
616 /**
617 * Sets a configurator to call after the service is fully initialized.
618 *
619 * @param string|array $configurator A PHP callable
620 *
621 * @return $this
622 */
623 public function setConfigurator($configurator)
624 {
625 if (is_string($configurator) && strpos($configurator, '::') !== false) {
626 $configurator = explode('::', $configurator, 2);
627 }
628
629 $this->configurator = $configurator;
630
631 return $this;
632 }
633
634 /**
635 * Gets the configurator to call after the service is fully initialized.
636 *
637 * @return callable|null The PHP callable to call
638 */
639 public function getConfigurator()
640 {
641 return $this->configurator;
642 }
643
644 /**
645 * Sets types that will default to this definition.
646 *
647 * @param string[] $types
648 *
649 * @return $this
650 */
651 public function setAutowiringTypes(array $types)
652 {
653 $this->autowiringTypes = array();
654
655 foreach ($types as $type) {
656 $this->autowiringTypes[$type] = true;
657 }
658
659 return $this;
660 }
661
662 /**
663 * Is the definition autowired?
664 *
665 * @return bool
666 */
667 public function isAutowired()
668 {
669 return $this->autowired;
670 }
671
672 /**
673 * Sets autowired.
674 *
675 * @param bool $autowired
676 *
677 * @return $this
678 */
679 public function setAutowired($autowired)
680 {
681 $this->autowired = $autowired;
682
683 return $this;
684 }
685
686 /**
687 * Gets autowiring types that will default to this definition.
688 *
689 * @return string[]
690 */
691 public function getAutowiringTypes()
692 {
693 return array_keys($this->autowiringTypes);
694 }
695
696 /**
697 * Adds a type that will default to this definition.
698 *
699 * @param string $type
700 *
701 * @return $this
702 */
703 public function addAutowiringType($type)
704 {
705 $this->autowiringTypes[$type] = true;
706
707 return $this;
708 }
709
710 /**
711 * Removes a type.
712 *
713 * @param string $type
714 *
715 * @return $this
716 */
717 public function removeAutowiringType($type)
718 {
719 unset($this->autowiringTypes[$type]);
720
721 return $this;
722 }
723
724 /**
725 * Will this definition default for the given type?
726 *
727 * @param string $type
728 *
729 * @return bool
730 */
731 public function hasAutowiringType($type)
732 {
733 return isset($this->autowiringTypes[$type]);
734 }
735 }