ChildDefinition.php 2.7 KB

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