/
BuilderConfiguration.php
289 lines (260 loc) · 6.53 KB
/
BuilderConfiguration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
namespace Symfony\Components\DependencyInjection;
/*
* This file is part of the symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* A BuilderConfiguration is a consistent set of definitions and parameters.
*
* @package symfony
* @subpackage dependency_injection
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class BuilderConfiguration
{
protected $definitions = array();
protected $parameters = array();
protected $aliases = array();
public function __construct(array $definitions = array(), array $parameters = array())
{
$this->setDefinitions($definitions);
$this->setParameters($parameters);
}
/**
* Merges a BuilderConfiguration with the current one.
*
* @param BuilderConfiguration $configuration
*/
public function merge(BuilderConfiguration $configuration = null)
{
if (null === $configuration)
{
return;
}
$this->addDefinitions($configuration->getDefinitions());
$this->addAliases($configuration->getAliases());
$this->addParameters($configuration->getParameters());
}
/**
* Sets the service container parameters.
*
* @param array $parameters An array of parameters
*/
public function setParameters(array $parameters)
{
$this->parameters = array();
foreach ($parameters as $key => $value)
{
$this->parameters[strtolower($key)] = $value;
}
}
/**
* Adds parameters to the service container parameters.
*
* @param array $parameters An array of parameters
*/
public function addParameters(array $parameters)
{
$this->setParameters(array_merge($this->parameters, $parameters));
}
/**
* Gets the service container parameters.
*
* @return array An array of parameters
*/
public function getParameters()
{
return $this->parameters;
}
/**
* Returns true if a parameter name is defined.
*
* @param string $name The parameter name
*
* @return Boolean true if the parameter name is defined, false otherwise
*/
public function hasParameter($name)
{
return array_key_exists(strtolower($name), $this->parameters);
}
/**
* Gets a service container parameter.
*
* @param string $name The parameter name
*
* @return mixed The parameter value
*
* @throws \InvalidArgumentException if the parameter is not defined
*/
public function getParameter($name)
{
if (!$this->hasParameter($name))
{
throw new \InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
}
return $this->parameters[strtolower($name)];
}
/**
* Sets a service container parameter.
*
* @param string $name The parameter name
* @param mixed $parameters The parameter value
*/
public function setParameter($name, $value)
{
$this->parameters[strtolower($name)] = $value;
}
/**
* Sets an alias for an existing service.
*
* @param string $alias The alias to create
* @param string $id The service to alias
*/
public function setAlias($alias, $id)
{
$this->aliases[$alias] = $id;
}
/**
* Adds definition aliases.
*
* @param array $aliases An array of aliases
*/
public function addAliases(array $aliases)
{
foreach ($aliases as $alias => $id)
{
$this->setAlias($alias, $id);
}
}
/**
* Gets all defined aliases.
*
* @return array An array of aliases
*/
public function getAliases()
{
return $this->aliases;
}
/**
* Returns true if a service alias exists.
*
* @param string $alias The alias
*
* @return Boolean true if the alias exists, false otherwise
*/
public function hasAlias($alias)
{
return array_key_exists($alias, $this->aliases);
}
/**
* Gets the service id for a given alias.
*
* @param string $alias The alias
*
* @return string The aliased id
*
* @throws \InvalidArgumentException if the service alias does not exist
*/
public function getAlias($alias)
{
if (!$this->hasAlias($alias))
{
throw new \InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $alias));
}
return $this->aliases[$alias];
}
/**
* Sets a definition.
*
* @param string $id The identifier
* @param Definition $definition A Definition instance
*/
public function setDefinition($id, Definition $definition)
{
unset($this->aliases[$id]);
return $this->definitions[$id] = $definition;
}
/**
* Adds the definitions.
*
* @param array $definitions An array of definitions
*/
public function addDefinitions(array $definitions)
{
foreach ($definitions as $id => $definition)
{
$this->setDefinition($id, $definition);
}
}
/**
* Sets the definitions.
*
* @param array $definitions An array of definitions
*/
public function setDefinitions(array $definitions)
{
$this->definitions = array();
$this->addDefinitions($definitions);
}
/**
* Gets all definitions.
*
* @return array An array of Definition instances
*/
public function getDefinitions()
{
return $this->definitions;
}
/**
* Returns true if a service definition exists under the given identifier.
*
* @param string $id The service identifier
*
* @return Boolean true if the service definition exists, false otherwise
*/
public function hasDefinition($id)
{
return array_key_exists($id, $this->definitions);
}
/**
* Gets a service definition.
*
* @param string $id The service identifier
*
* @return Definition A Definition instance
*
* @throws \InvalidArgumentException if the service definition does not exist
*/
public function getDefinition($id)
{
if (!$this->hasDefinition($id))
{
throw new \InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
}
return $this->definitions[$id];
}
/**
* Gets a service definition by id or alias.
*
* The method "unaliases" recursively to return a Definition instance.
*
* @param string $id The service identifier or alias
*
* @return Definition A Definition instance
*
* @throws \InvalidArgumentException if the service definition does not exist
*/
public function findDefinition($id)
{
if ($this->hasAlias($id))
{
return $this->findDefinition($this->getAlias($id));
}
return $this->getDefinition($id);
}
}