comparison vendor/symfony/dependency-injection/Definition.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Symfony\Component\DependencyInjection; 12 namespace Symfony\Component\DependencyInjection;
13 13
14 use Symfony\Component\DependencyInjection\Argument\BoundArgument;
14 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; 15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
15 use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException; 16 use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
16 17
17 /** 18 /**
18 * Definition represents a service definition. 19 * Definition represents a service definition.
27 private $shared = true; 28 private $shared = true;
28 private $deprecated = false; 29 private $deprecated = false;
29 private $deprecationTemplate; 30 private $deprecationTemplate;
30 private $properties = array(); 31 private $properties = array();
31 private $calls = array(); 32 private $calls = array();
33 private $instanceof = array();
34 private $autoconfigured = false;
32 private $configurator; 35 private $configurator;
33 private $tags = array(); 36 private $tags = array();
34 private $public = true; 37 private $public = true;
38 private $private = true;
35 private $synthetic = false; 39 private $synthetic = false;
36 private $abstract = false; 40 private $abstract = false;
37 private $lazy = false; 41 private $lazy = false;
38 private $decoratedService; 42 private $decoratedService;
39 private $autowired = false; 43 private $autowired = false;
40 private $autowiringTypes = array(); 44 private $autowiringTypes = array();
45 private $changes = array();
46 private $bindings = array();
47 private $errors = array();
48
49 protected $arguments = array();
41 50
42 private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.'; 51 private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
43
44 protected $arguments;
45 52
46 /** 53 /**
47 * @param string|null $class The service class 54 * @param string|null $class The service class
48 * @param array $arguments An array of arguments to pass to the service constructor 55 * @param array $arguments An array of arguments to pass to the service constructor
49 */ 56 */
50 public function __construct($class = null, array $arguments = array()) 57 public function __construct($class = null, array $arguments = array())
51 { 58 {
52 $this->class = $class; 59 if (null !== $class) {
60 $this->setClass($class);
61 }
53 $this->arguments = $arguments; 62 $this->arguments = $arguments;
54 } 63 }
55 64
56 /** 65 /**
66 * Returns all changes tracked for the Definition object.
67 *
68 * @return array An array of changes for this Definition
69 */
70 public function getChanges()
71 {
72 return $this->changes;
73 }
74
75 /**
76 * Sets the tracked changes for the Definition object.
77 *
78 * @param array $changes An array of changes for this Definition
79 *
80 * @return $this
81 */
82 public function setChanges(array $changes)
83 {
84 $this->changes = $changes;
85
86 return $this;
87 }
88
89 /**
57 * Sets a factory. 90 * Sets a factory.
58 * 91 *
59 * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call 92 * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
60 * 93 *
61 * @return $this 94 * @return $this
62 */ 95 */
63 public function setFactory($factory) 96 public function setFactory($factory)
64 { 97 {
65 if (is_string($factory) && strpos($factory, '::') !== false) { 98 $this->changes['factory'] = true;
99
100 if (is_string($factory) && false !== strpos($factory, '::')) {
66 $factory = explode('::', $factory, 2); 101 $factory = explode('::', $factory, 2);
67 } 102 }
68 103
69 $this->factory = $factory; 104 $this->factory = $factory;
70 105
88 * @param null|string $renamedId The new decorated service id 123 * @param null|string $renamedId The new decorated service id
89 * @param int $priority The priority of decoration 124 * @param int $priority The priority of decoration
90 * 125 *
91 * @return $this 126 * @return $this
92 * 127 *
93 * @throws InvalidArgumentException In case the decorated service id and the new decorated service id are equals. 128 * @throws InvalidArgumentException in case the decorated service id and the new decorated service id are equals
94 */ 129 */
95 public function setDecoratedService($id, $renamedId = null, $priority = 0) 130 public function setDecoratedService($id, $renamedId = null, $priority = 0)
96 { 131 {
97 if ($renamedId && $id == $renamedId) { 132 if ($renamedId && $id === $renamedId) {
98 throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id)); 133 throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
99 } 134 }
135
136 $this->changes['decorated_service'] = true;
100 137
101 if (null === $id) { 138 if (null === $id) {
102 $this->decoratedService = null; 139 $this->decoratedService = null;
103 } else { 140 } else {
104 $this->decoratedService = array($id, $renamedId, (int) $priority); 141 $this->decoratedService = array($id, $renamedId, (int) $priority);
124 * 161 *
125 * @return $this 162 * @return $this
126 */ 163 */
127 public function setClass($class) 164 public function setClass($class)
128 { 165 {
166 $this->changes['class'] = true;
167
129 $this->class = $class; 168 $this->class = $class;
130 169
131 return $this; 170 return $this;
132 } 171 }
133 172
142 } 181 }
143 182
144 /** 183 /**
145 * Sets the arguments to pass to the service constructor/factory method. 184 * Sets the arguments to pass to the service constructor/factory method.
146 * 185 *
147 * @param array $arguments An array of arguments
148 *
149 * @return $this 186 * @return $this
150 */ 187 */
151 public function setArguments(array $arguments) 188 public function setArguments(array $arguments)
152 { 189 {
153 $this->arguments = $arguments; 190 $this->arguments = $arguments;
154 191
155 return $this; 192 return $this;
156 } 193 }
157 194
195 /**
196 * Sets the properties to define when creating the service.
197 *
198 * @return $this
199 */
158 public function setProperties(array $properties) 200 public function setProperties(array $properties)
159 { 201 {
160 $this->properties = $properties; 202 $this->properties = $properties;
161 203
162 return $this; 204 return $this;
163 } 205 }
164 206
207 /**
208 * Gets the properties to define when creating the service.
209 *
210 * @return array
211 */
165 public function getProperties() 212 public function getProperties()
166 { 213 {
167 return $this->properties; 214 return $this->properties;
168 } 215 }
169 216
217 /**
218 * Sets a specific property.
219 *
220 * @param string $name
221 * @param mixed $value
222 *
223 * @return $this
224 */
170 public function setProperty($name, $value) 225 public function setProperty($name, $value)
171 { 226 {
172 $this->properties[$name] = $value; 227 $this->properties[$name] = $value;
173 228
174 return $this; 229 return $this;
187 242
188 return $this; 243 return $this;
189 } 244 }
190 245
191 /** 246 /**
192 * Sets a specific argument. 247 * Replaces a specific argument.
193 * 248 *
194 * @param int $index 249 * @param int|string $index
195 * @param mixed $argument 250 * @param mixed $argument
196 * 251 *
197 * @return $this 252 * @return $this
198 * 253 *
199 * @throws OutOfBoundsException When the replaced argument does not exist 254 * @throws OutOfBoundsException When the replaced argument does not exist
200 */ 255 */
202 { 257 {
203 if (0 === count($this->arguments)) { 258 if (0 === count($this->arguments)) {
204 throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.'); 259 throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
205 } 260 }
206 261
207 if ($index < 0 || $index > count($this->arguments) - 1) { 262 if (is_int($index) && ($index < 0 || $index > count($this->arguments) - 1)) {
208 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1)); 263 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
209 } 264 }
210 265
266 if (!array_key_exists($index, $this->arguments)) {
267 throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
268 }
269
211 $this->arguments[$index] = $argument; 270 $this->arguments[$index] = $argument;
212 271
213 return $this; 272 return $this;
214 } 273 }
215 274
216 /** 275 /**
276 * Sets a specific argument.
277 *
278 * @param int|string $key
279 * @param mixed $value
280 *
281 * @return $this
282 */
283 public function setArgument($key, $value)
284 {
285 $this->arguments[$key] = $value;
286
287 return $this;
288 }
289
290 /**
217 * Gets the arguments to pass to the service constructor/factory method. 291 * Gets the arguments to pass to the service constructor/factory method.
218 * 292 *
219 * @return array The array of arguments 293 * @return array The array of arguments
220 */ 294 */
221 public function getArguments() 295 public function getArguments()
224 } 298 }
225 299
226 /** 300 /**
227 * Gets an argument to pass to the service constructor/factory method. 301 * Gets an argument to pass to the service constructor/factory method.
228 * 302 *
229 * @param int $index 303 * @param int|string $index
230 * 304 *
231 * @return mixed The argument value 305 * @return mixed The argument value
232 * 306 *
233 * @throws OutOfBoundsException When the argument does not exist 307 * @throws OutOfBoundsException When the argument does not exist
234 */ 308 */
235 public function getArgument($index) 309 public function getArgument($index)
236 { 310 {
237 if ($index < 0 || $index > count($this->arguments) - 1) { 311 if (!array_key_exists($index, $this->arguments)) {
238 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1)); 312 throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
239 } 313 }
240 314
241 return $this->arguments[$index]; 315 return $this->arguments[$index];
242 } 316 }
243 317
244 /** 318 /**
245 * Sets the methods to call after service initialization. 319 * Sets the methods to call after service initialization.
246 *
247 * @param array $calls An array of method calls
248 * 320 *
249 * @return $this 321 * @return $this
250 */ 322 */
251 public function setMethodCalls(array $calls = array()) 323 public function setMethodCalls(array $calls = array())
252 { 324 {
324 { 396 {
325 return $this->calls; 397 return $this->calls;
326 } 398 }
327 399
328 /** 400 /**
401 * Sets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
402 *
403 * @param $instanceof ChildDefinition[]
404 *
405 * @return $this
406 */
407 public function setInstanceofConditionals(array $instanceof)
408 {
409 $this->instanceof = $instanceof;
410
411 return $this;
412 }
413
414 /**
415 * Gets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
416 *
417 * @return ChildDefinition[]
418 */
419 public function getInstanceofConditionals()
420 {
421 return $this->instanceof;
422 }
423
424 /**
425 * Sets whether or not instanceof conditionals should be prepended with a global set.
426 *
427 * @param bool $autoconfigured
428 *
429 * @return $this
430 */
431 public function setAutoconfigured($autoconfigured)
432 {
433 $this->changes['autoconfigured'] = true;
434
435 $this->autoconfigured = $autoconfigured;
436
437 return $this;
438 }
439
440 /**
441 * @return bool
442 */
443 public function isAutoconfigured()
444 {
445 return $this->autoconfigured;
446 }
447
448 /**
329 * Sets tags for this definition. 449 * Sets tags for this definition.
330 *
331 * @param array $tags
332 * 450 *
333 * @return $this 451 * @return $this
334 */ 452 */
335 public function setTags(array $tags) 453 public function setTags(array $tags)
336 { 454 {
421 * 539 *
422 * @return $this 540 * @return $this
423 */ 541 */
424 public function setFile($file) 542 public function setFile($file)
425 { 543 {
544 $this->changes['file'] = true;
545
426 $this->file = $file; 546 $this->file = $file;
427 547
428 return $this; 548 return $this;
429 } 549 }
430 550
445 * 565 *
446 * @return $this 566 * @return $this
447 */ 567 */
448 public function setShared($shared) 568 public function setShared($shared)
449 { 569 {
570 $this->changes['shared'] = true;
571
450 $this->shared = (bool) $shared; 572 $this->shared = (bool) $shared;
451 573
452 return $this; 574 return $this;
453 } 575 }
454 576
469 * 591 *
470 * @return $this 592 * @return $this
471 */ 593 */
472 public function setPublic($boolean) 594 public function setPublic($boolean)
473 { 595 {
596 $this->changes['public'] = true;
597
474 $this->public = (bool) $boolean; 598 $this->public = (bool) $boolean;
599 $this->private = false;
475 600
476 return $this; 601 return $this;
477 } 602 }
478 603
479 /** 604 /**
485 { 610 {
486 return $this->public; 611 return $this->public;
487 } 612 }
488 613
489 /** 614 /**
615 * Sets if this service is private.
616 *
617 * When set, the "private" state has a higher precedence than "public".
618 * In version 3.4, a "private" service always remains publicly accessible,
619 * but triggers a deprecation notice when accessed from the container,
620 * so that the service can be made really private in 4.0.
621 *
622 * @param bool $boolean
623 *
624 * @return $this
625 */
626 public function setPrivate($boolean)
627 {
628 $this->private = (bool) $boolean;
629
630 return $this;
631 }
632
633 /**
634 * Whether this service is private.
635 *
636 * @return bool
637 */
638 public function isPrivate()
639 {
640 return $this->private;
641 }
642
643 /**
490 * Sets the lazy flag of this service. 644 * Sets the lazy flag of this service.
491 * 645 *
492 * @param bool $lazy 646 * @param bool $lazy
493 * 647 *
494 * @return $this 648 * @return $this
495 */ 649 */
496 public function setLazy($lazy) 650 public function setLazy($lazy)
497 { 651 {
652 $this->changes['lazy'] = true;
653
498 $this->lazy = (bool) $lazy; 654 $this->lazy = (bool) $lazy;
499 655
500 return $this; 656 return $this;
501 } 657 }
502 658
569 * @param bool $status 725 * @param bool $status
570 * @param string $template Template message to use if the definition is deprecated 726 * @param string $template Template message to use if the definition is deprecated
571 * 727 *
572 * @return $this 728 * @return $this
573 * 729 *
574 * @throws InvalidArgumentException When the message template is invalid. 730 * @throws InvalidArgumentException when the message template is invalid
575 */ 731 */
576 public function setDeprecated($status = true, $template = null) 732 public function setDeprecated($status = true, $template = null)
577 { 733 {
578 if (null !== $template) { 734 if (null !== $template) {
579 if (preg_match('#[\r\n]|\*/#', $template)) { 735 if (preg_match('#[\r\n]|\*/#', $template)) {
585 } 741 }
586 742
587 $this->deprecationTemplate = $template; 743 $this->deprecationTemplate = $template;
588 } 744 }
589 745
746 $this->changes['deprecated'] = true;
747
590 $this->deprecated = (bool) $status; 748 $this->deprecated = (bool) $status;
591 749
592 return $this; 750 return $this;
593 } 751 }
594 752
622 * 780 *
623 * @return $this 781 * @return $this
624 */ 782 */
625 public function setConfigurator($configurator) 783 public function setConfigurator($configurator)
626 { 784 {
627 if (is_string($configurator) && strpos($configurator, '::') !== false) { 785 $this->changes['configurator'] = true;
786
787 if (is_string($configurator) && false !== strpos($configurator, '::')) {
628 $configurator = explode('::', $configurator, 2); 788 $configurator = explode('::', $configurator, 2);
629 } 789 }
630 790
631 $this->configurator = $configurator; 791 $this->configurator = $configurator;
632 792
647 * Sets types that will default to this definition. 807 * Sets types that will default to this definition.
648 * 808 *
649 * @param string[] $types 809 * @param string[] $types
650 * 810 *
651 * @return $this 811 * @return $this
812 *
813 * @deprecated since version 3.3, to be removed in 4.0.
652 */ 814 */
653 public function setAutowiringTypes(array $types) 815 public function setAutowiringTypes(array $types)
654 { 816 {
817 @trigger_error('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead.', E_USER_DEPRECATED);
818
655 $this->autowiringTypes = array(); 819 $this->autowiringTypes = array();
656 820
657 foreach ($types as $type) { 821 foreach ($types as $type) {
658 $this->autowiringTypes[$type] = true; 822 $this->autowiringTypes[$type] = true;
659 } 823 }
670 { 834 {
671 return $this->autowired; 835 return $this->autowired;
672 } 836 }
673 837
674 /** 838 /**
675 * Sets autowired. 839 * Enables/disables autowiring.
676 * 840 *
677 * @param bool $autowired 841 * @param bool $autowired
678 * 842 *
679 * @return $this 843 * @return $this
680 */ 844 */
681 public function setAutowired($autowired) 845 public function setAutowired($autowired)
682 { 846 {
683 $this->autowired = $autowired; 847 $this->changes['autowired'] = true;
848
849 $this->autowired = (bool) $autowired;
684 850
685 return $this; 851 return $this;
686 } 852 }
687 853
688 /** 854 /**
689 * Gets autowiring types that will default to this definition. 855 * Gets autowiring types that will default to this definition.
690 * 856 *
691 * @return string[] 857 * @return string[]
692 */ 858 *
693 public function getAutowiringTypes() 859 * @deprecated since version 3.3, to be removed in 4.0.
694 { 860 */
861 public function getAutowiringTypes(/*$triggerDeprecation = true*/)
862 {
863 if (1 > func_num_args() || func_get_arg(0)) {
864 @trigger_error('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead.', E_USER_DEPRECATED);
865 }
866
695 return array_keys($this->autowiringTypes); 867 return array_keys($this->autowiringTypes);
696 } 868 }
697 869
698 /** 870 /**
699 * Adds a type that will default to this definition. 871 * Adds a type that will default to this definition.
700 * 872 *
701 * @param string $type 873 * @param string $type
702 * 874 *
703 * @return $this 875 * @return $this
876 *
877 * @deprecated since version 3.3, to be removed in 4.0.
704 */ 878 */
705 public function addAutowiringType($type) 879 public function addAutowiringType($type)
706 { 880 {
881 @trigger_error(sprintf('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead for "%s".', $type), E_USER_DEPRECATED);
882
707 $this->autowiringTypes[$type] = true; 883 $this->autowiringTypes[$type] = true;
708 884
709 return $this; 885 return $this;
710 } 886 }
711 887
713 * Removes a type. 889 * Removes a type.
714 * 890 *
715 * @param string $type 891 * @param string $type
716 * 892 *
717 * @return $this 893 * @return $this
894 *
895 * @deprecated since version 3.3, to be removed in 4.0.
718 */ 896 */
719 public function removeAutowiringType($type) 897 public function removeAutowiringType($type)
720 { 898 {
899 @trigger_error(sprintf('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead for "%s".', $type), E_USER_DEPRECATED);
900
721 unset($this->autowiringTypes[$type]); 901 unset($this->autowiringTypes[$type]);
722 902
723 return $this; 903 return $this;
724 } 904 }
725 905
727 * Will this definition default for the given type? 907 * Will this definition default for the given type?
728 * 908 *
729 * @param string $type 909 * @param string $type
730 * 910 *
731 * @return bool 911 * @return bool
912 *
913 * @deprecated since version 3.3, to be removed in 4.0.
732 */ 914 */
733 public function hasAutowiringType($type) 915 public function hasAutowiringType($type)
734 { 916 {
917 @trigger_error(sprintf('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead for "%s".', $type), E_USER_DEPRECATED);
918
735 return isset($this->autowiringTypes[$type]); 919 return isset($this->autowiringTypes[$type]);
736 } 920 }
921
922 /**
923 * Gets bindings.
924 *
925 * @return array
926 */
927 public function getBindings()
928 {
929 return $this->bindings;
930 }
931
932 /**
933 * Sets bindings.
934 *
935 * Bindings map $named or FQCN arguments to values that should be
936 * injected in the matching parameters (of the constructor, of methods
937 * called and of controller actions).
938 *
939 * @param array $bindings
940 *
941 * @return $this
942 */
943 public function setBindings(array $bindings)
944 {
945 foreach ($bindings as $key => $binding) {
946 if (!$binding instanceof BoundArgument) {
947 $bindings[$key] = new BoundArgument($binding);
948 }
949 }
950
951 $this->bindings = $bindings;
952
953 return $this;
954 }
955
956 /**
957 * Add an error that occurred when building this Definition.
958 *
959 * @param string $error
960 */
961 public function addError($error)
962 {
963 $this->errors[] = $error;
964 }
965
966 /**
967 * Returns any errors that occurred while building this Definition.
968 *
969 * @return array
970 */
971 public function getErrors()
972 {
973 return $this->errors;
974 }
737 } 975 }