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

99 lines
3.0 KiB
PHP
Raw Normal View History

2010-12-29 19:12:24 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.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;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
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>
* @author Nicolas Grekas <p@tchwork.com>
2010-12-29 19:12:24 +00:00
*/
class RemoveUnusedDefinitionsPass extends AbstractRecursivePass implements RepeatablePassInterface
2010-12-29 19:12:24 +00:00
{
private $connectedIds = array();
2011-02-13 18:06:41 +00:00
/**
* {@inheritdoc}
2011-02-13 18:06:41 +00:00
*/
public function setRepeatedPass(RepeatedPass $repeatedPass)
{
@trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED);
}
2011-02-13 18:06:41 +00:00
/**
* Processes the ContainerBuilder to remove unused definitions.
*/
2010-12-29 19:12:24 +00:00
public function process(ContainerBuilder $container)
{
try {
$this->enableExpressionProcessing();
$this->container = $container;
$connectedIds = array();
$aliases = $container->getAliases();
foreach ($aliases as $id => $alias) {
if ($alias->isPublic()) {
$this->connectedIds[] = (string) $aliases[$id];
}
2010-12-29 19:12:24 +00:00
}
foreach ($container->getDefinitions() as $id => $definition) {
if ($definition->isPublic()) {
$connectedIds[$id] = true;
$this->processValue($definition);
}
}
while ($this->connectedIds) {
$ids = $this->connectedIds;
$this->connectedIds = array();
foreach ($ids as $id) {
if (!isset($connectedIds[$id]) && $container->has($id)) {
$connectedIds[$id] = true;
$this->processValue($container->getDefinition($id));
}
}
}
2011-01-05 11:13:27 +00:00
foreach ($container->getDefinitions() as $id => $definition) {
if (!isset($connectedIds[$id])) {
$container->removeDefinition($id);
$container->resolveEnvPlaceholders(serialize($definition));
$container->log($this, sprintf('Removed service "%s"; reason: unused.', $id));
}
2010-12-29 19:12:24 +00:00
}
} finally {
$this->container = null;
$this->connectedIds = array();
2010-12-29 19:12:24 +00:00
}
}
2010-12-29 19:12:24 +00:00
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
{
if (!$value instanceof Reference) {
return parent::processValue($value);
2010-12-29 19:12:24 +00:00
}
if (ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE !== $value->getInvalidBehavior()) {
$this->connectedIds[] = (string) $value;
}
return $value;
2010-12-29 19:12:24 +00:00
}
2011-06-08 11:16:48 +01:00
}