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 setFactoryClass($class)
82 $this->changes['factory_class'] = true;
84 return parent::setFactoryClass($class);
90 public function setFactoryMethod($method)
92 $this->changes['factory_method'] = true;
94 return parent::setFactoryMethod($method);
100 public function setFactoryService($service, $triggerDeprecationError = true)
102 $this->changes['factory_service'] = true;
104 return parent::setFactoryService($service, $triggerDeprecationError);
110 public function setConfigurator($callable)
112 $this->changes['configurator'] = true;
114 return parent::setConfigurator($callable);
120 public function setFile($file)
122 $this->changes['file'] = true;
124 return parent::setFile($file);
130 public function setPublic($boolean)
132 $this->changes['public'] = true;
134 return parent::setPublic($boolean);
140 public function setLazy($boolean)
142 $this->changes['lazy'] = true;
144 return parent::setLazy($boolean);
150 public function setDecoratedService($id, $renamedId = null, $priority = 0)
152 $this->changes['decorated_service'] = true;
154 return parent::setDecoratedService($id, $renamedId, $priority);
160 public function setDeprecated($boolean = true, $template = null)
162 $this->changes['deprecated'] = true;
164 return parent::setDeprecated($boolean, $template);
170 public function setAutowired($autowired)
172 $this->changes['autowire'] = true;
174 return parent::setAutowired($autowired);
178 * Gets an argument to pass to the service constructor/factory method.
180 * If replaceArgument() has been used to replace an argument, this method
181 * will return the replacement value.
185 * @return mixed The argument value
187 * @throws OutOfBoundsException When the argument does not exist
189 public function getArgument($index)
191 if (array_key_exists('index_'.$index, $this->arguments)) {
192 return $this->arguments['index_'.$index];
195 $lastIndex = count(array_filter(array_keys($this->arguments), 'is_int')) - 1;
197 if ($index < 0 || $index > $lastIndex) {
198 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, $lastIndex));
201 return $this->arguments[$index];
205 * You should always use this method when overwriting existing arguments
206 * of the parent definition.
208 * If you directly call setArguments() keep in mind that you must follow
209 * certain conventions when you want to overwrite the arguments of the
210 * parent definition, otherwise your arguments will only be appended.
213 * @param mixed $value
217 * @throws InvalidArgumentException when $index isn't an integer
219 public function replaceArgument($index, $value)
221 if (!is_int($index)) {
222 throw new InvalidArgumentException('$index must be an integer.');
225 $this->arguments['index_'.$index] = $value;