4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection;
14 use Symfony\Component\DependencyInjection\Argument\BoundArgument;
15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16 use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
19 * Definition represents a service definition.
21 * @author Fabien Potencier <fabien@symfony.com>
28 private $shared = true;
29 private $deprecated = false;
30 private $deprecationTemplate;
31 private $properties = array();
32 private $calls = array();
33 private $instanceof = array();
34 private $autoconfigured = false;
35 private $configurator;
36 private $tags = array();
37 private $public = true;
38 private $private = true;
39 private $synthetic = false;
40 private $abstract = false;
41 private $lazy = false;
42 private $decoratedService;
43 private $autowired = false;
44 private $autowiringTypes = array();
45 private $changes = array();
46 private $bindings = array();
47 private $errors = array();
49 protected $arguments = array();
51 private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
54 * @param string|null $class The service class
55 * @param array $arguments An array of arguments to pass to the service constructor
57 public function __construct($class = null, array $arguments = array())
59 if (null !== $class) {
60 $this->setClass($class);
62 $this->arguments = $arguments;
66 * Returns all changes tracked for the Definition object.
68 * @return array An array of changes for this Definition
70 public function getChanges()
72 return $this->changes;
76 * Sets the tracked changes for the Definition object.
78 * @param array $changes An array of changes for this Definition
82 public function setChanges(array $changes)
84 $this->changes = $changes;
92 * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
96 public function setFactory($factory)
98 $this->changes['factory'] = true;
100 if (\is_string($factory) && false !== strpos($factory, '::')) {
101 $factory = explode('::', $factory, 2);
104 $this->factory = $factory;
112 * @return string|array|null The PHP function or an array containing a class/Reference and a method to call
114 public function getFactory()
116 return $this->factory;
120 * Sets the service that this service is decorating.
122 * @param string|null $id The decorated service id, use null to remove decoration
123 * @param string|null $renamedId The new decorated service id
124 * @param int $priority The priority of decoration
128 * @throws InvalidArgumentException in case the decorated service id and the new decorated service id are equals
130 public function setDecoratedService($id, $renamedId = null, $priority = 0)
132 if ($renamedId && $id === $renamedId) {
133 throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
136 $this->changes['decorated_service'] = true;
139 $this->decoratedService = null;
141 $this->decoratedService = array($id, $renamedId, (int) $priority);
148 * Gets the service that this service is decorating.
150 * @return array|null An array composed of the decorated service id, the new id for it and the priority of decoration, null if no service is decorated
152 public function getDecoratedService()
154 return $this->decoratedService;
158 * Sets the service class.
160 * @param string $class The service class
164 public function setClass($class)
166 $this->changes['class'] = true;
168 $this->class = $class;
174 * Gets the service class.
176 * @return string|null The service class
178 public function getClass()
184 * Sets the arguments to pass to the service constructor/factory method.
188 public function setArguments(array $arguments)
190 $this->arguments = $arguments;
196 * Sets the properties to define when creating the service.
200 public function setProperties(array $properties)
202 $this->properties = $properties;
208 * Gets the properties to define when creating the service.
212 public function getProperties()
214 return $this->properties;
218 * Sets a specific property.
220 * @param string $name
221 * @param mixed $value
225 public function setProperty($name, $value)
227 $this->properties[$name] = $value;
233 * Adds an argument to pass to the service constructor/factory method.
235 * @param mixed $argument An argument
239 public function addArgument($argument)
241 $this->arguments[] = $argument;
247 * Replaces a specific argument.
249 * @param int|string $index
250 * @param mixed $argument
254 * @throws OutOfBoundsException When the replaced argument does not exist
256 public function replaceArgument($index, $argument)
258 if (0 === \count($this->arguments)) {
259 throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
262 if (\is_int($index) && ($index < 0 || $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));
266 if (!array_key_exists($index, $this->arguments)) {
267 throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
270 $this->arguments[$index] = $argument;
276 * Sets a specific argument.
278 * @param int|string $key
279 * @param mixed $value
283 public function setArgument($key, $value)
285 $this->arguments[$key] = $value;
291 * Gets the arguments to pass to the service constructor/factory method.
293 * @return array The array of arguments
295 public function getArguments()
297 return $this->arguments;
301 * Gets an argument to pass to the service constructor/factory method.
303 * @param int|string $index
305 * @return mixed The argument value
307 * @throws OutOfBoundsException When the argument does not exist
309 public function getArgument($index)
311 if (!array_key_exists($index, $this->arguments)) {
312 throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
315 return $this->arguments[$index];
319 * Sets the methods to call after service initialization.
323 public function setMethodCalls(array $calls = array())
325 $this->calls = array();
326 foreach ($calls as $call) {
327 $this->addMethodCall($call[0], $call[1]);
334 * Adds a method to call after service initialization.
336 * @param string $method The method name to call
337 * @param array $arguments An array of arguments to pass to the method call
341 * @throws InvalidArgumentException on empty $method param
343 public function addMethodCall($method, array $arguments = array())
345 if (empty($method)) {
346 throw new InvalidArgumentException('Method name cannot be empty.');
348 $this->calls[] = array($method, $arguments);
354 * Removes a method to call after service initialization.
356 * @param string $method The method name to remove
360 public function removeMethodCall($method)
362 foreach ($this->calls as $i => $call) {
363 if ($call[0] === $method) {
364 unset($this->calls[$i]);
373 * Check if the current definition has a given method to call after service initialization.
375 * @param string $method The method name to search for
379 public function hasMethodCall($method)
381 foreach ($this->calls as $call) {
382 if ($call[0] === $method) {
391 * Gets the methods to call after service initialization.
393 * @return array An array of method calls
395 public function getMethodCalls()
401 * Sets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
403 * @param $instanceof ChildDefinition[]
407 public function setInstanceofConditionals(array $instanceof)
409 $this->instanceof = $instanceof;
415 * Gets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
417 * @return ChildDefinition[]
419 public function getInstanceofConditionals()
421 return $this->instanceof;
425 * Sets whether or not instanceof conditionals should be prepended with a global set.
427 * @param bool $autoconfigured
431 public function setAutoconfigured($autoconfigured)
433 $this->changes['autoconfigured'] = true;
435 $this->autoconfigured = $autoconfigured;
443 public function isAutoconfigured()
445 return $this->autoconfigured;
449 * Sets tags for this definition.
453 public function setTags(array $tags)
463 * @return array An array of tags
465 public function getTags()
471 * Gets a tag by name.
473 * @param string $name The tag name
475 * @return array An array of attributes
477 public function getTag($name)
479 return isset($this->tags[$name]) ? $this->tags[$name] : array();
483 * Adds a tag for this definition.
485 * @param string $name The tag name
486 * @param array $attributes An array of attributes
490 public function addTag($name, array $attributes = array())
492 $this->tags[$name][] = $attributes;
498 * Whether this definition has a tag with the given name.
500 * @param string $name
504 public function hasTag($name)
506 return isset($this->tags[$name]);
510 * Clears all tags for a given name.
512 * @param string $name The tag name
516 public function clearTag($name)
518 unset($this->tags[$name]);
524 * Clears the tags for this definition.
528 public function clearTags()
530 $this->tags = array();
536 * Sets a file to require before creating the service.
538 * @param string $file A full pathname to include
542 public function setFile($file)
544 $this->changes['file'] = true;
552 * Gets the file to require before creating the service.
554 * @return string|null The full pathname to include
556 public function getFile()
562 * Sets if the service must be shared or not.
564 * @param bool $shared Whether the service must be shared or not
568 public function setShared($shared)
570 $this->changes['shared'] = true;
572 $this->shared = (bool) $shared;
578 * Whether this service is shared.
582 public function isShared()
584 return $this->shared;
588 * Sets the visibility of this service.
590 * @param bool $boolean
594 public function setPublic($boolean)
596 $this->changes['public'] = true;
598 $this->public = (bool) $boolean;
599 $this->private = false;
605 * Whether this service is public facing.
609 public function isPublic()
611 return $this->public;
615 * Sets if this service is private.
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.
622 * @param bool $boolean
626 public function setPrivate($boolean)
628 $this->private = (bool) $boolean;
634 * Whether this service is private.
638 public function isPrivate()
640 return $this->private;
644 * Sets the lazy flag of this service.
650 public function setLazy($lazy)
652 $this->changes['lazy'] = true;
654 $this->lazy = (bool) $lazy;
660 * Whether this service is lazy.
664 public function isLazy()
670 * Sets whether this definition is synthetic, that is not constructed by the
671 * container, but dynamically injected.
673 * @param bool $boolean
677 public function setSynthetic($boolean)
679 $this->synthetic = (bool) $boolean;
685 * Whether this definition is synthetic, that is not constructed by the
686 * container, but dynamically injected.
690 public function isSynthetic()
692 return $this->synthetic;
696 * Whether this definition is abstract, that means it merely serves as a
697 * template for other definitions.
699 * @param bool $boolean
703 public function setAbstract($boolean)
705 $this->abstract = (bool) $boolean;
711 * Whether this definition is abstract, that means it merely serves as a
712 * template for other definitions.
716 public function isAbstract()
718 return $this->abstract;
722 * Whether this definition is deprecated, that means it should not be called
725 * @param bool $status
726 * @param string $template Template message to use if the definition is deprecated
730 * @throws InvalidArgumentException when the message template is invalid
732 public function setDeprecated($status = true, $template = null)
734 if (null !== $template) {
735 if (preg_match('#[\r\n]|\*/#', $template)) {
736 throw new InvalidArgumentException('Invalid characters found in deprecation template.');
739 if (false === strpos($template, '%service_id%')) {
740 throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
743 $this->deprecationTemplate = $template;
746 $this->changes['deprecated'] = true;
748 $this->deprecated = (bool) $status;
754 * Whether this definition is deprecated, that means it should not be called
759 public function isDeprecated()
761 return $this->deprecated;
765 * Message to use if this definition is deprecated.
767 * @param string $id Service id relying on this definition
771 public function getDeprecationMessage($id)
773 return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
777 * Sets a configurator to call after the service is fully initialized.
779 * @param string|array $configurator A PHP callable
783 public function setConfigurator($configurator)
785 $this->changes['configurator'] = true;
787 if (\is_string($configurator) && false !== strpos($configurator, '::')) {
788 $configurator = explode('::', $configurator, 2);
791 $this->configurator = $configurator;
797 * Gets the configurator to call after the service is fully initialized.
799 * @return callable|null The PHP callable to call
801 public function getConfigurator()
803 return $this->configurator;
807 * Sets types that will default to this definition.
809 * @param string[] $types
813 * @deprecated since version 3.3, to be removed in 4.0.
815 public function setAutowiringTypes(array $types)
817 @trigger_error('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead.', E_USER_DEPRECATED);
819 $this->autowiringTypes = array();
821 foreach ($types as $type) {
822 $this->autowiringTypes[$type] = true;
829 * Is the definition autowired?
833 public function isAutowired()
835 return $this->autowired;
839 * Enables/disables autowiring.
841 * @param bool $autowired
845 public function setAutowired($autowired)
847 $this->changes['autowired'] = true;
849 $this->autowired = (bool) $autowired;
855 * Gets autowiring types that will default to this definition.
859 * @deprecated since version 3.3, to be removed in 4.0.
861 public function getAutowiringTypes(/*$triggerDeprecation = true*/)
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);
867 return array_keys($this->autowiringTypes);
871 * Adds a type that will default to this definition.
873 * @param string $type
877 * @deprecated since version 3.3, to be removed in 4.0.
879 public function addAutowiringType($type)
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);
883 $this->autowiringTypes[$type] = true;
891 * @param string $type
895 * @deprecated since version 3.3, to be removed in 4.0.
897 public function removeAutowiringType($type)
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);
901 unset($this->autowiringTypes[$type]);
907 * Will this definition default for the given type?
909 * @param string $type
913 * @deprecated since version 3.3, to be removed in 4.0.
915 public function hasAutowiringType($type)
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);
919 return isset($this->autowiringTypes[$type]);
927 public function getBindings()
929 return $this->bindings;
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).
939 * @param array $bindings
943 public function setBindings(array $bindings)
945 foreach ($bindings as $key => $binding) {
946 if (!$binding instanceof BoundArgument) {
947 $bindings[$key] = new BoundArgument($binding);
951 $this->bindings = $bindings;
957 * Add an error that occurred when building this Definition.
959 * @param string $error
961 public function addError($error)
963 $this->errors[] = $error;
967 * Returns any errors that occurred while building this Definition.
971 public function getErrors()
973 return $this->errors;