Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. RemoveUnusedDefinitionsPass.php

class RemoveUnusedDefinitionsPass

Removes unused service definitions from the container.

@author Johannes M. Schmitt <schmittjoh@gmail.com> @author Nicolas Grekas <p@tchwork.com>

Hierarchy

  • class \Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass implements \Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface
    • class \Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass extends \Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass

Expanded class hierarchy of RemoveUnusedDefinitionsPass

1 file declares its use of RemoveUnusedDefinitionsPass
NormalInstallerServiceProvider.php in core/lib/Drupal/Core/Installer/NormalInstallerServiceProvider.php

File

vendor/symfony/dependency-injection/Compiler/RemoveUnusedDefinitionsPass.php, line 23

Namespace

Symfony\Component\DependencyInjection\Compiler
View source
class RemoveUnusedDefinitionsPass extends AbstractRecursivePass {
    protected bool $skipScalars = true;
    private array $connectedIds = [];
    
    /**
     * Processes the ContainerBuilder to remove unused definitions.
     */
    public function process(ContainerBuilder $container) : void {
        try {
            $this->enableExpressionProcessing();
            $this->container = $container;
            $connectedIds = [];
            $aliases = $container->getAliases();
            foreach ($aliases as $id => $alias) {
                if ($alias->isPublic()) {
                    $this->connectedIds[] = (string) $aliases[$id];
                }
            }
            foreach ($container->getDefinitions() as $id => $definition) {
                if ($definition->isPublic()) {
                    $connectedIds[$id] = true;
                    $this->processValue($definition);
                }
            }
            while ($this->connectedIds) {
                $ids = $this->connectedIds;
                $this->connectedIds = [];
                foreach ($ids as $id) {
                    if (!isset($connectedIds[$id]) && $container->hasDefinition($id)) {
                        $connectedIds[$id] = true;
                        $this->processValue($container->getDefinition($id));
                    }
                }
            }
            foreach ($container->getDefinitions() as $id => $definition) {
                if (!isset($connectedIds[$id])) {
                    $container->removeDefinition($id);
                    $container->resolveEnvPlaceholders(!$definition->hasErrors() ? serialize($definition) : $definition);
                    $container->log($this, \sprintf('Removed service "%s"; reason: unused.', $id));
                }
            }
        } finally {
            $this->container = null;
            $this->connectedIds = [];
        }
    }
    protected function processValue(mixed $value, bool $isRoot = false) : mixed {
        if (!$value instanceof Reference) {
            return parent::processValue($value, $isRoot);
        }
        if (ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE !== $value->getInvalidBehavior()) {
            $this->connectedIds[] = (string) $value;
        }
        return $value;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AbstractRecursivePass::$container protected property
AbstractRecursivePass::$currentId protected property
AbstractRecursivePass::$expressionLanguage private property 1
AbstractRecursivePass::$inExpression private property
AbstractRecursivePass::$processExpressions private property
AbstractRecursivePass::enableExpressionProcessing protected function
AbstractRecursivePass::getConstructor protected function
AbstractRecursivePass::getExpressionLanguage private function 1
AbstractRecursivePass::getReflectionMethod protected function
AbstractRecursivePass::inExpression protected function
RemoveUnusedDefinitionsPass::$connectedIds private property
RemoveUnusedDefinitionsPass::$skipScalars protected property Overrides AbstractRecursivePass::$skipScalars
RemoveUnusedDefinitionsPass::process public function Processes the ContainerBuilder to remove unused definitions. Overrides AbstractRecursivePass::process
RemoveUnusedDefinitionsPass::processValue protected function Processes a value found in a definition tree. Overrides AbstractRecursivePass::processValue
RSS feed
Powered by Drupal