From 0754617c52f4bab9c2c53d717592145388ba7832 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 27 Jul 2017 14:55:51 +0200 Subject: [PATCH] [DI][ProxyManager] Pass the factory code to execute to DumperInterface::getProxyFactoryCode() --- .../LazyProxy/PhpDumper/ProxyDumper.php | 15 ++++++++------- .../Tests/LazyProxy/PhpDumper/ProxyDumperTest.php | 2 +- .../DependencyInjection/Dumper/PhpDumper.php | 2 +- .../LazyProxy/PhpDumper/DumperInterface.php | 6 +++--- .../LazyProxy/PhpDumper/NullDumper.php | 2 +- .../Tests/Fixtures/includes/classes.php | 2 +- .../Tests/LazyProxy/PhpDumper/NullDumperTest.php | 2 +- .../Component/DependencyInjection/composer.json | 1 + 8 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php b/src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php index b91bfeb922cf8..a8cdc7de3bbc3 100644 --- a/src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php +++ b/src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php @@ -65,7 +65,7 @@ public function isProxyCandidate(Definition $definition) /** * {@inheritdoc} */ - public function getProxyFactoryCode(Definition $definition, $id, $methodName = null) + public function getProxyFactoryCode(Definition $definition, $id, $factoryCode = null) { $instantiation = 'return'; @@ -73,11 +73,12 @@ public function getProxyFactoryCode(Definition $definition, $id, $methodName = n $instantiation .= " \$this->services['$id'] ="; } - if (func_num_args() >= 3) { - $methodName = func_get_arg(2); - } else { - @trigger_error(sprintf('You must use the third argument of %s to define the method to call to construct your service since version 3.1, not using it won\'t be supported in 4.0.', __METHOD__), E_USER_DEPRECATED); - $methodName = 'get'.Container::camelize($id).'Service'; + if (null === $factoryCode) { + @trigger_error(sprintf('The "%s()" method expects a third argument defining the code to execute to construct your service since version 3.4, providing it will be required in 4.0.', __METHOD__), E_USER_DEPRECATED); + $factoryCode = '$this->get'.Container::camelize($id).'Service(false)'; + } elseif (false === strpos($factoryCode, '(')) { + @trigger_error(sprintf('The "%s()" method expects its third argument to define the code to execute to construct your service since version 3.4, providing it will be required in 4.0.', __METHOD__), E_USER_DEPRECATED); + $factoryCode = "\$this->$factoryCode(false)"; } $proxyClass = $this->getProxyClassName($definition); @@ -92,7 +93,7 @@ public function getProxyFactoryCode(Definition $definition, $id, $methodName = n $instantiation $constructorCall( function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) { - \$wrappedInstance = \$this->$methodName(false); + \$wrappedInstance = $factoryCode; \$proxy->setProxyInitializer(null); diff --git a/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/ProxyDumperTest.php b/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/ProxyDumperTest.php index 64082c8f7f3f1..58de413674fa5 100644 --- a/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/ProxyDumperTest.php +++ b/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/ProxyDumperTest.php @@ -67,7 +67,7 @@ public function testGetProxyFactoryCodeWithCustomMethod() $definition->setLazy(true); - $code = $this->dumper->getProxyFactoryCode($definition, 'foo', 'getFoo2Service'); + $code = $this->dumper->getProxyFactoryCode($definition, 'foo', '$this->getFoo2Service(false)'); $this->assertStringMatchesFormat( '%wif ($lazyLoad) {%wreturn $this->services[\'foo\'] =%s' diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 86222ac551fa0..9021f70530a0e 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -639,7 +639,7 @@ private function addService($id, Definition $definition) EOF; - $code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id, $methodName) : ''; + $code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id, "\$this->$methodName(false)") : ''; if ($definition->isDeprecated()) { $code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id))); diff --git a/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/DumperInterface.php b/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/DumperInterface.php index ce88eba9742fd..0e06ce09b551b 100644 --- a/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/DumperInterface.php +++ b/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/DumperInterface.php @@ -33,12 +33,12 @@ public function isProxyCandidate(Definition $definition); * Generates the code to be used to instantiate a proxy in the dumped factory code. * * @param Definition $definition - * @param string $id service identifier - * @param string $methodName the method name to get the service, will be added to the interface in 4.0 + * @param string $id service identifier + * @param string $factoryCode the code to execute to create the service, will be added to the interface in 4.0 * * @return string */ - public function getProxyFactoryCode(Definition $definition, $id/**, $methodName = null */); + public function getProxyFactoryCode(Definition $definition, $id/**, $factoryCode = null */); /** * Generates the code for the lazy proxy. diff --git a/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/NullDumper.php b/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/NullDumper.php index 30cbdef0a6ad8..67f9fae94dbf8 100644 --- a/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/NullDumper.php +++ b/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/NullDumper.php @@ -33,7 +33,7 @@ public function isProxyCandidate(Definition $definition) /** * {@inheritdoc} */ - public function getProxyFactoryCode(Definition $definition, $id, $methodName = null) + public function getProxyFactoryCode(Definition $definition, $id, $factoryCode = null) { return ''; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php index 717dcdc52e579..cbb6a6e507faf 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php @@ -88,7 +88,7 @@ public function isProxyCandidate(Definition $definition) return false; } - public function getProxyFactoryCode(Definition $definition, $id, $methodName = null) + public function getProxyFactoryCode(Definition $definition, $id, $factoryCall = null) { return ''; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/NullDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/NullDumperTest.php index cde2c147e752c..b1b9b399c3728 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/NullDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/NullDumperTest.php @@ -28,7 +28,7 @@ public function testNullDumper() $definition = new Definition('stdClass'); $this->assertFalse($dumper->isProxyCandidate($definition)); - $this->assertSame('', $dumper->getProxyFactoryCode($definition, 'foo')); + $this->assertSame('', $dumper->getProxyFactoryCode($definition, 'foo', '(false)')); $this->assertSame('', $dumper->getProxyCode($definition)); } } diff --git a/src/Symfony/Component/DependencyInjection/composer.json b/src/Symfony/Component/DependencyInjection/composer.json index f5124f9c8521e..ba391a2bd9a98 100644 --- a/src/Symfony/Component/DependencyInjection/composer.json +++ b/src/Symfony/Component/DependencyInjection/composer.json @@ -34,6 +34,7 @@ "conflict": { "symfony/config": "<3.3.1", "symfony/finder": "<3.3", + "symfony/proxy-manager-bridge": "<3.4", "symfony/yaml": "<3.3" }, "provide": {