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

152 lines
4.8 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;
2011-01-17 22:28:59 +00:00
use Symfony\Component\DependencyInjection\ContainerInterface;
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();
foreach ($container->getDefinitions() as $id => $definition) {
$this->currentId = $id;
2011-01-07 14:44:29 +00:00
2011-01-05 11:13:27 +00:00
$definition->setArguments(
$this->inlineArguments($container, $definition->getArguments())
);
$definition->setMethodCalls(
$this->inlineArguments($container, $definition->getMethodCalls())
);
$definition->setProperties(
$this->inlineArguments($container, $definition->getProperties())
);
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
*
* @return array
2011-02-13 18:06:41 +00:00
*/
private function inlineArguments(ContainerBuilder $container, array $arguments)
2011-01-05 11:13:27 +00:00
{
foreach ($arguments as $k => $argument) {
if (is_array($argument)) {
$arguments[$k] = $this->inlineArguments($container, $argument);
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;
}
2012-07-28 23:02:29 +01:00
if ($this->isInlineableDefinition($container, $id, $definition = $container->getDefinition($id))) {
$this->compiler->addLogMessage($this->formatter->formatInlineService($this, $id, $this->currentId));
2011-04-12 23:51:22 +01:00
2011-01-17 22:28:59 +00:00
if (ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope()) {
$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()));
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 ContainerBuilder $container
2011-04-23 16:05:44 +01:00
* @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
*/
2012-07-28 23:02:29 +01:00
private function isInlineableDefinition(ContainerBuilder $container, $id, Definition $definition)
2011-01-05 11:13:27 +00:00
{
2011-01-17 22:28:59 +00:00
if (ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope()) {
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;
}
if (count($ids) > 1 && $definition->getFactoryService()) {
return false;
}
return $container->getDefinition(reset($ids))->getScope() === $definition->getScope();
2011-01-05 11:13:27 +00:00
}
}