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\Exception\InvalidArgumentException;
15 use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
18 * Definition represents a service definition.
20 * @author Fabien Potencier <fabien@symfony.com>
27 private $factoryClass;
28 private $factoryMethod;
29 private $factoryService;
30 private $shared = true;
31 private $deprecated = false;
32 private $deprecationTemplate;
33 private $scope = ContainerInterface::SCOPE_CONTAINER;
34 private $properties = array();
35 private $calls = array();
36 private $configurator;
37 private $tags = array();
38 private $public = true;
39 private $synthetic = false;
40 private $abstract = false;
41 private $synchronized = false;
42 private $lazy = false;
43 private $decoratedService;
44 private $autowired = false;
45 private $autowiringTypes = array();
47 private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
52 * @param string|null $class The service class
53 * @param array $arguments An array of arguments to pass to the service constructor
55 public function __construct($class = null, array $arguments = array())
57 $this->class = $class;
58 $this->arguments = $arguments;
64 * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
68 public function setFactory($factory)
70 if (is_string($factory) && strpos($factory, '::') !== false) {
71 $factory = explode('::', $factory, 2);
74 $this->factory = $factory;
82 * @return string|array The PHP function or an array containing a class/Reference and a method to call
84 public function getFactory()
86 return $this->factory;
90 * Sets the name of the class that acts as a factory using the factory method,
91 * which will be invoked statically.
93 * @param string $factoryClass The factory class name
97 * @deprecated since version 2.6, to be removed in 3.0.
99 public function setFactoryClass($factoryClass)
101 @trigger_error(sprintf('%s(%s) is deprecated since version 2.6 and will be removed in 3.0. Use Definition::setFactory() instead.', __METHOD__, $factoryClass), E_USER_DEPRECATED);
103 $this->factoryClass = $factoryClass;
109 * Gets the factory class.
111 * @return string|null The factory class name
113 * @deprecated since version 2.6, to be removed in 3.0.
115 public function getFactoryClass($triggerDeprecationError = true)
117 if ($triggerDeprecationError) {
118 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
121 return $this->factoryClass;
125 * Sets the factory method able to create an instance of this class.
127 * @param string $factoryMethod The factory method name
131 * @deprecated since version 2.6, to be removed in 3.0.
133 public function setFactoryMethod($factoryMethod)
135 @trigger_error(sprintf('%s(%s) is deprecated since version 2.6 and will be removed in 3.0. Use Definition::setFactory() instead.', __METHOD__, $factoryMethod), E_USER_DEPRECATED);
137 $this->factoryMethod = $factoryMethod;
143 * Sets the service that this service is decorating.
145 * @param null|string $id The decorated service id, use null to remove decoration
146 * @param null|string $renamedId The new decorated service id
147 * @param int $priority The priority of decoration
151 * @throws InvalidArgumentException In case the decorated service id and the new decorated service id are equals.
153 public function setDecoratedService($id, $renamedId = null, $priority = 0)
155 if ($renamedId && $id == $renamedId) {
156 throw new \InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
160 $this->decoratedService = null;
162 $this->decoratedService = array($id, $renamedId, (int) $priority);
169 * Gets the service that this service is decorating.
171 * @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
173 public function getDecoratedService()
175 return $this->decoratedService;
179 * Gets the factory method.
181 * @return string|null The factory method name
183 * @deprecated since version 2.6, to be removed in 3.0.
185 public function getFactoryMethod($triggerDeprecationError = true)
187 if ($triggerDeprecationError) {
188 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
191 return $this->factoryMethod;
195 * Sets the name of the service that acts as a factory using the factory method.
197 * @param string $factoryService The factory service id
201 * @deprecated since version 2.6, to be removed in 3.0.
203 public function setFactoryService($factoryService, $triggerDeprecationError = true)
205 if ($triggerDeprecationError) {
206 @trigger_error(sprintf('%s(%s) is deprecated since version 2.6 and will be removed in 3.0. Use Definition::setFactory() instead.', __METHOD__, $factoryService), E_USER_DEPRECATED);
209 $this->factoryService = $factoryService;
215 * Gets the factory service id.
217 * @return string|null The factory service id
219 * @deprecated since version 2.6, to be removed in 3.0.
221 public function getFactoryService($triggerDeprecationError = true)
223 if ($triggerDeprecationError) {
224 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
227 return $this->factoryService;
231 * Sets the service class.
233 * @param string $class The service class
237 public function setClass($class)
239 $this->class = $class;
245 * Gets the service class.
247 * @return string|null The service class
249 public function getClass()
255 * Sets the arguments to pass to the service constructor/factory method.
257 * @param array $arguments An array of arguments
261 public function setArguments(array $arguments)
263 $this->arguments = $arguments;
268 public function setProperties(array $properties)
270 $this->properties = $properties;
275 public function getProperties()
277 return $this->properties;
280 public function setProperty($name, $value)
282 $this->properties[$name] = $value;
288 * Adds an argument to pass to the service constructor/factory method.
290 * @param mixed $argument An argument
294 public function addArgument($argument)
296 $this->arguments[] = $argument;
302 * Sets a specific argument.
305 * @param mixed $argument
309 * @throws OutOfBoundsException When the replaced argument does not exist
311 public function replaceArgument($index, $argument)
313 if (0 === count($this->arguments)) {
314 throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
317 if ($index < 0 || $index > count($this->arguments) - 1) {
318 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
321 $this->arguments[$index] = $argument;
327 * Gets the arguments to pass to the service constructor/factory method.
329 * @return array The array of arguments
331 public function getArguments()
333 return $this->arguments;
337 * Gets an argument to pass to the service constructor/factory method.
341 * @return mixed The argument value
343 * @throws OutOfBoundsException When the argument does not exist
345 public function getArgument($index)
347 if ($index < 0 || $index > count($this->arguments) - 1) {
348 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
351 return $this->arguments[$index];
355 * Sets the methods to call after service initialization.
357 * @param array $calls An array of method calls
361 public function setMethodCalls(array $calls = array())
363 $this->calls = array();
364 foreach ($calls as $call) {
365 $this->addMethodCall($call[0], $call[1]);
372 * Adds a method to call after service initialization.
374 * @param string $method The method name to call
375 * @param array $arguments An array of arguments to pass to the method call
379 * @throws InvalidArgumentException on empty $method param
381 public function addMethodCall($method, array $arguments = array())
383 if (empty($method)) {
384 throw new InvalidArgumentException('Method name cannot be empty.');
386 $this->calls[] = array($method, $arguments);
392 * Removes a method to call after service initialization.
394 * @param string $method The method name to remove
398 public function removeMethodCall($method)
400 foreach ($this->calls as $i => $call) {
401 if ($call[0] === $method) {
402 unset($this->calls[$i]);
411 * Check if the current definition has a given method to call after service initialization.
413 * @param string $method The method name to search for
417 public function hasMethodCall($method)
419 foreach ($this->calls as $call) {
420 if ($call[0] === $method) {
429 * Gets the methods to call after service initialization.
431 * @return array An array of method calls
433 public function getMethodCalls()
439 * Sets tags for this definition.
445 public function setTags(array $tags)
455 * @return array An array of tags
457 public function getTags()
463 * Gets a tag by name.
465 * @param string $name The tag name
467 * @return array An array of attributes
469 public function getTag($name)
471 return isset($this->tags[$name]) ? $this->tags[$name] : array();
475 * Adds a tag for this definition.
477 * @param string $name The tag name
478 * @param array $attributes An array of attributes
482 public function addTag($name, array $attributes = array())
484 $this->tags[$name][] = $attributes;
490 * Whether this definition has a tag with the given name.
492 * @param string $name
496 public function hasTag($name)
498 return isset($this->tags[$name]);
502 * Clears all tags for a given name.
504 * @param string $name The tag name
508 public function clearTag($name)
510 unset($this->tags[$name]);
516 * Clears the tags for this definition.
520 public function clearTags()
522 $this->tags = array();
528 * Sets a file to require before creating the service.
530 * @param string $file A full pathname to include
534 public function setFile($file)
542 * Gets the file to require before creating the service.
544 * @return string|null The full pathname to include
546 public function getFile()
552 * Sets if the service must be shared or not.
554 * @param bool $shared Whether the service must be shared or not
558 public function setShared($shared)
560 $this->shared = (bool) $shared;
566 * Whether this service is shared.
570 public function isShared()
572 return $this->shared;
576 * Sets the scope of the service.
578 * @param string $scope Whether the service must be shared or not
582 * @deprecated since version 2.8, to be removed in 3.0.
584 public function setScope($scope, $triggerDeprecationError = true)
586 if ($triggerDeprecationError) {
587 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
590 if (ContainerInterface::SCOPE_PROTOTYPE === $scope) {
591 $this->setShared(false);
594 $this->scope = $scope;
600 * Returns the scope of the service.
604 * @deprecated since version 2.8, to be removed in 3.0.
606 public function getScope($triggerDeprecationError = true)
608 if ($triggerDeprecationError) {
609 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
616 * Sets the visibility of this service.
618 * @param bool $boolean
622 public function setPublic($boolean)
624 $this->public = (bool) $boolean;
630 * Whether this service is public facing.
634 public function isPublic()
636 return $this->public;
640 * Sets the synchronized flag of this service.
642 * @param bool $boolean
646 * @deprecated since version 2.7, will be removed in 3.0.
648 public function setSynchronized($boolean, $triggerDeprecationError = true)
650 if ($triggerDeprecationError) {
651 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
654 $this->synchronized = (bool) $boolean;
660 * Whether this service is synchronized.
664 * @deprecated since version 2.7, will be removed in 3.0.
666 public function isSynchronized($triggerDeprecationError = true)
668 if ($triggerDeprecationError) {
669 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
672 return $this->synchronized;
676 * Sets the lazy flag of this service.
682 public function setLazy($lazy)
684 $this->lazy = (bool) $lazy;
690 * Whether this service is lazy.
694 public function isLazy()
700 * Sets whether this definition is synthetic, that is not constructed by the
701 * container, but dynamically injected.
703 * @param bool $boolean
707 public function setSynthetic($boolean)
709 $this->synthetic = (bool) $boolean;
715 * Whether this definition is synthetic, that is not constructed by the
716 * container, but dynamically injected.
720 public function isSynthetic()
722 return $this->synthetic;
726 * Whether this definition is abstract, that means it merely serves as a
727 * template for other definitions.
729 * @param bool $boolean
733 public function setAbstract($boolean)
735 $this->abstract = (bool) $boolean;
741 * Whether this definition is abstract, that means it merely serves as a
742 * template for other definitions.
746 public function isAbstract()
748 return $this->abstract;
752 * Whether this definition is deprecated, that means it should not be called
755 * @param bool $status
756 * @param string $template Template message to use if the definition is deprecated
760 * @throws InvalidArgumentException When the message template is invalid.
762 public function setDeprecated($status = true, $template = null)
764 if (null !== $template) {
765 if (preg_match('#[\r\n]|\*/#', $template)) {
766 throw new InvalidArgumentException('Invalid characters found in deprecation template.');
769 if (false === strpos($template, '%service_id%')) {
770 throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
773 $this->deprecationTemplate = $template;
776 $this->deprecated = (bool) $status;
782 * Whether this definition is deprecated, that means it should not be called
787 public function isDeprecated()
789 return $this->deprecated;
793 * Message to use if this definition is deprecated.
795 * @param string $id Service id relying on this definition
799 public function getDeprecationMessage($id)
801 return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
805 * Sets a configurator to call after the service is fully initialized.
807 * @param callable $callable A PHP callable
811 public function setConfigurator($callable)
813 $this->configurator = $callable;
819 * Gets the configurator to call after the service is fully initialized.
821 * @return callable|null The PHP callable to call
823 public function getConfigurator()
825 return $this->configurator;
829 * Sets types that will default to this definition.
831 * @param string[] $types
835 public function setAutowiringTypes(array $types)
837 $this->autowiringTypes = array();
839 foreach ($types as $type) {
840 $this->autowiringTypes[$type] = true;
847 * Is the definition autowired?
851 public function isAutowired()
853 return $this->autowired;
859 * @param bool $autowired
863 public function setAutowired($autowired)
865 $this->autowired = $autowired;
871 * Gets autowiring types that will default to this definition.
875 public function getAutowiringTypes()
877 return array_keys($this->autowiringTypes);
881 * Adds a type that will default to this definition.
883 * @param string $type
887 public function addAutowiringType($type)
889 $this->autowiringTypes[$type] = true;
897 * @param string $type
901 public function removeAutowiringType($type)
903 unset($this->autowiringTypes[$type]);
909 * Will this definition default for the given type?
911 * @param string $type
915 public function hasAutowiringType($type)
917 return isset($this->autowiringTypes[$type]);