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 = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
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();
50 * @param string|null $class The service class
51 * @param array $arguments An array of arguments to pass to the service constructor
53 public function __construct($class = null, array $arguments = array())
55 $this->class = $class;
56 $this->arguments = $arguments;
62 * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
66 public function setFactory($factory)
68 if (is_string($factory) && strpos($factory, '::') !== false) {
69 $factory = explode('::', $factory, 2);
72 $this->factory = $factory;
80 * @return string|array The PHP function or an array containing a class/Reference and a method to call
82 public function getFactory()
84 return $this->factory;
88 * Sets the name of the class that acts as a factory using the factory method,
89 * which will be invoked statically.
91 * @param string $factoryClass The factory class name
95 * @deprecated since version 2.6, to be removed in 3.0.
97 public function setFactoryClass($factoryClass)
99 @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);
101 $this->factoryClass = $factoryClass;
107 * Gets the factory class.
109 * @return string|null The factory class name
111 * @deprecated since version 2.6, to be removed in 3.0.
113 public function getFactoryClass($triggerDeprecationError = true)
115 if ($triggerDeprecationError) {
116 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
119 return $this->factoryClass;
123 * Sets the factory method able to create an instance of this class.
125 * @param string $factoryMethod The factory method name
129 * @deprecated since version 2.6, to be removed in 3.0.
131 public function setFactoryMethod($factoryMethod)
133 @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);
135 $this->factoryMethod = $factoryMethod;
141 * Sets the service that this service is decorating.
143 * @param null|string $id The decorated service id, use null to remove decoration
144 * @param null|string $renamedId The new decorated service id
145 * @param int $priority The priority of decoration
149 * @throws InvalidArgumentException In case the decorated service id and the new decorated service id are equals.
151 public function setDecoratedService($id, $renamedId = null, $priority = 0)
153 if ($renamedId && $id == $renamedId) {
154 throw new \InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
158 $this->decoratedService = null;
160 $this->decoratedService = array($id, $renamedId, (int) $priority);
167 * Gets the service that this service is decorating.
169 * @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
171 public function getDecoratedService()
173 return $this->decoratedService;
177 * Gets the factory method.
179 * @return string|null The factory method name
181 * @deprecated since version 2.6, to be removed in 3.0.
183 public function getFactoryMethod($triggerDeprecationError = true)
185 if ($triggerDeprecationError) {
186 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
189 return $this->factoryMethod;
193 * Sets the name of the service that acts as a factory using the factory method.
195 * @param string $factoryService The factory service id
199 * @deprecated since version 2.6, to be removed in 3.0.
201 public function setFactoryService($factoryService, $triggerDeprecationError = true)
203 if ($triggerDeprecationError) {
204 @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);
207 $this->factoryService = $factoryService;
213 * Gets the factory service id.
215 * @return string|null The factory service id
217 * @deprecated since version 2.6, to be removed in 3.0.
219 public function getFactoryService($triggerDeprecationError = true)
221 if ($triggerDeprecationError) {
222 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
225 return $this->factoryService;
229 * Sets the service class.
231 * @param string $class The service class
235 public function setClass($class)
237 $this->class = $class;
243 * Gets the service class.
245 * @return string|null The service class
247 public function getClass()
253 * Sets the arguments to pass to the service constructor/factory method.
255 * @param array $arguments An array of arguments
259 public function setArguments(array $arguments)
261 $this->arguments = $arguments;
266 public function setProperties(array $properties)
268 $this->properties = $properties;
273 public function getProperties()
275 return $this->properties;
278 public function setProperty($name, $value)
280 $this->properties[$name] = $value;
286 * Adds an argument to pass to the service constructor/factory method.
288 * @param mixed $argument An argument
292 public function addArgument($argument)
294 $this->arguments[] = $argument;
300 * Sets a specific argument.
303 * @param mixed $argument
307 * @throws OutOfBoundsException When the replaced argument does not exist
309 public function replaceArgument($index, $argument)
311 if (0 === count($this->arguments)) {
312 throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
315 if ($index < 0 || $index > count($this->arguments) - 1) {
316 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
319 $this->arguments[$index] = $argument;
325 * Gets the arguments to pass to the service constructor/factory method.
327 * @return array The array of arguments
329 public function getArguments()
331 return $this->arguments;
335 * Gets an argument to pass to the service constructor/factory method.
339 * @return mixed The argument value
341 * @throws OutOfBoundsException When the argument does not exist
343 public function getArgument($index)
345 if ($index < 0 || $index > count($this->arguments) - 1) {
346 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
349 return $this->arguments[$index];
353 * Sets the methods to call after service initialization.
355 * @param array $calls An array of method calls
359 public function setMethodCalls(array $calls = array())
361 $this->calls = array();
362 foreach ($calls as $call) {
363 $this->addMethodCall($call[0], $call[1]);
370 * Adds a method to call after service initialization.
372 * @param string $method The method name to call
373 * @param array $arguments An array of arguments to pass to the method call
377 * @throws InvalidArgumentException on empty $method param
379 public function addMethodCall($method, array $arguments = array())
381 if (empty($method)) {
382 throw new InvalidArgumentException(sprintf('Method name cannot be empty.'));
384 $this->calls[] = array($method, $arguments);
390 * Removes a method to call after service initialization.
392 * @param string $method The method name to remove
396 public function removeMethodCall($method)
398 foreach ($this->calls as $i => $call) {
399 if ($call[0] === $method) {
400 unset($this->calls[$i]);
409 * Check if the current definition has a given method to call after service initialization.
411 * @param string $method The method name to search for
415 public function hasMethodCall($method)
417 foreach ($this->calls as $call) {
418 if ($call[0] === $method) {
427 * Gets the methods to call after service initialization.
429 * @return array An array of method calls
431 public function getMethodCalls()
437 * Sets tags for this definition.
443 public function setTags(array $tags)
453 * @return array An array of tags
455 public function getTags()
461 * Gets a tag by name.
463 * @param string $name The tag name
465 * @return array An array of attributes
467 public function getTag($name)
469 return isset($this->tags[$name]) ? $this->tags[$name] : array();
473 * Adds a tag for this definition.
475 * @param string $name The tag name
476 * @param array $attributes An array of attributes
480 public function addTag($name, array $attributes = array())
482 $this->tags[$name][] = $attributes;
488 * Whether this definition has a tag with the given name.
490 * @param string $name
494 public function hasTag($name)
496 return isset($this->tags[$name]);
500 * Clears all tags for a given name.
502 * @param string $name The tag name
506 public function clearTag($name)
508 unset($this->tags[$name]);
514 * Clears the tags for this definition.
518 public function clearTags()
520 $this->tags = array();
526 * Sets a file to require before creating the service.
528 * @param string $file A full pathname to include
532 public function setFile($file)
540 * Gets the file to require before creating the service.
542 * @return string|null The full pathname to include
544 public function getFile()
550 * Sets if the service must be shared or not.
552 * @param bool $shared Whether the service must be shared or not
556 public function setShared($shared)
558 $this->shared = (bool) $shared;
564 * Whether this service is shared.
568 public function isShared()
570 return $this->shared;
574 * Sets the scope of the service.
576 * @param string $scope Whether the service must be shared or not
580 * @deprecated since version 2.8, to be removed in 3.0.
582 public function setScope($scope, $triggerDeprecationError = true)
584 if ($triggerDeprecationError) {
585 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
588 if (ContainerInterface::SCOPE_PROTOTYPE === $scope) {
589 $this->setShared(false);
592 $this->scope = $scope;
598 * Returns the scope of the service.
602 * @deprecated since version 2.8, to be removed in 3.0.
604 public function getScope($triggerDeprecationError = true)
606 if ($triggerDeprecationError) {
607 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
614 * Sets the visibility of this service.
616 * @param bool $boolean
620 public function setPublic($boolean)
622 $this->public = (bool) $boolean;
628 * Whether this service is public facing.
632 public function isPublic()
634 return $this->public;
638 * Sets the synchronized flag of this service.
640 * @param bool $boolean
644 * @deprecated since version 2.7, will be removed in 3.0.
646 public function setSynchronized($boolean, $triggerDeprecationError = true)
648 if ($triggerDeprecationError) {
649 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
652 $this->synchronized = (bool) $boolean;
658 * Whether this service is synchronized.
662 * @deprecated since version 2.7, will be removed in 3.0.
664 public function isSynchronized($triggerDeprecationError = true)
666 if ($triggerDeprecationError) {
667 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
670 return $this->synchronized;
674 * Sets the lazy flag of this service.
680 public function setLazy($lazy)
682 $this->lazy = (bool) $lazy;
688 * Whether this service is lazy.
692 public function isLazy()
698 * Sets whether this definition is synthetic, that is not constructed by the
699 * container, but dynamically injected.
701 * @param bool $boolean
705 public function setSynthetic($boolean)
707 $this->synthetic = (bool) $boolean;
713 * Whether this definition is synthetic, that is not constructed by the
714 * container, but dynamically injected.
718 public function isSynthetic()
720 return $this->synthetic;
724 * Whether this definition is abstract, that means it merely serves as a
725 * template for other definitions.
727 * @param bool $boolean
731 public function setAbstract($boolean)
733 $this->abstract = (bool) $boolean;
739 * Whether this definition is abstract, that means it merely serves as a
740 * template for other definitions.
744 public function isAbstract()
746 return $this->abstract;
750 * Whether this definition is deprecated, that means it should not be called
753 * @param bool $status
754 * @param string $template Template message to use if the definition is deprecated
758 * @throws InvalidArgumentException When the message template is invalid.
760 public function setDeprecated($status = true, $template = null)
762 if (null !== $template) {
763 if (preg_match('#[\r\n]|\*/#', $template)) {
764 throw new InvalidArgumentException('Invalid characters found in deprecation template.');
767 if (false === strpos($template, '%service_id%')) {
768 throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
771 $this->deprecationTemplate = $template;
774 $this->deprecated = (bool) $status;
780 * Whether this definition is deprecated, that means it should not be called
785 public function isDeprecated()
787 return $this->deprecated;
791 * Message to use if this definition is deprecated.
793 * @param string $id Service id relying on this definition
797 public function getDeprecationMessage($id)
799 return str_replace('%service_id%', $id, $this->deprecationTemplate);
803 * Sets a configurator to call after the service is fully initialized.
805 * @param callable $callable A PHP callable
809 public function setConfigurator($callable)
811 $this->configurator = $callable;
817 * Gets the configurator to call after the service is fully initialized.
819 * @return callable|null The PHP callable to call
821 public function getConfigurator()
823 return $this->configurator;
827 * Sets types that will default to this definition.
829 * @param string[] $types
833 public function setAutowiringTypes(array $types)
835 $this->autowiringTypes = array();
837 foreach ($types as $type) {
838 $this->autowiringTypes[$type] = true;
845 * Is the definition autowired?
849 public function isAutowired()
851 return $this->autowired;
857 * @param bool $autowired
861 public function setAutowired($autowired)
863 $this->autowired = $autowired;
869 * Gets autowiring types that will default to this definition.
873 public function getAutowiringTypes()
875 return array_keys($this->autowiringTypes);
879 * Adds a type that will default to this definition.
881 * @param string $type
885 public function addAutowiringType($type)
887 $this->autowiringTypes[$type] = true;
895 * @param string $type
899 public function removeAutowiringType($type)
901 unset($this->autowiringTypes[$type]);
907 * Will this definition default for the given type?
909 * @param string $type
913 public function hasAutowiringType($type)
915 return isset($this->autowiringTypes[$type]);