Version 1
[yaffs-website] / web / core / lib / Drupal / Core / DependencyInjection / Compiler / ProxyServicesPass.php
1 <?php
2
3 namespace Drupal\Core\DependencyInjection\Compiler;
4
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;
9
10 /**
11  * Replaces all services with a lazy flag.
12  */
13 class ProxyServicesPass implements CompilerPassInterface {
14
15   /**
16    * {@inheritdoc}
17    */
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);
29
30           $container->register($service_id, $proxy_class)
31             ->setArguments([new Reference('service_container'), $new_service_id]);
32         }
33         else {
34           $class_name = $definition->getClass();
35
36           // Find the root namespace.
37           $match = [];
38           preg_match('/([a-zA-Z0-9_]+\\\\[a-zA-Z0-9_]+)\\\\(.+)/', $class_name, $match);
39           $root_namespace = $match[1];
40
41           // Find the root namespace path.
42           $root_namespace_dir = '[namespace_root_path]';
43
44           $namespaces = $container->getParameter('container.namespaces');
45
46           // Hardcode Drupal Core, because it is not registered.
47           $namespaces['Drupal\Core'] = 'core/lib/Drupal/Core';
48
49           if (isset($namespaces[$root_namespace])) {
50             $root_namespace_dir = $namespaces[$root_namespace];
51           }
52
53           $message = <<<EOF
54
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"
58
59
60 EOF;
61           trigger_error($message, E_USER_WARNING);
62         }
63       }
64     }
65   }
66
67 }