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 * This definition decorates another definition.
20 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
22 class DefinitionDecorator extends Definition
25 private $changes = array();
28 * @param string $parent The id of Definition instance to decorate
30 public function __construct($parent)
32 parent::__construct();
34 $this->parent = $parent;
38 * Returns the Definition being decorated.
42 public function getParent()
48 * Returns all changes tracked for the Definition object.
50 * @return array An array of changes for this Definition
52 public function getChanges()
54 return $this->changes;
60 public function setClass($class)
62 $this->changes['class'] = true;
64 return parent::setClass($class);
70 public function setFactory($callable)
72 $this->changes['factory'] = true;
74 return parent::setFactory($callable);
80 public function setConfigurator($callable)
82 $this->changes['configurator'] = true;
84 return parent::setConfigurator($callable);
90 public function setFile($file)
92 $this->changes['file'] = true;
94 return parent::setFile($file);
100 public function setPublic($boolean)
102 $this->changes['public'] = true;
104 return parent::setPublic($boolean);
110 public function setLazy($boolean)
112 $this->changes['lazy'] = true;
114 return parent::setLazy($boolean);
120 public function setDecoratedService($id, $renamedId = null, $priority = 0)
122 $this->changes['decorated_service'] = true;
124 return parent::setDecoratedService($id, $renamedId, $priority);
130 public function setDeprecated($boolean = true, $template = null)
132 $this->changes['deprecated'] = true;
134 return parent::setDeprecated($boolean, $template);
140 public function setAutowired($autowired)
142 $this->changes['autowire'] = true;
144 return parent::setAutowired($autowired);
148 * Gets an argument to pass to the service constructor/factory method.
150 * If replaceArgument() has been used to replace an argument, this method
151 * will return the replacement value.
155 * @return mixed The argument value
157 * @throws OutOfBoundsException When the argument does not exist
159 public function getArgument($index)
161 if (array_key_exists('index_'.$index, $this->arguments)) {
162 return $this->arguments['index_'.$index];
165 $lastIndex = count(array_filter(array_keys($this->arguments), 'is_int')) - 1;
167 if ($index < 0 || $index > $lastIndex) {
168 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, $lastIndex));
171 return $this->arguments[$index];
175 * You should always use this method when overwriting existing arguments
176 * of the parent definition.
178 * If you directly call setArguments() keep in mind that you must follow
179 * certain conventions when you want to overwrite the arguments of the
180 * parent definition, otherwise your arguments will only be appended.
183 * @param mixed $value
187 * @throws InvalidArgumentException when $index isn't an integer
189 public function replaceArgument($index, $value)
191 if (!is_int($index)) {
192 throw new InvalidArgumentException('$index must be an integer.');
195 $this->arguments['index_'.$index] = $value;