8000 [DI] Generate one factory class per private service · symfony/symfony@528afec · GitHub
[go: up one dir, main page]

Skip to content

Commit 528afec

Browse files
[DI] Generate one factory class per private service
1 parent 515b014 commit 528afec

29 files changed

+355
-581
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,21 @@ public function isProxyCandidate(Definition $definition)
6565
/**
6666
* {@inheritdoc}
6767
*/
68-
public function getProxyFactoryCode(Definition $definition, $id, $methodName = null)
68+
public function getProxyFactoryCode(Definition $definition, $id, $methodCall = null)
6969
{
7070
$instantiation = 'return';
7171

7272
if ($definition->isShared()) {
73-
$instantiation .= sprintf(' $this->%s[\'%s\'] =', $definition->isPublic() || !method_exists(ContainerBuilder::class, 'addClassResource') ? 'services' : 'privates', $id);
73+
if (!method_exists(ContainerBuilder::class, 'addClassResource')) {
74+
$instantiation .= sprintf(' $container->%s[\'%s\'] =', $definition->isPublic() ? 'services' : 'privates', $id);
75+
} else {
76+
// BC with DI v3.4
77+
$instantiation .= sprintf(' $this->services[\'%s\'] =', $id);
78+
}
7479
}
7580

76-
if (null === $methodName) {
77-
throw new \InvalidArgumentException(sprintf('Missing name of method to call to construct the service "%s".', $id));
81+
if (null === $methodCall) {
82+
throw new \InvalidArgumentException(sprintf('Missing method call to construct the service "%s".', $id));
7883
}
7984

8085
$proxyClass = $this->getProxyClassName($definition);
@@ -89,8 +94,8 @@ public function getProxyFactoryCode(Definition $definition, $id, $methodName = n
8994
if (\$lazyLoad) {
9095
9196
< 57AE /span>$instantiation $constructorCall(
92-
function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) {
93-
\$wrappedInstance = \$this->$methodName(false);
97+
function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) use (\$container) {
98+
\$wrappedInstance = $methodCall;
9499
95100
\$proxy->setProxyInitializer(null);
96101

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Dumper/PhpDumperTest.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,9 @@ class PhpDumperTest extends TestCase
2727
{
2828
public function testDumpContainerWithProxyService()
2929
{
30-
$container = new ContainerBuilder();
31-
32-
$container->register('foo', 'stdClass');
33-
$container->getDefinition('foo')->setLazy(true);
34-
$container->compile();
35-
36-
$dumper = new PhpDumper($container);
37-
38-
$dumper->setProxyDumper(new ProxyDumper());
39-
40-
$dumpedString = $dumper->dump();
41-
4230
$this->assertStringMatchesFormatFile(
4331
__DIR__.'/../Fixtures/php/lazy_service_structure.txt',
44-
$dumpedString,
32+
$this->dumpLazyServiceProjectServiceContainer(),
4533
'->dump() does generate proxy lazy loading logic.'
4634
);
4735
}
@@ -51,17 +39,15 @@ public function testDumpContainerWithProxyService()
5139
*/
5240
public function testDumpContainerWithProxyServiceWillShareProxies()
5341
{
54-
if (class_exists(StaticProxyConstructor::class)) { // detecting ProxyManager v2
55-
require_once __DIR__.'/../Fixtures/php/lazy_service_with_hints.php';
56-
} else {
57-
require_once __DIR__.'/../Fixtures/php/lazy_service.php';
42+
if (!class_exists('LazyServiceProjectServiceContainer', false)) {
43+
eval('?>'.$this->dumpLazyServiceProjectServiceContainer());
5844
}
5945

6046
$container = new \LazyServiceProjectServiceContainer();
6147

62-
/* @var $proxy \stdClass_c1d194250ee2e2b7d2eab8b8212368a8 */
6348
$proxy = $container->get('foo');
64-
$this->assertInstanceOf('stdClass_c1d194250ee2e2b7d2eab8b8212368a8', $proxy);
49+
$this->assertInstanceOf('stdClass', $proxy);
50+
$this->assertInstanceOf('ProxyManager\Proxy\LazyLoadingInterface', $proxy);
6551
$this->assertSame($proxy, $container->get('foo'));
6652

6753
$this->assertFalse($proxy->isProxyInitialized());
@@ -71,4 +57,19 @@ public function testDumpContainerWithProxyServiceWillShareProxies()
7157
$this->assertTrue($proxy->isProxyInitialized());
7258
$this->assertSame($proxy, $container->get('foo'));
7359
}
60+
61+
private function dumpLazyServiceProjectServiceContainer()
62+
{
63+
$container = new ContainerBuilder();
64+
65+
$container->register('foo', 'stdClass');
66+
$container->getDefinition('foo')->setLazy(true);
67+
$container->compile();
68+
69+
$dumper = new PhpDumper($container);
70+
71+
$dumper->setProxyDumper(new ProxyDumper());
72+
73+
return $dumper->dump(array('class' => 'LazyServiceProjectServiceContainer'));
74+
}
7475
}

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/php/lazy_service.php

Lines changed: 0 additions & 182 deletions
This file was deleted.

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/php/lazy_service_structure.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22

33
use %a
4-
class ProjectServiceContainer extends Container
4+
class LazyServiceProjectServiceContainer extends Container
55
{%a
6-
public function getFooService($lazyLoad = true)
6+
protected function getFooService(bool $lazyLoad = true)
77
{
8+
$container = $this;
9+
810
if ($lazyLoad) {
911

10-
return $this->services['foo'] =%sstdClass_%s(
11-
function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) {
12-
$wrappedInstance = $this->getFooService(false);
12+
return $container->services['foo'] =%sstdClass_%s(
13+
function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {
14+
$wrappedInstance = $container->getFooService(false);
1315

1416
$proxy->setProxyInitializer(null);
1517

0 commit comments

Comments
 (0)
0