DefinitionDecorator.php 5.2 KB

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