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

186 lines
6.6 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\ChildDefinition;
2011-01-26 23:14:31 +00:00
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2011-01-26 23:14:31 +00:00
/**
* This replaces all ChildDefinition instances with their equivalent fully
2011-01-26 23:14:31 +00:00
* merged Definition instance.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
2011-01-26 23:14:31 +00:00
*/
class ResolveDefinitionTemplatesPass extends AbstractRecursivePass implements CompilerPassInterface
2011-01-26 23:14:31 +00:00
{
protected function processValue($value, $isRoot = false)
{
if (!$value instanceof Definition) {
return parent::processValue($value, $isRoot);
}
if ($isRoot) {
// yes, we are specifically fetching the definition from the
// container to ensure we are not operating on stale data
$value = $this->container->getDefinition($this->currentId);
}
if ($value instanceof ChildDefinition) {
$value = $this->resolveDefinition($value);
if ($isRoot) {
$this->container->setDefinition($this->currentId, $value);
}
2011-01-26 23:14:31 +00:00
}
return parent::processValue($value, $isRoot);
2011-01-26 23:14:31 +00:00
}
2011-02-13 18:06:41 +00:00
/**
2014-12-21 17:00:50 +00:00
* Resolves the definition.
2011-02-13 18:06:41 +00:00
*
* @return Definition
*
* @throws RuntimeException When the definition is invalid
2011-02-13 18:06:41 +00:00
*/
private function resolveDefinition(ChildDefinition $definition)
{
try {
return $this->doResolveDefinition($definition);
} catch (ExceptionInterface $e) {
$r = new \ReflectionProperty($e, 'message');
$r->setAccessible(true);
$r->setValue($e, sprintf('Service "%s": %s', $this->currentId, $e->getMessage()));
throw $e;
}
}
private function doResolveDefinition(ChildDefinition $definition)
2011-01-26 23:14:31 +00:00
{
if (!$this->container->has($parent = $definition->getParent())) {
throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
2011-01-26 23:14:31 +00:00
}
$parentDef = $this->container->findDefinition($parent);
if ($parentDef instanceof ChildDefinition) {
$id = $this->currentId;
$this->currentId = $parent;
$parentDef = $this->resolveDefinition($parentDef);
$this->container->setDefinition($parent, $parentDef);
$this->currentId = $id;
2011-01-26 23:14:31 +00:00
}
$compiler = $this->container->getCompiler();
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatResolveInheritance($this, $this->currentId, $parent));
2011-01-26 23:14:31 +00:00
$def = new Definition();
// merge in parent definition
2015-09-04 20:54:37 +01:00
// purposely ignored attributes: abstract, tags
2011-01-26 23:14:31 +00:00
$def->setClass($parentDef->getClass());
$def->setArguments($parentDef->getArguments());
$def->setMethodCalls($parentDef->getMethodCalls());
$def->setProperties($parentDef->getProperties());
$def->setAutowiringTypes($parentDef->getAutowiringTypes());
if ($parentDef->isDeprecated()) {
$def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
}
$def->setFactory($parentDef->getFactory());
2011-01-26 23:14:31 +00:00
$def->setConfigurator($parentDef->getConfigurator());
$def->setFile($parentDef->getFile());
$def->setPublic($parentDef->isPublic());
2013-07-25 06:55:15 +01:00
$def->setLazy($parentDef->isLazy());
$def->setAutowiredMethods($parentDef->getAutowiredMethods());
2011-01-26 23:14:31 +00:00
// overwrite with values specified in the decorator
$changes = $definition->getChanges();
if (isset($changes['class'])) {
$def->setClass($definition->getClass());
}
if (isset($changes['factory'])) {
$def->setFactory($definition->getFactory());
}
2011-01-26 23:14:31 +00:00
if (isset($changes['configurator'])) {
$def->setConfigurator($definition->getConfigurator());
}
if (isset($changes['file'])) {
$def->setFile($definition->getFile());
}
if (isset($changes['public'])) {
$def->setPublic($definition->isPublic());
}
2013-10-30 08:30:20 +00:00
if (isset($changes['lazy'])) {
2013-07-25 06:55:15 +01:00
$def->setLazy($definition->isLazy());
}
if (isset($changes['deprecated'])) {
$def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
}
if (isset($changes['autowired_methods'])) {
$def->setAutowiredMethods($definition->getAutowiredMethods());
}
if (isset($changes['decorated_service'])) {
$decoratedService = $definition->getDecoratedService();
if (null === $decoratedService) {
$def->setDecoratedService($decoratedService);
} else {
$def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
}
}
2011-01-26 23:14:31 +00:00
// 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 = (int) 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 ($calls = $definition->getMethodCalls()) {
2011-01-26 23:14:31 +00:00
$def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
}
// merge autowiring types
foreach ($definition->getAutowiringTypes() as $autowiringType) {
$def->addAutowiringType($autowiringType);
}
2011-01-26 23:14:31 +00:00
// these attributes are always taken from the child
$def->setAbstract($definition->isAbstract());
$def->setShared($definition->isShared());
2011-01-26 23:14:31 +00:00
$def->setTags($definition->getTags());
// append parent tags when inheriting is enabled
if ($definition->getInheritTags()) {
foreach ($parentDef->getTags() as $k => $v) {
foreach ($v as $v) {
$def->addTag($k, $v);
}
}
}
2011-01-26 23:14:31 +00:00
return $def;
}
2011-03-09 13:38:54 +00:00
}