3 namespace Drupal\Core\DependencyInjection\Compiler;
5 use Drupal\Component\ProxyBuilder\ProxyBuilder;
6 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7 use Symfony\Component\DependencyInjection\ContainerBuilder;
8 use Symfony\Component\DependencyInjection\Reference;
11 * Replaces all services with a lazy flag.
13 class ProxyServicesPass implements CompilerPassInterface {
18 public function process(ContainerBuilder $container) {
19 foreach ($container->getDefinitions() as $service_id => $definition) {
20 if ($definition->isLazy()) {
21 $proxy_class = ProxyBuilder::buildProxyClassName($definition->getClass());
22 if (class_exists($proxy_class)) {
23 // Copy the existing definition to a new entry.
24 $definition->setLazy(FALSE);
25 // Ensure that the service is accessible.
26 $definition->setPublic(TRUE);
27 $new_service_id = 'drupal.proxy_original_service.' . $service_id;
28 $container->setDefinition($new_service_id, $definition);
30 $container->register($service_id, $proxy_class)
31 ->setArguments([new Reference('service_container'), $new_service_id]);
34 $class_name = $definition->getClass();
36 // Find the root namespace.
38 preg_match('/([a-zA-Z0-9_]+\\\\[a-zA-Z0-9_]+)\\\\(.+)/', $class_name, $match);
39 $root_namespace = $match[1];
41 // Find the root namespace path.
42 $root_namespace_dir = '[namespace_root_path]';
44 $namespaces = $container->getParameter('container.namespaces');
46 // Hardcode Drupal Core, because it is not registered.
47 $namespaces['Drupal\Core'] = 'core/lib/Drupal/Core';
49 if (isset($namespaces[$root_namespace])) {
50 $root_namespace_dir = $namespaces[$root_namespace];
55 Missing proxy class '$proxy_class' for lazy service '$service_id'.
56 Use the following command to generate the proxy class:
57 php core/scripts/generate-proxy-class.php '$class_name' "$root_namespace_dir"
61 trigger_error($message, E_USER_WARNING);