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

76 lines
2.4 KiB
PHP
Raw Normal View History

2010-12-29 19:12:24 +00:00
<?php
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Alias;
2010-12-29 19:12:24 +00:00
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
2011-01-05 11:13:27 +00:00
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
2010-12-29 19:12:24 +00:00
/**
2011-01-05 11:13:27 +00:00
* Removes unused service definitions from the container.
2010-12-29 19:12:24 +00:00
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class RemoveUnusedDefinitionsPass implements RepeatablePassInterface
2010-12-29 19:12:24 +00:00
{
protected $repeatedPass;
protected $graph;
public function setRepeatedPass(RepeatedPass $repeatedPass)
{
$this->repeatedPass = $repeatedPass;
}
2010-12-29 19:12:24 +00:00
public function process(ContainerBuilder $container)
{
$this->graph = $this->repeatedPass->getCompiler()->getServiceReferenceGraph();
2010-12-29 19:12:24 +00:00
$hasChanged = false;
foreach ($container->getDefinitions() as $id => $definition) {
if ($definition->isPublic()) {
continue;
}
if ($this->graph->hasNode($id)) {
$edges = $this->graph->getNode($id)->getInEdges();
$referencingAliases = array();
$sourceIds = array();
foreach ($edges as $edge) {
$node = $edge->getSourceNode();
$sourceIds[] = $node->getId();
if ($node->isAlias()) {
$referencingAlias[] = $node->getValue();
}
}
$isReferenced = (count(array_unique($sourceIds)) - count($referencingAliases)) > 0;
} else {
$referencingAliases = array();
$isReferenced = false;
}
2011-01-05 11:13:27 +00:00
if (1 === count($referencingAliases) && false === $isReferenced) {
$container->setDefinition((string) reset($referencingAliases), $definition);
2011-01-05 11:13:27 +00:00
$definition->setPublic(true);
$container->remove($id);
} else if (0 === count($referencingAliases) && false === $isReferenced) {
2010-12-29 19:12:24 +00:00
$container->remove($id);
$hasChanged = true;
}
}
if ($hasChanged) {
$this->repeatedPass->setRepeat();
2010-12-29 19:12:24 +00:00
}
}
}