ChildDefinition.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\BadMethodCallException;
  12. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  13. use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
  14. /**
  15. * This definition extends another definition.
  16. *
  17. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18. */
  19. class ChildDefinition extends Definition
  20. {
  21. private $parent;
  22. /**
  23. * @param string $parent The id of Definition instance to decorate
  24. */
  25. public function __construct($parent)
  26. {
  27. $this->parent = $parent;
  28. }
  29. /**
  30. * Returns the Definition to inherit from.
  31. *
  32. * @return string
  33. */
  34. public function getParent()
  35. {
  36. return $this->parent;
  37. }
  38. /**
  39. * Sets the Definition to inherit from.
  40. *
  41. * @param string $parent
  42. *
  43. * @return $this
  44. */
  45. public function setParent($parent)
  46. {
  47. $this->parent = $parent;
  48. return $this;
  49. }
  50. /**
  51. * Gets an argument to pass to the service constructor/factory method.
  52. *
  53. * If replaceArgument() has been used to replace an argument, this method
  54. * will return the replacement value.
  55. *
  56. * @param int|string $index
  57. *
  58. * @return mixed The argument value
  59. *
  60. * @throws OutOfBoundsException When the argument does not exist
  61. */
  62. public function getArgument($index)
  63. {
  64. if (array_key_exists('index_'.$index, $this->arguments)) {
  65. return $this->arguments['index_'.$index];
  66. }
  67. return parent::getArgument($index);
  68. }
  69. /**
  70. * You should always use this method when overwriting existing arguments
  71. * of the parent definition.
  72. *
  73. * If you directly call setArguments() keep in mind that you must follow
  74. * certain conventions when you want to overwrite the arguments of the
  75. * parent definition, otherwise your arguments will only be appended.
  76. *
  77. * @param int|string $index
  78. * @param mixed $value
  79. *
  80. * @return self the current instance
  81. *
  82. * @throws InvalidArgumentException when $index isn't an integer
  83. */
  84. public function replaceArgument($index, $value)
  85. {
  86. if (is_int($index)) {
  87. $this->arguments['index_'.$index] = $value;
  88. } elseif (0 === strpos($index, '$')) {
  89. $this->arguments[$index] = $value;
  90. } else {
  91. throw new InvalidArgumentException('The argument must be an existing index or the name of a constructor\'s parameter.');
  92. }
  93. return $this;
  94. }
  95. /**
  96. * @internal
  97. */
  98. public function setAutoconfigured($autoconfigured)
  99. {
  100. throw new BadMethodCallException('A ChildDefinition cannot be autoconfigured.');
  101. }
  102. /**
  103. * @internal
  104. */
  105. public function setInstanceofConditionals(array $instanceof)
  106. {
  107. throw new BadMethodCallException('A ChildDefinition cannot have instanceof conditionals set on it.');
  108. }
  109. }
  110. class_alias(ChildDefinition::class, DefinitionDecorator::class);