Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4279f53

Browse files
bug #27366 [DI] never inline lazy services (nicolas-grekas)
This PR was merged into the 2.8 branch. Discussion ---------- [DI] never inline lazy services | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | no | Fixed tickets | - | License | MIT | Doc PR | - Should apply also: - to deprecated services since 2.8 - to errored services since 3.4 Commits ------- 3b4d7ab [DI] never inline lazy services
2 parents 9c089b3 + 3b4d7ab commit 4279f53

File tree

4 files changed

+96
-6
lines changed

4 files changed

+96
-6
lines changed

src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,15 @@ private function inlineArguments(ContainerBuilder $container, array $arguments,
106106
*/
107107
private function isInlineableDefinition(ContainerBuilder $container, $id, Definition $definition)
108108
{
109+
if ($definition->isDeprecated() || $definition->isLazy() || $definition->isSynthetic()) {
110+
return false;
111+
}
112+
109113
if (!$definition->isShared() || ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
110114
return true;
111115
}
112116

113-
if ($definition->isDeprecated() || $definition->isPublic() || $definition->isLazy()) {
117+
if ($definition->isPublic()) {
114118
return false;
115119
}
116120

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Tests\Dumper;
1313

14-
use DummyProxyDumper;
1514
use PHPUnit\Framework\TestCase;
1615
use Symfony\Component\DependencyInjection\ContainerBuilder;
1716
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
@@ -278,6 +277,19 @@ public function testInlinedDefinitionReferencingServiceContainer()
278277
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services13.php', $dumper->dump(), '->dump() dumps inline definitions which reference service_container');
279278
}
280279

280+
public function testNonSharedLazyDefinitionReferences()
281+
{
282+
$container = new ContainerBuilder();
283+
$container->register('foo', 'stdClass')->setShared(false)->setLazy(true);
284+
$container->register('bar', 'stdClass')->addArgument(new Reference('foo', ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, false));
285+
$container->compile();
286+
287+
$dumper = new PhpDumper($container);
288+
$dumper->setProxyDumper(new \DummyProxyDumper());
289+
290+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_non_shared_lazy.php', $dumper->dump());
291+
}
292+
281293
public function testInitializePropertiesBeforeMethodCalls()
282294
{
283295
require_once self::$fixturesPath.'/includes/classes.php';
@@ -343,7 +355,7 @@ public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServic
343355

344356
$dumper = new PhpDumper($container);
345357

346-
$dumper->setProxyDumper(new DummyProxyDumper());
358+
$dumper->setProxyDumper(new \DummyProxyDumper());
347359
$dumper->dump();
348360

349361
$this->addToAssertionCount(1);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ class DummyProxyDumper implements ProxyDumper
8484
{
8585
public function isProxyCandidate(Definition $definition)
8686
{
87-
return false;
87+
return $definition->isLazy();
8888
}
8989

9090
public function getProxyFactoryCode(Definition $definition, $id)
9191
{
92-
return '';
92+
return " // lazy factory\n\n";
9393
}
9494

9595
public function getProxyCode(Definition $definition)
9696
{
97-
return '';
97+
return "// proxy code\n";
9898
}
9999
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerInterface;
4+
use Symfony\Component\DependencyInjection\Container;
5+
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
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+
class ProjectServiceContainer extends Container
16+
{
17+
private $parameters;
18+
private $targetDirs = array();
19+
20+
public function __construct()
21+
{
22+
$this->services =
23+
$this->scopedServices =
24+
$this->scopeStacks = array();
25+
$this->scopes = array();
26+
$this->scopeChildren = array();
27+
$this->methodMap = array(
28+
'bar' => 'getBarService',
29+
'foo' => 'getFooService',
30+
);
31+
32+
$this->aliases = array();
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function compile()
39+
{
40+
throw new LogicException('You cannot compile a dumped frozen container.');
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function isFrozen()
47+
{
48+
return true;
49+
}
50+
51+
/**
52+
* Gets the public 'bar' shared service.
53+
*
54+
* @return \stdClass
55+
*/
56+
protected function getBarService()
57+
{
58+
return $this->services['bar'] = new \stdClass($this->get('foo'));
59+
}
60+
61+
/**
62+
* Gets the public 'foo' service.
63+
*
64+
* @return \stdClass
65+
*/
66+
public function getFooService($lazyLoad = true)
67+
{
68+
// lazy factory
69+
70+
return new \stdClass();
71+
}
72+
}
73+
74+
// proxy code

0 commit comments

Comments
 (0)