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.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
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.
|
|
|
|
*/
|
|
|
|
|
2010-12-29 19:12:24 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
|
|
|
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
2018-06-01 17:19:33 +01:00
|
|
|
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>
|
2018-06-01 17:19:33 +01:00
|
|
|
* @author Nicolas Grekas <p@tchwork.com>
|
2010-12-29 19:12:24 +00:00
|
|
|
*/
|
2018-06-01 17:19:33 +01:00
|
|
|
class RemoveUnusedDefinitionsPass extends AbstractRecursivePass implements RepeatablePassInterface
|
2010-12-29 19:12:24 +00:00
|
|
|
{
|
2018-06-01 17:19:33 +01:00
|
|
|
private $connectedIds = array();
|
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)
|
|
|
|
{
|
2018-07-06 13:02:59 +01:00
|
|
|
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED);
|
2011-01-08 08:39:53 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-06-01 17:19:33 +01:00
|
|
|
try {
|
|
|
|
$this->enableExpressionProcessing();
|
|
|
|
$this->container = $container;
|
|
|
|
$connectedIds = array();
|
|
|
|
$aliases = $container->getAliases();
|
2011-01-09 09:53:46 +00:00
|
|
|
|
2018-06-01 17:19:33 +01:00
|
|
|
foreach ($aliases as $id => $alias) {
|
|
|
|
if ($alias->isPublic()) {
|
|
|
|
$this->connectedIds[] = (string) $aliases[$id];
|
|
|
|
}
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 17:19:33 +01:00
|
|
|
foreach ($container->getDefinitions() as $id => $definition) {
|
|
|
|
if ($definition->isPublic()) {
|
|
|
|
$connectedIds[$id] = true;
|
|
|
|
$this->processValue($definition);
|
|
|
|
}
|
|
|
|
}
|
2011-01-09 13:57:17 +00:00
|
|
|
|
2018-06-01 17:19:33 +01:00
|
|
|
while ($this->connectedIds) {
|
|
|
|
$ids = $this->connectedIds;
|
|
|
|
$this->connectedIds = array();
|
|
|
|
foreach ($ids as $id) {
|
2018-07-03 16:15:31 +01:00
|
|
|
if (!isset($connectedIds[$id]) && $container->hasDefinition($id)) {
|
2018-06-01 17:19:33 +01:00
|
|
|
$connectedIds[$id] = true;
|
|
|
|
$this->processValue($container->getDefinition($id));
|
2011-01-09 09:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-05 11:13:27 +00:00
|
|
|
|
2018-06-01 17:19:33 +01: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
|
|
|
}
|
2018-06-01 17:19:33 +01:00
|
|
|
} finally {
|
|
|
|
$this->container = null;
|
|
|
|
$this->connectedIds = array();
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
2018-06-01 17:19:33 +01:00
|
|
|
}
|
2010-12-29 19:12:24 +00:00
|
|
|
|
2018-06-01 17:19:33 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function processValue($value, $isRoot = false)
|
|
|
|
{
|
|
|
|
if (!$value instanceof Reference) {
|
|
|
|
return parent::processValue($value);
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
2018-06-01 17:19:33 +01: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
|
|
|
}
|