|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\DependencyInjection; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 15 | +use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException; |
| 16 | + |
| 17 | +/** |
| 18 | + * This definition extends another definition. |
| 19 | + * |
| 20 | + * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
| 21 | + */ |
| 22 | +class ChildDefinition extends Definition |
| 23 | +{ |
| 24 | + private $parent; |
| 25 | + private $changes = array(); |
| 26 | + |
| 27 | + /** |
| 28 | + * @param string $parent The id of Definition instance to decorate |
| 29 | + */ |
| 30 | + public function __construct($parent) |
| 31 | + { |
| 32 | + parent::__construct(); |
| 33 | + |
| 34 | + $this->parent = $parent; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns the Definition being decorated. |
| 39 | + * |
| 40 | + * @return string |
| 41 | + */ |
| 42 | + public function getParent() |
| 43 | + { |
| 44 | + return $this->parent; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns all changes tracked for the Definition object. |
| 49 | + * |
| 50 | + * @return array An array of changes for this Definition |
| 51 | + */ |
| 52 | + public function getChanges() |
| 53 | + { |
| 54 | + return $this->changes; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * {@inheritdoc} |
| 59 | + */ |
| 60 | + public function setClass($class) |
| 61 | + { |
| 62 | + $this->changes['class'] = true; |
| 63 | + |
| 64 | + return parent::setClass($class); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function setFactory($callable) |
| 71 | + { |
| 72 | + $this->changes['factory'] = true; |
| 73 | + |
| 74 | + return parent::setFactory($callable); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * {@inheritdoc} |
| 79 | + */ |
| 80 | + public function setConfigurator($callable) |
| 81 | + { |
| 82 | + $this->changes['configurator'] = true; |
| 83 | + |
| 84 | + return parent::setConfigurator($callable); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritdoc} |
| 89 | + */ |
| 90 | + public function setFile($file) |
| 91 | + { |
| 92 | + $this->changes['file'] = true; |
| 93 | + |
| 94 | + return parent::setFile($file); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * {@inheritdoc} |
| 99 | + */ |
| 100 | + public function setPublic($boolean) |
| 101 | + { |
| 102 | + $this->changes['public'] = true; |
| 103 | + |
| 104 | + return parent::setPublic($boolean); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * {@inheritdoc} |
| 109 | + */ |
| 110 | + public function setLazy($boolean) |
| 111 | + { |
| 112 | + $this->changes['lazy'] = true; |
| 113 | + |
| 114 | + return parent::setLazy($boolean); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * {@inheritdoc} |
| 119 | + */ |
| 120 | + public function setDecoratedService($id, $renamedId = null, $priority = 0) |
| 121 | + { |
| 122 | + $this->changes['decorated_service'] = true; |
| 123 | + |
| 124 | + return parent::setDecoratedService($id, $renamedId, $priority); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * {@inheritdoc} |
| 129 | + */ |
| 130 | + public function setDeprecated($boolean = true, $template = null) |
| 131 | + { |
| 132 | + $this->changes['deprecated'] = true; |
| 133 | + |
| 134 | + return parent::setDeprecated($boolean, $template); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * {@inheritdoc} |
| 139 | + */ |
| 140 | + public function setAutowired($autowired) |
| 141 | + { |
| 142 | + $this->changes['autowire'] = true; |
| 143 | + |
| 144 | + return parent::setAutowired($autowired); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Gets an argument to pass to the service constructor/factory method. |
| 149 | + * |
| 150 | + * If replaceArgument() has been used to replace an argument, this method |
| 151 | + * will return the replacement value. |
| 152 | + * |
| 153 | + * @param int $index |
| 154 | + * |
| 155 | + * @return mixed The argument value |
| 156 | + * |
| 157 | + * @throws OutOfBoundsException When the argument does not exist |
| 158 | + */ |
| 159 | + public function getArgument($index) |
| 160 | + { |
| 161 | + if (array_key_exists('index_'.$index, $this->arguments)) { |
| 162 | + return $this->arguments['index_'.$index]; |
| 163 | + } |
| 164 | + |
| 165 | + $lastIndex = count(array_filter(array_keys($this->arguments), 'is_int')) - 1; |
| 166 | + |
| 167 | + if ($index < 0 || $index > $lastIndex) { |
| 168 | + throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, $lastIndex)); |
| 169 | + } |
| 170 | + |
| 171 | + return $this->arguments[$index]; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * You should always use this method when overwriting existing arguments |
| 176 | + * of the parent definition. |
| 177 | + * |
| 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. |
| 181 | + * |
| 182 | + * @param int $index |
| 183 | + * @param mixed $value |
| 184 | + * |
| 185 | + * @return self the current instance |
| 186 | + * |
| 187 | + * @throws InvalidArgumentException when $index isn't an integer |
| 188 | + */ |
| 189 | + public function replaceArgument($index, $value) |
| 190 | + { |
| 191 | + if (!is_int($index)) { |
| 192 | + throw new InvalidArgumentException('$index must be an integer.'); |
| 193 | + } |
| 194 | + |
| 195 | + $this->arguments['index_'.$index] = $value; |
| 196 | + |
| 197 | + return $this; |
| 198 | + } |
| 199 | +} |
0 commit comments