2010-06-27 17:28:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-06-27 17:28:29 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-06-27 17:28:29 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\ParameterBag;
|
|
|
|
|
2010-06-27 17:28:29 +01:00
|
|
|
/**
|
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-06-27 17:28:29 +01:00
|
|
|
*/
|
|
|
|
class ParameterBag implements ParameterBagInterface
|
|
|
|
{
|
|
|
|
protected $parameters;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param array $parameters An array of parameters
|
|
|
|
*/
|
|
|
|
public function __construct(array $parameters = array())
|
|
|
|
{
|
|
|
|
$this->parameters = array();
|
|
|
|
$this->add($parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all parameters.
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
$this->parameters = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds parameters to the service container parameters.
|
|
|
|
*
|
|
|
|
* @param array $parameters An array of parameters
|
|
|
|
*/
|
|
|
|
public function add(array $parameters)
|
|
|
|
{
|
|
|
|
foreach ($parameters as $key => $value) {
|
|
|
|
$this->parameters[strtolower($key)] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the service container parameters.
|
|
|
|
*
|
|
|
|
* @return array An array of parameters
|
|
|
|
*/
|
|
|
|
public function all()
|
|
|
|
{
|
|
|
|
return $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 get($name)
|
|
|
|
{
|
|
|
|
$name = strtolower($name);
|
|
|
|
|
|
|
|
if (!array_key_exists($name, $this->parameters)) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->parameters[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a service container parameter.
|
|
|
|
*
|
|
|
|
* @param string $name The parameter name
|
|
|
|
* @param mixed $parameters The parameter value
|
|
|
|
*/
|
|
|
|
public function set($name, $value)
|
|
|
|
{
|
|
|
|
$this->parameters[strtolower($name)] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 has($name)
|
|
|
|
{
|
|
|
|
return array_key_exists(strtolower($name), $this->parameters);
|
|
|
|
}
|
2010-07-15 19:15:49 +01:00
|
|
|
|
2010-07-16 08:15:22 +01:00
|
|
|
/**
|
|
|
|
* Replaces parameter placeholders (%name%) by their values for all parameters.
|
|
|
|
*/
|
|
|
|
public function resolve()
|
|
|
|
{
|
|
|
|
foreach ($this->parameters as $key => $value) {
|
|
|
|
$this->parameters[$key] = $this->resolveValue($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-15 19:15:49 +01:00
|
|
|
/**
|
|
|
|
* Replaces parameter placeholders (%name%) by their values.
|
|
|
|
*
|
|
|
|
* @param mixed $value A value
|
|
|
|
*
|
2010-12-23 08:56:05 +00:00
|
|
|
* @throws \InvalidArgumentException if a placeholder references a parameter that does not exist
|
2010-07-15 19:15:49 +01:00
|
|
|
*/
|
|
|
|
public function resolveValue($value)
|
|
|
|
{
|
|
|
|
if (is_array($value)) {
|
|
|
|
$args = array();
|
|
|
|
foreach ($value as $k => $v) {
|
|
|
|
$args[$this->resolveValue($k)] = $this->resolveValue($v);
|
|
|
|
}
|
|
|
|
|
2010-07-16 08:15:22 +01:00
|
|
|
return $args;
|
2010-07-15 19:15:49 +01:00
|
|
|
}
|
|
|
|
|
2010-07-16 08:15:22 +01:00
|
|
|
if (!is_string($value)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/^%([^%]+)%$/', $value, $match)) {
|
2011-01-21 01:39:28 +00:00
|
|
|
// we do this to deal with non string values (Boolean, integer, ...)
|
2010-07-16 08:15:22 +01:00
|
|
|
// the preg_replace_callback converts them to strings
|
|
|
|
return $this->get(strtolower($match[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return str_replace('%%', '%', preg_replace_callback(array('/(?<!%)%([^%]+)%/'), array($this, 'resolveValueCallback'), $value));
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Value callback
|
|
|
|
*
|
|
|
|
* @see resolveValue
|
|
|
|
*
|
|
|
|
* @param array $match
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-07-16 08:15:22 +01:00
|
|
|
protected function resolveValueCallback($match)
|
|
|
|
{
|
|
|
|
return $this->get(strtolower($match[1]));
|
2010-07-15 19:15:49 +01:00
|
|
|
}
|
2010-06-27 17:28:29 +01:00
|
|
|
}
|