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