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

Skip to content

Commit 88aea90

Browse files
[DI] Generate one factory class per private service
1 parent 0caed93 commit 88aea90

File tree

14 files changed

+679
-475
lines changed

14 files changed

+679
-475
lines changed

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

Lines changed: 10 additions & 5 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, $factoryCode = 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(' $this->%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 === $factoryCode) {
82+
throw new \InvalidArgumentException(sprintf('Missing method call to construct the service "%s".', $id));
7883
}
7984

8085
$proxyClass = $this->getProxyClassName($definition);
@@ -90,7 +95,7 @@ public function getProxyFactoryCode(Definition $definition, $id, $methodName = n
9095
9196
$instantiation $constructorCall(
9297
function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) {
93-
\$wrappedInstance = \$this->$methodName(false);
98+
\$wrappedInstance = $factoryCode;
9499
95100
\$proxy->setProxyInitializer(null);
96101

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bridge\ProxyManager\Tests\LazyProxy\Dumper;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\StaticProxyConstructor;
1615
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
1716
use Symfony\Component\DependencyInjection\ContainerBuilder;
1817
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
@@ -27,21 +26,9 @@ class PhpDumperTest extends TestCase
2726
{
2827
public function testDumpContainerWithProxyService()
2928
{
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-
4229
$this->assertStringMatchesFormatFile(
4330
__DIR__.'/../Fixtures/php/lazy_service_structure.txt',
44-
$dumpedString,
31+
$this->dumpLazyServiceProjectServiceContainer(),
4532
'->dump() does generate proxy lazy loading logic.'
4633
);
4734
}
@@ -51,17 +38,15 @@ public function testDumpContainerWithProxyService()
5138
*/
5239
public function testDumpContainerWithProxyServiceWillShareProxies()
5340
{
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';
41+
if (!class_exists('LazyServiceProjectServiceContainer', false)) {
42+
eval('?>'.$this->dumpLazyServiceProjectServiceContainer());
5843
}
5944

6045
$container = new \LazyServiceProjectServiceContainer();
6146

62-
/* @var $proxy \stdClass_c1d194250ee2e2b7d2eab8b8212368a8 */
6347
$proxy = $container->get('foo');
64-
$this->assertInstanceOf('stdClass_c1d194250ee2e2b7d2eab8b8212368a8', $proxy);
48+
$this->assertInstanceOf('stdClass', $proxy);
49+
$this->assertInstanceOf('ProxyManager\Proxy\LazyLoadingInterface', $proxy);
6550
$this->assertSame($proxy, $container->get('foo'));
6651

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

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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
{
88
if ($lazyLoad) {
99

@@ -21,6 +21,6 @@ class ProjectServiceContainer extends Container
2121
return new \stdClass();
2222
}
2323
}
24-
24+
%A
2525
class stdClass_%s extends %SstdClass implements \ProxyManager\%s
2626
{%a}%A

0 commit comments

Comments
 (0)
0