2010-12-29 19:12:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-12-29 19:12:24 +00: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-12-29 19:12:24 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
|
|
|
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
|
2010-12-29 19:12:24 +00:00
|
|
|
/**
|
|
|
|
* Resolves all parameter placeholders "%somevalue%" to their real values.
|
|
|
|
*
|
|
|
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
|
|
|
*/
|
|
|
|
class ResolveParameterPlaceHoldersPass implements CompilerPassInterface
|
|
|
|
{
|
|
|
|
protected $parameterBag;
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Processes the ContainerBuilder to resolve parameter placeholders.
|
|
|
|
*
|
|
|
|
* @param ContainerBuilder $container
|
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function process(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$this->parameterBag = $container->getParameterBag();
|
|
|
|
|
2011-02-04 18:10:13 +00:00
|
|
|
foreach ($container->getDefinitions() as $definition) {
|
2010-12-29 19:12:24 +00:00
|
|
|
$definition->setClass($this->resolveValue($definition->getClass()));
|
2010-12-30 16:27:43 +00:00
|
|
|
$definition->setFile($this->resolveValue($definition->getFile()));
|
2010-12-29 19:12:24 +00:00
|
|
|
$definition->setArguments($this->resolveValue($definition->getArguments()));
|
|
|
|
|
|
|
|
$calls = array();
|
|
|
|
foreach ($definition->getMethodCalls() as $name => $arguments) {
|
|
|
|
$calls[$this->resolveValue($name)] = $this->resolveValue($arguments);
|
|
|
|
}
|
|
|
|
$definition->setMethodCalls($calls);
|
|
|
|
}
|
|
|
|
|
|
|
|
$aliases = array();
|
|
|
|
foreach ($container->getAliases() as $name => $target) {
|
|
|
|
$aliases[$this->resolveValue($name)] = $this->resolveValue($target);
|
|
|
|
}
|
|
|
|
$container->setAliases($aliases);
|
|
|
|
|
|
|
|
$injectors = array();
|
|
|
|
foreach ($container->getInterfaceInjectors() as $class => $injector) {
|
|
|
|
$injector->setClass($this->resolveValue($injector->getClass()));
|
|
|
|
$injectors[$this->resolveValue($class)] = $injector;
|
|
|
|
}
|
|
|
|
$container->setInterfaceInjectors($injectors);
|
2011-01-15 19:46:06 +00:00
|
|
|
|
|
|
|
$parameterBag = $container->getParameterBag();
|
|
|
|
foreach ($parameterBag->all() as $key => $value) {
|
|
|
|
$parameterBag->set($key, $this->resolveValue($value));
|
|
|
|
}
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Expands parameters into their full values
|
|
|
|
*
|
|
|
|
* @param mixed $value The value to resolve
|
|
|
|
* @return mixed The resolved value
|
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
protected function resolveValue($value)
|
|
|
|
{
|
|
|
|
if (is_array($value)) {
|
|
|
|
$resolved = array();
|
|
|
|
foreach ($value as $k => $v) {
|
|
|
|
$resolved[$this->resolveValue($k)] = $this->resolveValue($v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $resolved;
|
|
|
|
} else if (is_string($value)) {
|
|
|
|
return $this->resolveString($value);
|
|
|
|
}
|
2011-02-27 17:27:22 +00:00
|
|
|
|
|
|
|
return $value;
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Resolves parameters inside a string
|
|
|
|
*
|
|
|
|
* @param string $value The string to resolve
|
|
|
|
* @return string The resolved string
|
|
|
|
* @throws \RuntimeException when a given parameter has a type problem.
|
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function resolveString($value)
|
|
|
|
{
|
|
|
|
if (preg_match('/^%[^%]+%$/', $value)) {
|
|
|
|
return $this->resolveValue($this->parameterBag->resolveValue($value));
|
|
|
|
}
|
|
|
|
|
|
|
|
$self = $this;
|
|
|
|
$parameterBag = $this->parameterBag;
|
|
|
|
return preg_replace_callback('/(?<!%)%[^%]+%/',
|
|
|
|
function($parameter) use ($self, $parameterBag) {
|
|
|
|
$resolved = $parameterBag->resolveValue($parameter[0]);
|
|
|
|
if (!is_string($resolved)) {
|
|
|
|
throw new \RuntimeException('You can only embed strings in other parameters.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->resolveString($resolved);
|
|
|
|
},
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
}
|
2010-12-30 16:27:43 +00:00
|
|
|
}
|