diff --git a/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php b/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php index 07e30ee..db3278a 100644 --- a/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php +++ b/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php @@ -37,13 +37,17 @@ public function isProxyCandidate(Definition $definition) { * {@inheritdoc} */ public function getProxyFactoryCode(Definition $definition, $id) { - $id = var_export($id, TRUE); // Note: the specific get method is called initially with $lazyLoad=TRUE; // When you want to retrieve the actual service, the code generated in // ProxyBuilder calls the method with lazy loading disabled. - $output = " if (\$lazyLoad) {\n"; - $output .= " return \$this->services[$id] = new " . $this->builder->buildProxyClassName($definition->getClass()) . '(' . $id . ");\n"; - $output .= " }\n"; + $output = <<<'EOS' + if ($lazyLoad) { + return $this->services['{{ id }}'] = new {{ class_name }}('{{ id }}'); + } + +EOS; + $output = str_replace('{{ id }}', $id, $output); + $output = str_replace('{{ class_name }}', $this->builder->buildProxyClassName($definition->getClass()), $output); return $output; } diff --git a/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php b/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php index e284902..c8acb00 100644 --- a/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php +++ b/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php @@ -161,33 +161,41 @@ public function testMethod($parameter) { */ protected function buildExpectedClass($class, $expected_methods_body, $interface_string = ' ') { $proxy_class = $this->proxyBuilder->buildProxyClassName($class); - $expected_string = "class $proxy_class $interface_string { + $expected_string = <<<'EOS' +class {{ proxy_class }} {{ interface_string }} { /** * @var string */ - protected \$serviceId; + protected $serviceId; /** - * @var \\$class + * @var \{{ class }} */ - protected \$service; + protected $service; - public function __construct(\$service_id) { - \$this->serviceId = \$service_id; + public function __construct($service_id) { + $this->serviceId = $service_id; } protected function lazyLoadItself() { - if (!isset(\$this->service)) { - \$method_name = 'get' . Container::camelize(\$this->serviceId) . 'Service'; - \$this->service = \\Drupal::getContainer()->\$method_name(FALSE); + if (!isset($this->service)) { + $method_name = 'get' . Container::camelize($this->serviceId) . 'Service'; + $this->service = \Drupal::getContainer()->$method_name(FALSE); } - return \$this->service; + return $this->service; } -$expected_methods_body + +{{ expected_methods_body }} + } -"; + +EOS; + $expected_string = str_replace('{{ proxy_class }}', $proxy_class, $expected_string); + $expected_string = str_replace('{{ class }}', $class, $expected_string); + $expected_string = str_replace('{{ expected_methods_body }}', $expected_methods_body, $expected_string); + $expected_string = str_replace('{{ interface_string }}', $interface_string, $expected_string); return $expected_string; }