Skip to content

Commit 82fc826

Browse files
committed
Fix the lazy PhpDumper
1 parent 5efed7b commit 82fc826

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,18 @@ public function isProxyCandidate(Definition $definition)
6363
/**
6464
* {@inheritdoc}
6565
*/
66-
public function getProxyFactoryCode(Definition $definition, $id)
66+
public function getProxyFactoryCode(Definition $definition, $id, $methodName = null)
6767
{
6868
$instantiation = 'return';
6969

7070
if ($definition->isShared()) {
7171
$instantiation .= " \$this->services['$id'] =";
7272
}
7373

74-
$methodName = 'get'.Container::camelize($id).'Service';
74+
if (null === $methodName) {
75+
@trigger_error(sprintf('You should use the third argument of %s to define the method to call to construct your service', __METHOD__), E_USER_DEPRECATED);
76+
$methodName = 'get'.Container::camelize($id).'Service';
77+
}
7578
$proxyClass = $this->getProxyClassName($definition);
7679

7780
$generatedClass = $this->generateProxyClass($definition);

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/ProxyDumperTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ public function testGetProxyCode()
6060
);
6161
}
6262

63+
public function testGetProxyFactoryCodeWithCustomMethod()
64+
{
65+
$definition = new Definition(__CLASS__);
66+
67+
$definition->setLazy(true);
68+
69+
$code = $this->dumper->getProxyFactoryCode($definition, 'foo', 'getFoo2Service');
70+
71+
$this->assertStringMatchesFormat(
72+
'%wif ($lazyLoad) {%wreturn $this->services[\'foo\'] =%s'
73+
.'SymfonyBridgeProxyManagerTestsLazyProxyPhpDumperProxyDumperTest_%s(%wfunction '
74+
.'(&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) {'
75+
.'%w$wrappedInstance = $this->getFoo2Service(false);%w$proxy->setProxyInitializer(null);'
76+
.'%wreturn true;%w}%w);%w}%w',
77+
$code
78+
);
79+
}
80+
81+
/**
82+
* @group legacy
83+
*/
6384
public function testGetProxyFactoryCode()
6485
{
6586
$definition = new Definition(__CLASS__);

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,19 +630,20 @@ private function addService($id, $definition)
630630
// with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
631631
$isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
632632
$visibility = $isProxyCandidate ? 'public' : 'protected';
633+
$methodName = $this->generateMethodName($id);
633634
$code = <<<EOF
634635
635636
/*{$this->docStar}
636637
* Gets the '$id' service.$doc
637638
*$lazyInitializationDoc
638639
* $return
639640
*/
640-
{$visibility} function {$this->generateMethodName($id)}($lazyInitialization)
641+
{$visibility} function {$methodName}($lazyInitialization)
641642
{
642643
643644
EOF;
644645

645-
$code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id) : '';
646+
$code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id, $methodName) : '';
646647

647648
if ($definition->isSynthetic()) {
648649
$code .= sprintf(" throw new RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n }\n", $id);

src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/DumperInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public function isProxyCandidate(Definition $definition);
3434
*
3535
* @param Definition $definition
3636
* @param string $id service identifier
37+
* @param string $methodName the method name to get the service, will be added to the interface in 4.0.
3738
*
3839
* @return string
3940
*/
40-
public function getProxyFactoryCode(Definition $definition, $id);
41+
public function getProxyFactoryCode(Definition $definition, $id/**, $methodName = null */);
4142

4243
/**
4344
* Generates the code for the lazy proxy.

src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/NullDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function isProxyCandidate(Definition $definition)
3131
/**
3232
* {@inheritdoc}
3333
*/
34-
public function getProxyFactoryCode(Definition $definition, $id)
34+
public function getProxyFactoryCode(Definition $definition, $id, $methodName = null)
3535
{
3636
return '';
3737
}

0 commit comments

Comments
 (0)