ResolveDefinitionTemplatesPass.php in Service Container 7.2
File
modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php
View source
<?php
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;
class ResolveDefinitionTemplatesPass implements CompilerPassInterface {
private $container;
private $compiler;
private $formatter;
public function process(ContainerBuilder $container) {
$this->container = $container;
$this->compiler = $container
->getCompiler();
$this->formatter = $this->compiler
->getLoggingFormatter();
foreach ($container
->getDefinitions() as $id => $definition) {
$definition = $container
->getDefinition($id);
if (!$definition instanceof DefinitionDecorator || $definition
->isAbstract()) {
continue;
}
$this
->resolveDefinition($id, $definition);
}
}
private function resolveDefinition($id, DefinitionDecorator $definition) {
if (!$this->container
->hasDefinition($parent = $definition
->getParent())) {
throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $id));
}
$parentDef = $this->container
->getDefinition($parent);
if ($parentDef instanceof DefinitionDecorator) {
$parentDef = $this
->resolveDefinition($parent, $parentDef);
}
$this->compiler
->addLogMessage($this->formatter
->formatResolveInheritance($this, $id, $parent));
$def = new Definition();
$def
->setClass($parentDef
->getClass());
$def
->setArguments($parentDef
->getArguments());
$def
->setMethodCalls($parentDef
->getMethodCalls());
$def
->setProperties($parentDef
->getProperties());
if ($parentDef
->getFactoryClass(false)) {
$def
->setFactoryClass($parentDef
->getFactoryClass(false));
}
if ($parentDef
->getFactoryMethod(false)) {
$def
->setFactoryMethod($parentDef
->getFactoryMethod(false));
}
if ($parentDef
->getFactoryService(false)) {
$def
->setFactoryService($parentDef
->getFactoryService(false));
}
$def
->setFactory($parentDef
->getFactory());
$def
->setConfigurator($parentDef
->getConfigurator());
$def
->setFile($parentDef
->getFile());
$def
->setPublic($parentDef
->isPublic());
$def
->setLazy($parentDef
->isLazy());
$changes = $definition
->getChanges();
if (isset($changes['class'])) {
$def
->setClass($definition
->getClass());
}
if (isset($changes['factory_class'])) {
$def
->setFactoryClass($definition
->getFactoryClass(false));
}
if (isset($changes['factory_method'])) {
$def
->setFactoryMethod($definition
->getFactoryMethod(false));
}
if (isset($changes['factory_service'])) {
$def
->setFactoryService($definition
->getFactoryService(false));
}
if (isset($changes['factory'])) {
$def
->setFactory($definition
->getFactory());
}
if (isset($changes['configurator'])) {
$def
->setConfigurator($definition
->getConfigurator());
}
if (isset($changes['file'])) {
$def
->setFile($definition
->getFile());
}
if (isset($changes['public'])) {
$def
->setPublic($definition
->isPublic());
}
if (isset($changes['lazy'])) {
$def
->setLazy($definition
->isLazy());
}
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));
}
$index = (int) substr($k, strlen('index_'));
$def
->replaceArgument($index, $v);
}
foreach ($definition
->getProperties() as $k => $v) {
$def
->setProperty($k, $v);
}
if (count($calls = $definition
->getMethodCalls()) > 0) {
$def
->setMethodCalls(array_merge($def
->getMethodCalls(), $calls));
}
$def
->setAbstract($definition
->isAbstract());
$def
->setScope($definition
->getScope());
$def
->setTags($definition
->getTags());
$this->container
->setDefinition($id, $def);
return $def;
}
}