ProxyServicesPass.php 2.2 KB

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