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 $shared = true;
28 private $deprecated = false;
29 private $deprecationTemplate;
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();
42 private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
47 * @param string|null $class The service class
48 * @param array $arguments An array of arguments to pass to the service constructor
50 public function __construct($class = null, array $arguments = array())
52 $this->class = $class;
53 $this->arguments = $arguments;
59 * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
63 public function setFactory($factory)
65 if (is_string($factory) && strpos($factory, '::') !== false) {
66 $factory = explode('::', $factory, 2);
69 $this->factory = $factory;
77 * @return string|array The PHP function or an array containing a class/Reference and a method to call
79 public function getFactory()
81 return $this->factory;
85 * Sets the service that this service is decorating.
87 * @param null|string $id The decorated service id, use null to remove decoration
88 * @param null|string $renamedId The new decorated service id
89 * @param int $priority The priority of decoration
93 * @throws InvalidArgumentException In case the decorated service id and the new decorated service id are equals.
95 public function setDecoratedService($id, $renamedId = null, $priority = 0)
97 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));
102 $this->decoratedService = null;
104 $this->decoratedService = array($id, $renamedId, (int) $priority);
111 * Gets the service that this service is decorating.
113 * @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
115 public function getDecoratedService()
117 return $this->decoratedService;
121 * Sets the service class.
123 * @param string $class The service class
127 public function setClass($class)
129 $this->class = $class;
135 * Gets the service class.
137 * @return string|null The service class
139 public function getClass()
145 * Sets the arguments to pass to the service constructor/factory method.
147 * @param array $arguments An array of arguments
151 public function setArguments(array $arguments)
153 $this->arguments = $arguments;
158 public function setProperties(array $properties)
160 $this->properties = $properties;
165 public function getProperties()
167 return $this->properties;
170 public function setProperty($name, $value)
172 $this->properties[$name] = $value;
178 * Adds an argument to pass to the service constructor/factory method.
180 * @param mixed $argument An argument
184 public function addArgument($argument)
186 $this->arguments[] = $argument;
192 * Sets a specific argument.
195 * @param mixed $argument
199 * @throws OutOfBoundsException When the replaced argument does not exist
201 public function replaceArgument($index, $argument)
203 if (0 === count($this->arguments)) {
204 throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
207 if ($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));
211 $this->arguments[$index] = $argument;
217 * Gets the arguments to pass to the service constructor/factory method.
219 * @return array The array of arguments
221 public function getArguments()
223 return $this->arguments;
227 * Gets an argument to pass to the service constructor/factory method.
231 * @return mixed The argument value
233 * @throws OutOfBoundsException When the argument does not exist
235 public function getArgument($index)
237 if ($index < 0 || $index > count($this->arguments) - 1) {
238 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
241 return $this->arguments[$index];
245 * Sets the methods to call after service initialization.
247 * @param array $calls An array of method calls
251 public function setMethodCalls(array $calls = array())
253 $this->calls = array();
254 foreach ($calls as $call) {
255 $this->addMethodCall($call[0], $call[1]);
262 * Adds a method to call after service initialization.
264 * @param string $method The method name to call
265 * @param array $arguments An array of arguments to pass to the method call
269 * @throws InvalidArgumentException on empty $method param
271 public function addMethodCall($method, array $arguments = array())
273 if (empty($method)) {
274 throw new InvalidArgumentException('Method name cannot be empty.');
276 $this->calls[] = array($method, $arguments);
282 * Removes a method to call after service initialization.
284 * @param string $method The method name to remove
288 public function removeMethodCall($method)
290 foreach ($this->calls as $i => $call) {
291 if ($call[0] === $method) {
292 unset($this->calls[$i]);
301 * Check if the current definition has a given method to call after service initialization.
303 * @param string $method The method name to search for
307 public function hasMethodCall($method)
309 foreach ($this->calls as $call) {
310 if ($call[0] === $method) {
319 * Gets the methods to call after service initialization.
321 * @return array An array of method calls
323 public function getMethodCalls()
329 * Sets tags for this definition.
335 public function setTags(array $tags)
345 * @return array An array of tags
347 public function getTags()
353 * Gets a tag by name.
355 * @param string $name The tag name
357 * @return array An array of attributes
359 public function getTag($name)
361 return isset($this->tags[$name]) ? $this->tags[$name] : array();
365 * Adds a tag for this definition.
367 * @param string $name The tag name
368 * @param array $attributes An array of attributes
372 public function addTag($name, array $attributes = array())
374 $this->tags[$name][] = $attributes;
380 * Whether this definition has a tag with the given name.
382 * @param string $name
386 public function hasTag($name)
388 return isset($this->tags[$name]);
392 * Clears all tags for a given name.
394 * @param string $name The tag name
398 public function clearTag($name)
400 unset($this->tags[$name]);
406 * Clears the tags for this definition.
410 public function clearTags()
412 $this->tags = array();
418 * Sets a file to require before creating the service.
420 * @param string $file A full pathname to include
424 public function setFile($file)
432 * Gets the file to require before creating the service.
434 * @return string|null The full pathname to include
436 public function getFile()
442 * Sets if the service must be shared or not.
444 * @param bool $shared Whether the service must be shared or not
448 public function setShared($shared)
450 $this->shared = (bool) $shared;
456 * Whether this service is shared.
460 public function isShared()
462 return $this->shared;
466 * Sets the visibility of this service.
468 * @param bool $boolean
472 public function setPublic($boolean)
474 $this->public = (bool) $boolean;
480 * Whether this service is public facing.
484 public function isPublic()
486 return $this->public;
490 * Sets the lazy flag of this service.
496 public function setLazy($lazy)
498 $this->lazy = (bool) $lazy;
504 * Whether this service is lazy.
508 public function isLazy()
514 * Sets whether this definition is synthetic, that is not constructed by the
515 * container, but dynamically injected.
517 * @param bool $boolean
521 public function setSynthetic($boolean)
523 $this->synthetic = (bool) $boolean;
529 * Whether this definition is synthetic, that is not constructed by the
530 * container, but dynamically injected.
534 public function isSynthetic()
536 return $this->synthetic;
540 * Whether this definition is abstract, that means it merely serves as a
541 * template for other definitions.
543 * @param bool $boolean
547 public function setAbstract($boolean)
549 $this->abstract = (bool) $boolean;
555 * Whether this definition is abstract, that means it merely serves as a
556 * template for other definitions.
560 public function isAbstract()
562 return $this->abstract;
566 * Whether this definition is deprecated, that means it should not be called
569 * @param bool $status
570 * @param string $template Template message to use if the definition is deprecated
574 * @throws InvalidArgumentException When the message template is invalid.
576 public function setDeprecated($status = true, $template = null)
578 if (null !== $template) {
579 if (preg_match('#[\r\n]|\*/#', $template)) {
580 throw new InvalidArgumentException('Invalid characters found in deprecation template.');
583 if (false === strpos($template, '%service_id%')) {
584 throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
587 $this->deprecationTemplate = $template;
590 $this->deprecated = (bool) $status;
596 * Whether this definition is deprecated, that means it should not be called
601 public function isDeprecated()
603 return $this->deprecated;
607 * Message to use if this definition is deprecated.
609 * @param string $id Service id relying on this definition
613 public function getDeprecationMessage($id)
615 return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
619 * Sets a configurator to call after the service is fully initialized.
621 * @param string|array $configurator A PHP callable
625 public function setConfigurator($configurator)
627 if (is_string($configurator) && strpos($configurator, '::') !== false) {
628 $configurator = explode('::', $configurator, 2);
631 $this->configurator = $configurator;
637 * Gets the configurator to call after the service is fully initialized.
639 * @return callable|null The PHP callable to call
641 public function getConfigurator()
643 return $this->configurator;
647 * Sets types that will default to this definition.
649 * @param string[] $types
653 public function setAutowiringTypes(array $types)
655 $this->autowiringTypes = array();
657 foreach ($types as $type) {
658 $this->autowiringTypes[$type] = true;
665 * Is the definition autowired?
669 public function isAutowired()
671 return $this->autowired;
677 * @param bool $autowired
681 public function setAutowired($autowired)
683 $this->autowired = $autowired;
689 * Gets autowiring types that will default to this definition.
693 public function getAutowiringTypes()
695 return array_keys($this->autowiringTypes);
699 * Adds a type that will default to this definition.
701 * @param string $type
705 public function addAutowiringType($type)
707 $this->autowiringTypes[$type] = true;
715 * @param string $type
719 public function removeAutowiringType($type)
721 unset($this->autowiringTypes[$type]);
727 * Will this definition default for the given type?
729 * @param string $type
733 public function hasAutowiringType($type)
735 return isset($this->autowiringTypes[$type]);