Skip to content

Commit

Permalink
minor #23693 [DI][ProxyManager] Pass the factory code to execute to D…
Browse files Browse the repository at this point in the history
…umperInterface::getProxyFactoryCode() (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Passing the full code to call the factory is more flexible, as spotted while working on #23678.

Commits
-------

0754617 [DI][ProxyManager] Pass the factory code to execute to DumperInterface::getProxyFactoryCode()
  • Loading branch information
fabpot committed Jul 28, 2017
2 parents 8110598 + 0754617 commit f20b959
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 15 deletions.
Expand Up @@ -65,19 +65,20 @@ public function isProxyCandidate(Definition $definition)
/**
* {@inheritdoc}
*/
public function getProxyFactoryCode(Definition $definition, $id, $methodName = null)
public function getProxyFactoryCode(Definition $definition, $id, $factoryCode = null)
{
$instantiation = 'return';

if ($definition->isShared()) {
$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);

Expand All @@ -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);
Expand Down
Expand Up @@ -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'
Expand Down
Expand Up @@ -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)));
Expand Down
Expand Up @@ -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.
Expand Down
Expand Up @@ -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 '';
}
Expand Down
Expand Up @@ -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 '';
}
Expand Down
Expand Up @@ -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));
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/composer.json
Expand Up @@ -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": {
Expand Down

0 comments on commit f20b959

Please sign in to comment.