4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\ParameterBag;
14 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
15 use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
21 * @author Fabien Potencier <fabien@symfony.com>
23 class ParameterBag implements ParameterBagInterface
25 protected $parameters = array();
26 protected $resolved = false;
29 * @param array $parameters An array of parameters
31 public function __construct(array $parameters = array())
33 $this->add($parameters);
37 * Clears all parameters.
39 public function clear()
41 $this->parameters = array();
45 * Adds parameters to the service container parameters.
47 * @param array $parameters An array of parameters
49 public function add(array $parameters)
51 foreach ($parameters as $key => $value) {
52 $this->parameters[strtolower($key)] = $value;
61 return $this->parameters;
67 public function get($name)
69 $name = strtolower($name);
71 if (!array_key_exists($name, $this->parameters)) {
73 throw new ParameterNotFoundException($name);
76 $alternatives = array();
77 foreach ($this->parameters as $key => $parameterValue) {
78 $lev = levenshtein($name, $key);
79 if ($lev <= strlen($name) / 3 || false !== strpos($key, $name)) {
80 $alternatives[] = $key;
84 throw new ParameterNotFoundException($name, null, null, null, $alternatives);
87 return $this->parameters[$name];
91 * Sets a service container parameter.
93 * @param string $name The parameter name
94 * @param mixed $value The parameter value
96 public function set($name, $value)
98 $this->parameters[strtolower($name)] = $value;
104 public function has($name)
106 return array_key_exists(strtolower($name), $this->parameters);
110 * Removes a parameter.
112 * @param string $name The parameter name
114 public function remove($name)
116 unset($this->parameters[strtolower($name)]);
122 public function resolve()
124 if ($this->resolved) {
128 $parameters = array();
129 foreach ($this->parameters as $key => $value) {
131 $value = $this->resolveValue($value);
132 $parameters[$key] = $this->unescapeValue($value);
133 } catch (ParameterNotFoundException $e) {
134 $e->setSourceKey($key);
140 $this->parameters = $parameters;
141 $this->resolved = true;
145 * Replaces parameter placeholders (%name%) by their values.
147 * @param mixed $value A value
148 * @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
150 * @return mixed The resolved value
152 * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
153 * @throws ParameterCircularReferenceException if a circular reference if detected
154 * @throws RuntimeException when a given parameter has a type problem.
156 public function resolveValue($value, array $resolving = array())
158 if (is_array($value)) {
160 foreach ($value as $k => $v) {
161 $args[$this->resolveValue($k, $resolving)] = $this->resolveValue($v, $resolving);
167 if (!is_string($value)) {
171 return $this->resolveString($value, $resolving);
175 * Resolves parameters inside a string.
177 * @param string $value The string to resolve
178 * @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
180 * @return string The resolved string
182 * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
183 * @throws ParameterCircularReferenceException if a circular reference if detected
184 * @throws RuntimeException when a given parameter has a type problem.
186 public function resolveString($value, array $resolving = array())
188 // we do this to deal with non string values (Boolean, integer, ...)
189 // as the preg_replace_callback throw an exception when trying
190 // a non-string in a parameter value
191 if (preg_match('/^%([^%\s]+)%$/', $value, $match)) {
192 $key = strtolower($match[1]);
194 if (isset($resolving[$key])) {
195 throw new ParameterCircularReferenceException(array_keys($resolving));
198 $resolving[$key] = true;
200 return $this->resolved ? $this->get($key) : $this->resolveValue($this->get($key), $resolving);
205 return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($self, $resolving, $value) {
207 if (!isset($match[1])) {
211 $key = strtolower($match[1]);
212 if (isset($resolving[$key])) {
213 throw new ParameterCircularReferenceException(array_keys($resolving));
216 $resolved = $self->get($key);
218 if (!is_string($resolved) && !is_numeric($resolved)) {
219 throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "%s" of type %s inside string value "%s".', $key, gettype($resolved), $value));
222 $resolved = (string) $resolved;
223 $resolving[$key] = true;
225 return $self->isResolved() ? $resolved : $self->resolveString($resolved, $resolving);
229 public function isResolved()
231 return $this->resolved;
237 public function escapeValue($value)
239 if (is_string($value)) {
240 return str_replace('%', '%%', $value);
243 if (is_array($value)) {
245 foreach ($value as $k => $v) {
246 $result[$k] = $this->escapeValue($v);
258 public function unescapeValue($value)
260 if (is_string($value)) {
261 return str_replace('%%', '%', $value);
264 if (is_array($value)) {
266 foreach ($value as $k => $v) {
267 $result[$k] = $this->unescapeValue($v);