Version 1
[yaffs-website] / vendor / symfony / dependency-injection / DefinitionDecorator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection;
13
14 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
15 use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
16
17 /**
18  * This definition decorates another definition.
19  *
20  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
21  */
22 class DefinitionDecorator extends Definition
23 {
24     private $parent;
25     private $changes = array();
26
27     /**
28      * @param string $parent The id of Definition instance to decorate
29      */
30     public function __construct($parent)
31     {
32         parent::__construct();
33
34         $this->parent = $parent;
35     }
36
37     /**
38      * Returns the Definition being decorated.
39      *
40      * @return string
41      */
42     public function getParent()
43     {
44         return $this->parent;
45     }
46
47     /**
48      * Returns all changes tracked for the Definition object.
49      *
50      * @return array An array of changes for this Definition
51      */
52     public function getChanges()
53     {
54         return $this->changes;
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     public function setClass($class)
61     {
62         $this->changes['class'] = true;
63
64         return parent::setClass($class);
65     }
66
67     /**
68      * {@inheritdoc}
69      */
70     public function setFactory($callable)
71     {
72         $this->changes['factory'] = true;
73
74         return parent::setFactory($callable);
75     }
76
77     /**
78      * {@inheritdoc}
79      */
80     public function setFactoryClass($class)
81     {
82         $this->changes['factory_class'] = true;
83
84         return parent::setFactoryClass($class);
85     }
86
87     /**
88      * {@inheritdoc}
89      */
90     public function setFactoryMethod($method)
91     {
92         $this->changes['factory_method'] = true;
93
94         return parent::setFactoryMethod($method);
95     }
96
97     /**
98      * {@inheritdoc}
99      */
100     public function setFactoryService($service, $triggerDeprecationError = true)
101     {
102         $this->changes['factory_service'] = true;
103
104         return parent::setFactoryService($service, $triggerDeprecationError);
105     }
106
107     /**
108      * {@inheritdoc}
109      */
110     public function setConfigurator($callable)
111     {
112         $this->changes['configurator'] = true;
113
114         return parent::setConfigurator($callable);
115     }
116
117     /**
118      * {@inheritdoc}
119      */
120     public function setFile($file)
121     {
122         $this->changes['file'] = true;
123
124         return parent::setFile($file);
125     }
126
127     /**
128      * {@inheritdoc}
129      */
130     public function setPublic($boolean)
131     {
132         $this->changes['public'] = true;
133
134         return parent::setPublic($boolean);
135     }
136
137     /**
138      * {@inheritdoc}
139      */
140     public function setLazy($boolean)
141     {
142         $this->changes['lazy'] = true;
143
144         return parent::setLazy($boolean);
145     }
146
147     /**
148      * {@inheritdoc}
149      */
150     public function setDecoratedService($id, $renamedId = null, $priority = 0)
151     {
152         $this->changes['decorated_service'] = true;
153
154         return parent::setDecoratedService($id, $renamedId, $priority);
155     }
156
157     /**
158      * {@inheritdoc}
159      */
160     public function setDeprecated($boolean = true, $template = null)
161     {
162         $this->changes['deprecated'] = true;
163
164         return parent::setDeprecated($boolean, $template);
165     }
166
167     /**
168      * {@inheritdoc}
169      */
170     public function setAutowired($autowired)
171     {
172         $this->changes['autowire'] = true;
173
174         return parent::setAutowired($autowired);
175     }
176
177     /**
178      * Gets an argument to pass to the service constructor/factory method.
179      *
180      * If replaceArgument() has been used to replace an argument, this method
181      * will return the replacement value.
182      *
183      * @param int $index
184      *
185      * @return mixed The argument value
186      *
187      * @throws OutOfBoundsException When the argument does not exist
188      */
189     public function getArgument($index)
190     {
191         if (array_key_exists('index_'.$index, $this->arguments)) {
192             return $this->arguments['index_'.$index];
193         }
194
195         $lastIndex = count(array_filter(array_keys($this->arguments), 'is_int')) - 1;
196
197         if ($index < 0 || $index > $lastIndex) {
198             throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, $lastIndex));
199         }
200
201         return $this->arguments[$index];
202     }
203
204     /**
205      * You should always use this method when overwriting existing arguments
206      * of the parent definition.
207      *
208      * If you directly call setArguments() keep in mind that you must follow
209      * certain conventions when you want to overwrite the arguments of the
210      * parent definition, otherwise your arguments will only be appended.
211      *
212      * @param int   $index
213      * @param mixed $value
214      *
215      * @return $this
216      *
217      * @throws InvalidArgumentException when $index isn't an integer
218      */
219     public function replaceArgument($index, $value)
220     {
221         if (!is_int($index)) {
222             throw new InvalidArgumentException('$index must be an integer.');
223         }
224
225         $this->arguments['index_'.$index] = $value;
226
227         return $this;
228     }
229 }