8000 bug #27568 [DI] Deduplicate generated proxy classes (nicolas-grekas) · palex-fpt/symfony@f36930f · GitHub
[go: up one dir, main page]

Skip to content

Commit f36930f

Browse files
committed
bug symfony#27568 [DI] Deduplicate generated proxy classes (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [DI] Deduplicate generated proxy classes | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#27537 | License | MIT | Doc PR | - Commits ------- 322f58b [DI] Deduplicate generated proxy classes
2 parents ac70edf + 322f58b commit f36930f

File tree

5 files changed

+111
-5
lines changed

5 files changed

+111
-5
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ private function collectLineage($class, array &$lineage)
396396

397397
private function generateProxyClasses()
398398
{
399+
$alreadyGenerated = array();
399400
$definitions = $this->container->getDefinitions();
400401
$strip = '' === $this->docStar && method_exists('Symfony\Component\HttpKernel\Kernel', 'stripComments');
401402
$proxyDumper = $this->getProxyDumper();
@@ -404,8 +405,12 @@ private function generateProxyClasses()
404405
if (!$proxyDumper->isProxyCandidate($definition)) {
405406
continue;
406407
}
408+
if (isset($alreadyGenerated[$class = $definition->getClass()])) {
409+
continue;
410+
}
411+
$alreadyGenerated[$class] = true;
407412
// register class' reflector for resource tracking
408-
$this->container->getReflectionClass($definition->getClass());
413+
$this->container->getReflectionClass($class);
409414
$proxyCode = "\n".$proxyDumper->getProxyCode($definition);
410415
if ($strip) {
411416
$proxyCode = "<?php\n".$proxyCode;

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,19 @@ public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServic
576576
$this->addToAssertionCount(1);
577577
}
578578

579+
public function testDedupLazyProxy()
580+
{
581+
$container = new ContainerBuilder();
582+
$container->register('foo', 'stdClass')->setLazy(true)->setPublic(true);
583+
$container->register('bar', 'stdClass')->setLazy(true)->setPublic(true);
584+
$container->compile();
585+
586+
$dumper = new PhpDumper($container);
587+
$dumper->setProxyDumper(new \DummyProxyDumper());
588+
589+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_dedup_lazy_proxy.php', $dumper->dump());
590+
}
591+
579592
public function testLazyArgumentProvideGenerator()
580593
{
581594
require_once self::$fixturesPath.'/includes/classes.php';

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ public function isProxyCandidate(Definition $definition)
9090

9191
public function getProxyFactoryCode(Definition $definition, $id, $factoryCall = null)
9292
{
93-
return " // lazy factory\n\n";
93+
return " // lazy factory for {$definition->getClass()}\n\n";
9494
}
9595

9696
public function getProxyCode(Definition $definition)
9797
{
98-
return "// proxy code\n";
98+
return "// proxy code for {$definition->getClass()}\n";
9999
}
100100
}
101101

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
4+
use Symfony\Component\DependencyInjection\ContainerInterface;
5+
use Symfony\Component\DependencyInjection\Container;
6+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7+
use Symfony\Component\DependencyInjection\Exception\LogicException;
8+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
10+
11+
/**
12+
* This class has been auto-generated
13+
* by the Symfony Dependency Injection Component.
14+
*
15+
* @final since Symfony 3.3
67E6 16+
*/
17+
class ProjectServiceContainer extends Container
18+
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
22+
public function __construct()
23+
{
24+
$this->services = array();
25+
$this->methodMap = array(
26+
'bar' => 'getBarService',
27+
'foo' => 'getFooService',
28+
);
29+
30+
$this->aliases = array();
31+
}
32+
33+
public function getRemovedIds()
34+
{
35+
return array(
36+
'Psr\\Container\\ContainerInterface' => true,
37+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
38+
);
39+
}
40+
41+
public function compile()
42+
{
43+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
44+
}
45+
46+
public function isCompiled()
47+
{
48+
return true;
49+
}
50+
51+
public function isFrozen()
52+
{
53+
@trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
54+
55+
return true;
56+
}
57+
58+
protected function createProxy($class, \Closure $factory)
59+
{
60+
return $factory();
61+
}
62+
63+
/**
64+
* Gets the public 'bar' shared service.
65+
*
66+
* @return \stdClass
67+
*/
68+
protected function getBarService($lazyLoad = true)
69+
{
70+
// lazy factory for stdClass
71+
72+
return new \stdClass();
73+
}
74+
75+
/**
76+
* Gets the public 'foo' shared service.
77+
*
78+
* @return \stdClass
79+
*/
80+
protected function getFooService($lazyLoad = true)
81+
{
82+
// lazy factory for stdClass
83+
84+
return new \stdClass();
85+
}
86+
}
87+
88+
// proxy code for stdClass

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ protected function getBarService()
8383
*/
8484
protected function getFooService($lazyLoad = true)
8585
{
86-
// lazy factory
86+
// lazy factory for stdClass
8787

8888
return new \stdClass();
8989
}
9090
}
9191

92-
// proxy code
92+
// proxy code for stdClass

0 commit comments

Comments
 (0)
0