/src/Symfony/Component/DependencyInjection/DefinitionDecorator.php

https://github.com/meze/symfony · PHP · 202 lines · 75 code · 28 blank · 99 comment · 4 complexity · ddeb0b481edd8406503ce4cdca66e4eb MD5 · raw file

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