8000 Fix the lazy PhpDumper · symfony/symfony@795399a · GitHub
[go: up one dir, main page]

Skip to content

Commit 795399a

Browse files
committed
Fix the lazy PhpDumper
1 parent 5efed7b commit 795399a

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ public function getProxyFactoryCode(Definition $definition, $id)
7171
$instantiation .= " \$this->services['$id'] =";
7272
}
7373

74-
$methodName = 'get'.Container::camelize($id).'Service';
74+
if (func_num_args() >= 3) {
75+
$methodName = func_get_arg(2);
76+
} else {
77+
@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);
78+
$methodName = 'get'.Container::camelize($id).'Service';
79+
}
7580
$proxyClass = $this->getProxyClassName($definition);
7681

7782
$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.

0 commit comments

Comments
 (0)
0