[DI][ProxyManager] Pass the factory code to execute to DumperInterface::getProxyFactoryCode()

This commit is contained in:
Nicolas Grekas 2017-07-27 14:55:51 +02:00
parent 6bbb391175
commit 0754617c52
8 changed files with 17 additions and 15 deletions

View File

@ -65,7 +65,7 @@ class ProxyDumper implements DumperInterface
/**
* {@inheritdoc}
*/
public function getProxyFactoryCode(Definition $definition, $id, $methodName = null)
public function getProxyFactoryCode(Definition $definition, $id, $factoryCode = null)
{
$instantiation = 'return';
@ -73,11 +73,12 @@ class ProxyDumper implements DumperInterface
$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 @@ class ProxyDumper implements DumperInterface
$instantiation $constructorCall(
function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) {
\$wrappedInstance = \$this->$methodName(false);
\$wrappedInstance = $factoryCode;
\$proxy->setProxyInitializer(null);

View File

@ -67,7 +67,7 @@ class ProxyDumperTest extends TestCase
$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'

View File

@ -639,7 +639,7 @@ class PhpDumper extends Dumper
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)));

View File

@ -33,12 +33,12 @@ interface DumperInterface
* 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.

View File

@ -33,7 +33,7 @@ class NullDumper implements DumperInterface
/**
* {@inheritdoc}
*/
public function getProxyFactoryCode(Definition $definition, $id, $methodName = null)
public function getProxyFactoryCode(Definition $definition, $id, $factoryCode = null)
{
return '';
}

View File

@ -88,7 +88,7 @@ class DummyProxyDumper implements ProxyDumper
return false;
}
public function getProxyFactoryCode(Definition $definition, $id, $methodName = null)
public function getProxyFactoryCode(Definition $definition, $id, $factoryCall = null)
{
return '';
}

View File

@ -28,7 +28,7 @@ class NullDumperTest extends TestCase
$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));
}
}

View File

@ -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": {