This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php

149 lines
5.2 KiB
PHP
Raw Normal View History

2011-01-26 23:14:31 +00:00
<?php
2011-05-31 09:57:06 +01:00
/*
2012-03-31 22:00:32 +01:00
* This file is part of the Symfony package.
2011-05-31 09:57:06 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
*
2012-03-31 22:00:32 +01:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2011-05-31 09:57:06 +01:00
*/
2011-01-26 23:14:31 +00:00
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2011-01-26 23:14:31 +00:00
/**
* This replaces all DefinitionDecorator instances with their equivalent fully
* merged Definition instance.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ResolveDefinitionTemplatesPass implements CompilerPassInterface
{
private $container;
private $compiler;
private $formatter;
2011-01-26 23:14:31 +00:00
2011-02-13 18:06:41 +00:00
/**
* Process the ContainerBuilder to replace DefinitionDecorator instances with their real Definition instances.
*
* @param ContainerBuilder $container
2011-02-13 18:06:41 +00:00
*/
2011-01-26 23:14:31 +00:00
public function process(ContainerBuilder $container)
{
$this->container = $container;
$this->compiler = $container->getCompiler();
$this->formatter = $this->compiler->getLoggingFormatter();
2011-01-26 23:14:31 +00:00
foreach (array_keys($container->getDefinitions()) as $id) {
// yes, we are specifically fetching the definition from the
// container to ensure we are not operating on stale data
$definition = $container->getDefinition($id);
2011-03-09 13:38:54 +00:00
if (!$definition instanceof DefinitionDecorator || $definition->isAbstract()) {
2011-01-26 23:14:31 +00:00
continue;
}
$this->resolveDefinition($id, $definition);
}
}
2011-02-13 18:06:41 +00:00
/**
* Resolves the definition
*
2012-05-15 21:19:31 +01:00
* @param string $id The definition identifier
* @param DefinitionDecorator $definition
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return Definition
*/
private function resolveDefinition($id, DefinitionDecorator $definition)
2011-01-26 23:14:31 +00:00
{
if (!$this->container->hasDefinition($parent = $definition->getParent())) {
throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $id));
2011-01-26 23:14:31 +00:00
}
$parentDef = $this->container->getDefinition($parent);
if ($parentDef instanceof DefinitionDecorator) {
$parentDef = $this->resolveDefinition($parent, $parentDef);
}
$this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $id, $parent));
2011-01-26 23:14:31 +00:00
$def = new Definition();
// merge in parent definition
// purposely ignored attributes: scope, abstract, tags
$def->setClass($parentDef->getClass());
$def->setArguments($parentDef->getArguments());
$def->setMethodCalls($parentDef->getMethodCalls());
$def->setProperties($parentDef->getProperties());
$def->setFactoryClass($parentDef->getFactoryClass());
2011-01-26 23:14:31 +00:00
$def->setFactoryMethod($parentDef->getFactoryMethod());
$def->setFactoryService($parentDef->getFactoryService());
2011-01-26 23:14:31 +00:00
$def->setConfigurator($parentDef->getConfigurator());
$def->setFile($parentDef->getFile());
$def->setPublic($parentDef->isPublic());
// overwrite with values specified in the decorator
$changes = $definition->getChanges();
if (isset($changes['class'])) {
$def->setClass($definition->getClass());
}
if (isset($changes['factory_class'])) {
$def->setFactoryClass($definition->getFactoryClass());
}
2011-01-26 23:14:31 +00:00
if (isset($changes['factory_method'])) {
$def->setFactoryMethod($definition->getFactoryMethod());
}
if (isset($changes['factory_service'])) {
$def->setFactoryService($definition->getFactoryService());
}
if (isset($changes['configurator'])) {
$def->setConfigurator($definition->getConfigurator());
}
if (isset($changes['file'])) {
$def->setFile($definition->getFile());
}
if (isset($changes['public'])) {
$def->setPublic($definition->isPublic());
}
// merge arguments
foreach ($definition->getArguments() as $k => $v) {
if (is_numeric($k)) {
$def->addArgument($v);
continue;
}
if (0 !== strpos($k, 'index_')) {
throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
2011-01-26 23:14:31 +00:00
}
$index = (integer) substr($k, strlen('index_'));
$def->replaceArgument($index, $v);
2011-01-26 23:14:31 +00:00
}
// merge properties
foreach ($definition->getProperties() as $k => $v) {
$def->setProperty($k, $v);
}
2011-01-26 23:14:31 +00:00
// append method calls
if (count($calls = $definition->getMethodCalls()) > 0) {
$def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
}
// these attributes are always taken from the child
$def->setAbstract($definition->isAbstract());
$def->setScope($definition->getScope());
$def->setTags($definition->getTags());
// set new definition on container
$this->container->setDefinition($id, $def);
return $def;
}
2011-03-09 13:38:54 +00:00
}