ChildDefinition.php 3.2 KB

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