2010-12-29 19:12:24 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-12-29 19:12:24 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
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
|
|
|
* Removes unused service definitions from the container.
|
2010-12-29 19:12:24 +00:00
|
|
|
*
|
|
|
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
|
|
|
*/
|
2011-01-08 08:39:53 +00:00
|
|
|
class RemoveUnusedDefinitionsPass implements RepeatablePassInterface
|
2010-12-29 19:12:24 +00:00
|
|
|
{
|
2011-01-08 08:39:53 +00:00
|
|
|
protected $repeatedPass;
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2011-01-08 08:39:53 +00:00
|
|
|
public function setRepeatedPass(RepeatedPass $repeatedPass)
|
|
|
|
{
|
|
|
|
$this->repeatedPass = $repeatedPass;
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Processes the ContainerBuilder to remove unused definitions.
|
|
|
|
*
|
|
|
|
* @param ContainerBuilder $container
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function process(ContainerBuilder $container)
|
|
|
|
{
|
2011-01-17 22:28:59 +00:00
|
|
|
$graph = $container->getCompiler()->getServiceReferenceGraph();
|
2011-01-09 09:53:46 +00:00
|
|
|
|
2010-12-29 19:12:24 +00:00
|
|
|
$hasChanged = false;
|
|
|
|
foreach ($container->getDefinitions() as $id => $definition) {
|
|
|
|
if ($definition->isPublic()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-17 22:28:59 +00:00
|
|
|
if ($graph->hasNode($id)) {
|
|
|
|
$edges = $graph->getNode($id)->getInEdges();
|
2011-01-09 09:53:46 +00:00
|
|
|
$referencingAliases = array();
|
2011-01-09 13:57:17 +00:00
|
|
|
$sourceIds = array();
|
2011-01-09 09:53:46 +00:00
|
|
|
foreach ($edges as $edge) {
|
|
|
|
$node = $edge->getSourceNode();
|
2011-01-09 13:57:17 +00:00
|
|
|
$sourceIds[] = $node->getId();
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
if ($node->isAlias()) {
|
|
|
|
$referencingAlias[] = $node->getValue();
|
|
|
|
}
|
|
|
|
}
|
2011-01-09 13:57:17 +00:00
|
|
|
$isReferenced = (count(array_unique($sourceIds)) - count($referencingAliases)) > 0;
|
2011-01-09 09:53:46 +00:00
|
|
|
} else {
|
|
|
|
$referencingAliases = array();
|
|
|
|
$isReferenced = false;
|
|
|
|
}
|
2011-01-05 11:13:27 +00:00
|
|
|
|
|
|
|
if (1 === count($referencingAliases) && false === $isReferenced) {
|
2011-01-09 09:53:46 +00:00
|
|
|
$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) {
|
2011-01-08 08:39:53 +00:00
|
|
|
$this->repeatedPass->setRepeat();
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|