class ProxyServicesPass

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/DependencyInjection/Compiler/ProxyServicesPass.php \Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass
  2. 10 core/lib/Drupal/Core/DependencyInjection/Compiler/ProxyServicesPass.php \Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass
  3. 11.x core/lib/Drupal/Core/DependencyInjection/Compiler/ProxyServicesPass.php \Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass

Replaces all services with a lazy flag.

Hierarchy

  • class \Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass implements \Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface

Expanded class hierarchy of ProxyServicesPass

See also

Lazy Services

2 files declare their use of ProxyServicesPass
CoreServiceProvider.php in core/lib/Drupal/Core/CoreServiceProvider.php
ProxyServicesPassTest.php in core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/ProxyServicesPassTest.php

File

core/lib/Drupal/Core/DependencyInjection/Compiler/ProxyServicesPass.php, line 15

Namespace

Drupal\Core\DependencyInjection\Compiler
View source
class ProxyServicesPass implements CompilerPassInterface {
    
    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container) {
        foreach ($container->getDefinitions() as $service_id => $definition) {
            if ($definition->isLazy()) {
                $proxy_class = ProxyBuilder::buildProxyClassName($definition->getClass());
                if (class_exists($proxy_class)) {
                    // Copy the existing definition to a new entry.
                    $definition->setLazy(FALSE);
                    // Ensure that the service is accessible.
                    $definition->setPublic(TRUE);
                    $new_service_id = 'drupal.proxy_original_service.' . $service_id;
                    $container->setDefinition($new_service_id, $definition);
                    $container->register($service_id, $proxy_class)
                        ->setArguments([
                        new Reference('service_container'),
                        $new_service_id,
                    ]);
                }
                else {
                    $class_name = $definition->getClass();
                    // Find the root namespace.
                    $match = [];
                    preg_match('/([a-zA-Z0-9_]+\\\\[a-zA-Z0-9_]+)\\\\(.+)/', $class_name, $match);
                    $root_namespace = $match[1];
                    // Find the root namespace path.
                    $root_namespace_dir = '[namespace_root_path]';
                    $namespaces = $container->getParameter('container.namespaces');
                    // Hardcode Drupal Core, because it is not registered.
                    $namespaces['Drupal\\Core'] = 'core/lib/Drupal/Core';
                    if (isset($namespaces[$root_namespace])) {
                        $root_namespace_dir = $namespaces[$root_namespace];
                    }
                    $message = <<<EOF

Missing proxy class '{<span class="php-variable">$proxy_class</span>}' for lazy service '{<span class="php-variable">$service_id</span>}'.
Use the following command to generate the proxy class:
  php core/scripts/generate-proxy-class.php '{<span class="php-variable">$class_name</span>}' "{<span class="php-variable">$root_namespace_dir</span>}"


EOF;
                    trigger_error($message, E_USER_WARNING);
                }
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
ProxyServicesPass::process public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.