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/InlineServiceDefinitionsPass.php

145 lines
4.7 KiB
PHP
Raw Normal View History

2011-01-05 11:13:27 +00:00
<?php
/*
* This file is part of the Symfony package.
2011-01-05 11:13:27 +00:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2011-01-05 11:13:27 +00:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2011-01-05 11:13:27 +00:00
*/
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
2011-01-05 11:13:27 +00:00
/**
* Inline service definitions where this is possible.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class InlineServiceDefinitionsPass implements RepeatablePassInterface
2011-01-05 11:13:27 +00:00
{
private $repeatedPass;
private $graph;
2011-04-12 23:51:22 +01:00
private $compiler;
private $formatter;
private $currentId;
2011-02-13 18:06:41 +00:00
/**
* {@inheritdoc}
2011-02-13 18:06:41 +00:00
*/
public function setRepeatedPass(RepeatedPass $repeatedPass)
{
$this->repeatedPass = $repeatedPass;
}
2011-01-07 14:44:29 +00:00
2011-02-13 18:06:41 +00:00
/**
* Processes the ContainerBuilder for inline service definitions.
*
* @param ContainerBuilder $container
2011-02-13 18:06:41 +00:00
*/
2011-01-05 11:13:27 +00:00
public function process(ContainerBuilder $container)
{
2011-04-12 23:51:22 +01:00
$this->compiler = $container->getCompiler();
$this->formatter = $this->compiler->getLoggingFormatter();
$this->graph = $this->compiler->getServiceReferenceGraph();
$container->setDefinitions($this->inlineArguments($container, $container->getDefinitions(), true));
2011-01-05 11:13:27 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Processes inline arguments.
*
* @param ContainerBuilder $container The ContainerBuilder
2011-04-23 16:05:44 +01:00
* @param array $arguments An array of arguments
* @param bool $isRoot If we are processing the root definitions or not
*
* @return array
2011-02-13 18:06:41 +00:00
*/
private function inlineArguments(ContainerBuilder $container, array $arguments, $isRoot = false)
2011-01-05 11:13:27 +00:00
{
foreach ($arguments as $k => $argument) {
if ($isRoot) {
$this->currentId = $k;
}
2011-01-05 11:13:27 +00:00
if (is_array($argument)) {
$arguments[$k] = $this->inlineArguments($container, $argument);
} elseif ($argument instanceof ArgumentInterface) {
$argument->setValues($this->inlineArguments($container, $argument->getValues()));
2011-12-18 13:42:59 +00:00
} elseif ($argument instanceof Reference) {
2011-01-05 11:13:27 +00:00
if (!$container->hasDefinition($id = (string) $argument)) {
continue;
}
if ($this->isInlineableDefinition($id, $definition = $container->getDefinition($id))) {
$this->compiler->addLogMessage($this->formatter->formatInlineService($this, $id, $this->currentId));
2011-04-12 23:51:22 +01:00
2015-09-04 20:54:37 +01:00
if ($definition->isShared()) {
$arguments[$k] = $definition;
} else {
$arguments[$k] = clone $definition;
}
2011-01-05 11:13:27 +00:00
}
2011-12-18 13:42:59 +00:00
} elseif ($argument instanceof Definition) {
$argument->setArguments($this->inlineArguments($container, $argument->getArguments()));
$argument->setMethodCalls($this->inlineArguments($container, $argument->getMethodCalls()));
$argument->setProperties($this->inlineArguments($container, $argument->getProperties()));
$configurator = $this->inlineArguments($container, array($argument->getConfigurator()));
$argument->setConfigurator($configurator[0]);
$factory = $this->inlineArguments($container, array($argument->getFactory()));
$argument->setFactory($factory[0]);
2011-01-05 11:13:27 +00:00
}
}
return $arguments;
}
2011-02-13 18:06:41 +00:00
/**
* Checks if the definition is inlineable.
*
* @param string $id
* @param Definition $definition
2011-12-13 07:50:54 +00:00
*
2014-11-30 13:33:44 +00:00
* @return bool If the definition is inlineable
2011-02-13 18:06:41 +00:00
*/
private function isInlineableDefinition($id, Definition $definition)
2011-01-05 11:13:27 +00:00
{
2015-09-04 20:54:37 +01:00
if (!$definition->isShared()) {
2011-01-05 11:13:27 +00:00
return true;
}
if ($definition->isPublic() || $definition->isLazy()) {
2011-01-05 11:13:27 +00:00
return false;
}
if (!$this->graph->hasNode($id)) {
return true;
2011-01-05 11:13:27 +00:00
}
if ($this->currentId == $id) {
return false;
}
$ids = array();
foreach ($this->graph->getNode($id)->getInEdges() as $edge) {
$ids[] = $edge->getSourceNode()->getId();
2011-01-05 11:13:27 +00:00
}
if (count(array_unique($ids)) > 1) {
return false;
}
2015-01-04 20:27:00 +00:00
if (count($ids) > 1 && is_array($factory = $definition->getFactory()) && ($factory[0] instanceof Reference || $factory[0] instanceof Definition)) {
return false;
}
2015-09-04 20:54:37 +01:00
return true;
2011-01-05 11:13:27 +00:00
}
}