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

99 lines
2.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;
2011-01-05 11:13:27 +00:00
/**
* Inline service definitions where this is possible.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class InlineServiceDefinitionsPass extends AbstractRecursivePass implements RepeatablePassInterface
2011-01-05 11:13:27 +00:00
{
private $repeatedPass;
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
/**
* {@inheritdoc}
2011-02-13 18:06:41 +00:00
*/
protected function processValue($value, $isRoot = false)
2011-01-05 11:13:27 +00:00
{
if ($value instanceof ArgumentInterface) {
// Reference found in ArgumentInterface::getValues() are not inlineable
return $value;
}
if ($value instanceof Reference && $this->container->hasDefinition($id = (string) $value)) {
$definition = $this->container->getDefinition($id);
2011-01-05 11:13:27 +00:00
if ($this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) {
$this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
2011-04-12 23:51:22 +01:00
if ($definition->isShared()) {
return $definition;
2011-01-05 11:13:27 +00:00
}
$value = clone $definition;
2011-01-05 11:13:27 +00:00
}
}
return parent::processValue($value, $isRoot);
2011-01-05 11:13:27 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Checks if the definition is inlineable.
*
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, ServiceReferenceGraph $graph)
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 (!$graph->hasNode($id)) {
return true;
2011-01-05 11:13:27 +00:00
}
if ($this->currentId == $id) {
return false;
}
$ids = array();
foreach ($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
}
}