diff --git a/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php b/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php index 37216da..bce65dc 100644 --- a/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php +++ b/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php @@ -37,20 +37,26 @@ public static function buildProxyClassName($class_name) { public function build($class_name) { $reflection = new \ReflectionClass($class_name); - // The actual class; $output = ''; - $class_start = 'class ' . $this->buildProxyClassName($class_name); + $class_documentation = <<<'EOS' +/** + * Provides a proxy class for \{{ class_name }}. + * + * @see \Drupal\Component\ProxyBuilder + */ + +EOS; + $class_start = 'class {{ proxy_class_name }}'; if ($interfaces = $reflection->getInterfaceNames()) { foreach ($interfaces as &$interface) { $interface = '\\' . $interface; } - $output .= ' implements ' . implode(', ', $interfaces); + $class_start .= ' implements ' . implode(', ', $interfaces); } + // The actual class; $properties = <<<'EOS' - { - /** * @var string */ @@ -64,8 +70,6 @@ public function build($class_name) { EOS; - $properties = str_replace('{{ class_name }}', $class_name, $properties); - $output .= $properties; // Add all the methods. @@ -96,7 +100,12 @@ public function build($class_name) { return " $value"; }, explode("\n", $output))); - return $class_start . $output . "\n}\n"; + $final_output = $class_documentation . $class_start . "\n{\n\n" . $output . "\n}\n"; + + $final_output = str_replace('{{ class_name }}', $class_name, $final_output); + $final_output = str_replace('{{ proxy_class_name }}', $this->buildProxyClassName($class_name), $final_output); + + return $final_output; } /** @@ -106,13 +115,14 @@ public function build($class_name) { */ protected function buildLazyLoadItselfMethod() { $output = <<<'EOS' -protected function lazyLoadItself() { - if (!isset($this->service)) { - $method_name = 'get' . Container::camelize($this->serviceId) . 'Service'; - $this->service = \Drupal::getContainer()->$method_name(FALSE); - } +protected function lazyLoadItself() +{ + if (!isset($this->service)) { + $method_name = 'get' . Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } - return $this->service; + return $this->service; } EOS; @@ -147,7 +157,7 @@ protected function buildMethod(\ReflectionMethod $reflection_method) { $signature_line .= implode(', ', $parameters); $signature_line .= ')'; - $output = $signature_line . ' {' . "\n"; + $output = $signature_line . "\n{\n"; $output .= $this->buildMethodBody($reflection_method); @@ -204,11 +214,11 @@ protected function buildMethodBody(\ReflectionMethod $reflection_method) { $function_name = $reflection_method->getName(); if (!$reflection_method->isStatic()) { - $output .= ' return $this->lazyLoadItself()->' . $function_name . '('; + $output .= ' return $this->lazyLoadItself()->' . $function_name . '('; } else { $class_name = $reflection_method->getDeclaringClass()->getName(); - $output .= " \\$class_name::$function_name("; + $output .= " \\$class_name::$function_name("; } // Add parameters; @@ -229,8 +239,10 @@ protected function buildMethodBody(\ReflectionMethod $reflection_method) { */ protected function buildConstructorMethod() { $output = <<<'EOS' -public function __construct($service_id) { - $this->serviceId = $service_id; +public function __construct(ContainerInterface $container, $serviceId) +{ + $this->container = $container; + $this->serviceId = $serviceId; } EOS; diff --git a/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php b/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php index db3278a..99eae59 100644 --- a/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php +++ b/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php @@ -42,7 +42,7 @@ public function getProxyFactoryCode(Definition $definition, $id) { // ProxyBuilder calls the method with lazy loading disabled. $output = <<<'EOS' if ($lazyLoad) { - return $this->services['{{ id }}'] = new {{ class_name }}('{{ id }}'); + return $this->services['{{ id }}'] = new {{ class_name }}($this, '{{ id }}'); } EOS; diff --git a/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php b/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php index f08a6b7..13c2478 100644 --- a/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php +++ b/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php @@ -65,8 +65,9 @@ public function testBuildSimpleMethod() { $method_body = <<<'EOS' - public function method() { - return $this->lazyLoadItself()->method(); + public function method() + { + return $this->lazyLoadItself()->method(); } EOS; @@ -85,8 +86,9 @@ public function testBuildMethodWithParameter() { $method_body = <<<'EOS' - public function methodWithParameter($parameter) { - return $this->lazyLoadItself()->methodWithParameter($parameter); + public function methodWithParameter($parameter) + { + return $this->lazyLoadItself()->methodWithParameter($parameter); } EOS; @@ -107,8 +109,9 @@ public function testBuildComplexMethod() { $method_body = <<<'EOS' public function complexMethod($parameter, callable $function, \Drupal\Tests\Component\ProxyBuilder\TestServiceNoMethod $test_service = NULL, array &$elements = array ( - )) { - return $this->lazyLoadItself()->complexMethod($parameter, $function, $test_service, $elements); + )) + { + return $this->lazyLoadItself()->complexMethod($parameter, $function, $test_service, $elements); } EOS; @@ -128,13 +131,14 @@ public function testBuildWithInterface() { $method_body = <<<'EOS' - public function testMethod($parameter) { - return $this->lazyLoadItself()->testMethod($parameter); + public function testMethod($parameter) + { + return $this->lazyLoadItself()->testMethod($parameter); } EOS; - $interface_string = ' implements \Drupal\Tests\Component\ProxyBuilder\TestInterface'; + $interface_string = ' implements \Drupal\Tests\Component\ProxyBuilder\TestInterface'; $this->assertEquals($this->buildExpectedClass($class, $method_body, $interface_string), $result); } @@ -150,8 +154,9 @@ public function testBuildWithProtectedAndPrivateMethod() { $method_body = <<<'EOS' - public function testMethod($parameter) { - return $this->lazyLoadItself()->testMethod($parameter); + public function testMethod($parameter) + { + return $this->lazyLoadItself()->testMethod($parameter); } EOS; @@ -172,8 +177,9 @@ public function testBuildWithPublicStaticMethod() { // Ensure that the static method is not wrapped. $method_body = <<<'EOS' - public static function testMethod($parameter) { - \Drupal\Tests\Component\ProxyBuilder\TestServiceWithPublicStaticMethod::testMethod($parameter); + public static function testMethod($parameter) + { + \Drupal\Tests\Component\ProxyBuilder\TestServiceWithPublicStaticMethod::testMethod($parameter); } EOS; @@ -190,10 +196,16 @@ public static function testMethod($parameter) { * @return string * The code of the entire proxy. */ - protected function buildExpectedClass($class, $expected_methods_body, $interface_string = ' ') { + protected function buildExpectedClass($class, $expected_methods_body, $interface_string = '') { $proxy_class = $this->proxyBuilder->buildProxyClassName($class); $expected_string = <<<'EOS' -class {{ proxy_class }} {{ interface_string }} { +/** + * Provides a proxy class for \{{ class }}. + * + * @see \Drupal\Component\ProxyBuilder + */ +class {{ proxy_class }}{{ interface_string }} +{ /** * @var string @@ -205,17 +217,20 @@ class {{ proxy_class }} {{ interface_string }} { */ protected $service; - public function __construct($service_id) { - $this->serviceId = $service_id; + public function __construct(ContainerInterface $container, $serviceId) + { + $this->container = $container; + $this->serviceId = $serviceId; } - protected function lazyLoadItself() { - if (!isset($this->service)) { - $method_name = 'get' . Container::camelize($this->serviceId) . 'Service'; - $this->service = \Drupal::getContainer()->$method_name(FALSE); - } + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } - return $this->service; + return $this->service; } {{ expected_methods_body }} } diff --git a/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyDumperTest.php b/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyDumperTest.php index 53e8777..d8fed50 100644 --- a/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyDumperTest.php +++ b/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyDumperTest.php @@ -65,7 +65,6 @@ public function providerTestIsProxyCandidate() { $definition = new Definition('Drupal\Tests\Component\ProxyBuilder\TestService'); $definition->setLazy(TRUE); $data[] = [$definition, TRUE]; - // Existing and lazy service. return $data; } @@ -78,7 +77,7 @@ public function testGetProxyFactoryCode() { $expected = <<<'EOS' if ($lazyLoad) { - return $this->services['test_service'] = new Drupal_Tests_Component_ProxyBuilder_TestService_Proxy('test_service'); + return $this->services['test_service'] = new Drupal_Tests_Component_ProxyBuilder_TestService_Proxy($this, 'test_service'); } EOS;