2011-01-05 11:13:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-05 11:13:27 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-05 11:13:27 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +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
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
|
|
|
|
2011-01-17 22:28:59 +00:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2011-01-15 13:29:43 +00:00
|
|
|
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>
|
|
|
|
*/
|
2011-01-08 08:39:53 +00:00
|
|
|
class InlineServiceDefinitionsPass implements RepeatablePassInterface
|
2011-01-05 11:13:27 +00:00
|
|
|
{
|
2011-03-11 13:50:46 +00:00
|
|
|
private $repeatedPass;
|
|
|
|
private $graph;
|
2011-04-12 23:51:22 +01:00
|
|
|
private $compiler;
|
|
|
|
private $formatter;
|
|
|
|
private $currentId;
|
2011-01-08 08:39:53 +00:00
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-08 08:39:53 +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.
|
|
|
|
*
|
2011-03-01 12:44:42 +00:00
|
|
|
* @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())
|
|
|
|
);
|
2011-03-04 14:26:00 +00:00
|
|
|
|
|
|
|
$definition->setProperties(
|
|
|
|
$this->inlineArguments($container, $definition->getProperties())
|
|
|
|
);
|
2013-11-02 12:45:44 +00:00
|
|
|
|
|
|
|
$configurator = $this->inlineArguments($container, array($definition->getConfigurator()));
|
2014-09-24 07:35:53 +01:00
|
|
|
$definition->setConfigurator($configurator[0]);
|
2014-04-13 16:18:39 +01:00
|
|
|
|
|
|
|
$factory = $this->inlineArguments($container, array($definition->getFactory()));
|
2014-09-24 07:35:53 +01:00
|
|
|
$definition->setFactory($factory[0]);
|
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
|
2012-09-07 23:56:45 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-03-11 13:50:46 +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))) {
|
2011-04-14 20:01:37 +01:00
|
|
|
$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()) {
|
2011-01-09 13:57:17 +00:00
|
|
|
$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) {
|
2011-01-09 13:57:17 +00:00
|
|
|
$argument->setArguments($this->inlineArguments($container, $argument->getArguments()));
|
|
|
|
$argument->setMethodCalls($this->inlineArguments($container, $argument->getMethodCalls()));
|
2011-03-04 14:26:00 +00:00
|
|
|
$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.
|
|
|
|
*
|
2011-03-01 12:44:42 +00:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:39:53 +01:00
|
|
|
if ($definition->isPublic() || $definition->isLazy()) {
|
2011-01-05 11:13:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
if (!$this->graph->hasNode($id)) {
|
|
|
|
return true;
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-29 11:49:18 +00:00
|
|
|
if ($this->currentId == $id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
$ids = array();
|
|
|
|
foreach ($this->graph->getNode($id)->getInEdges() as $edge) {
|
|
|
|
$ids[] = $edge->getSourceNode()->getId();
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 12:44:42 +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;
|
|
|
|
}
|
|
|
|
|
2014-11-03 11:58:57 +00:00
|
|
|
if (count($ids) > 1 && $definition->getFactoryService()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-01 12:44:42 +00:00
|
|
|
return $container->getDefinition(reset($ids))->getScope() === $definition->getScope();
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
2011-02-04 18:10:13 +00:00
|
|
|
}
|