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

Skip to content

Commit bfa3c85

Browse files
[DI] never inline lazy services
1 parent 84bba75 commit bfa3c85

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ private function inlineArguments(ContainerBuilder $container, array $arguments)
116116
*/
117117
private function isInlineableDefinition(ContainerBuilder $container, $id, Definition $definition)
118118
{
119+
if ($definition->isLazy()) {
120+
return false;
121+
}
122+
119123
if (ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope()) {
120124
return true;
121125
}

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;
@@ -270,6 +269,19 @@ public function testInlinedDefinitionReferencingServiceContainer()
270269
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services13.php', $dumper->dump(), '->dump() dumps inline definitions which reference service_container');
271270
}
272271

272+
public function testNonSharedLazyDefinitionReferences()
273+
{
274+
$container = new ContainerBuilder();
275+
$container->register('foo', 'stdClass')->setScope(ContainerBuilder::SCOPE_PROTOTYPE)->setLazy(true);
276+
$container->register('bar', 'stdClass')->addArgument(new Reference('foo', ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, false));
277+
$container->compile();
278+
279+
$dumper = new PhpDumper($container);
280+
$dumper->setProxyDumper(new \DummyProxyDumper());
281+
282+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_non_shared_lazy.php', $dumper->dump());
283+
}
284+
273285
public function testInitializePropertiesBeforeMethodCalls()
274286
{
275287
require_once self::$fixturesPath.'/includes/classes.php';
@@ -335,7 +347,7 @@ public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServic
335347

336348
$dumper = new PhpDumper($container);
337349

338-
$dumper->setProxyDumper(new DummyProxyDumper());
350+
$dumper->setProxyDumper(new \DummyProxyDumper());
339351
$dumper->dump();
340352

341353
$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)