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

Skip to content

Commit 117317c

Browse files
Merge branch '4.4' into 5.1
* 4.4: [DI] fix inlining of non-shared services
2 parents b7cd4c7 + 804f194 commit 117317c

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class InlineServiceDefinitionsPass extends AbstractRecursivePass
2929
private $connectedIds = [];
3030
private $notInlinedIds = [];
3131
private $inlinedIds = [];
32+
private $notInlinableIds = [];
3233
private $graph;
3334

3435
public function __construct(AnalyzeServiceReferencesPass $analyzingPass = null)
@@ -85,6 +86,10 @@ public function process(ContainerBuilder $container)
8586
} while ($this->inlinedIds && $this->analyzingPass);
8687

8788
foreach ($remainingInlinedIds as $id) {
89+
if (isset($this->notInlinableIds[$id])) {
90+
continue;
91+
}
92+
8893
$definition = $container->getDefinition($id);
8994

9095
if (!$definition->isShared() && !$definition->isPublic()) {
@@ -94,6 +99,7 @@ public function process(ContainerBuilder $container)
9499
} finally {
95100
$this->container = null;
96101
$this->connectedIds = $this->notInlinedIds = $this->inlinedIds = [];
102+
$this->notInlinableIds = [];
97103
$this->graph = null;
98104
}
99105
}
@@ -124,6 +130,8 @@ protected function processValue($value, bool $isRoot = false)
124130
$definition = $this->container->getDefinition($id);
125131

126132
if (!$this->isInlineableDefinition($id, $definition)) {
133+
$this->notInlinableIds[$id] = true;
134+
127135
return $value;
128136
}
129137

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,10 @@ private function addInlineReference(string $id, Definition $definition, string $
930930
return '';
931931
}
932932

933+
if ($this->container->hasDefinition($targetId) && ($def = $this->container->getDefinition($targetId)) && !$def->isShared()) {
934+
return '';
935+
}
936+
933937
$hasSelfRef = isset($this->circularReferences[$id][$targetId]) && !isset($this->definitionVariables[$definition]);
934938

935939
if ($hasSelfRef && !$forConstructor && !$forConstructor = !$this->circularReferences[$id][$targetId]) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
2222
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
2323
use Symfony\Component\DependencyInjection\Argument\ServiceLocator as ArgumentServiceLocator;
24+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
2425
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2526
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
2627
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -720,6 +721,24 @@ public function testNonSharedLazyDefinitionReferences()
720721
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_non_shared_lazy.php', $dumper->dump());
721722
}
722723

724+
public function testNonSharedDuplicates()
725+
{
726+
$container = new ContainerBuilder();
727+
$container->register('foo', 'stdClass')->setShared(false);
728+
$container->register('baz', 'stdClass')->setPublic(true)
729+
->addArgument(new ServiceLocatorArgument(['foo' => new Reference('foo')]));
730+
$container->register('bar', 'stdClass')->setPublic(true)
731+
->addArgument(new Reference('foo'))
732+
->addArgument(new Reference('foo'))
733+
;
734+
$container->compile();
735+
736+
$dumper = new PhpDumper($container);
737+
$dumper->setProxyDumper(new \DummyProxyDumper());
738+
739+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_non_shared_duplicates.php', $dumper->dump());
740+
}
741+
723742
public function testInitializePropertiesBeforeMethodCalls()
724743
{
725744
require_once self::$fixturesPath.'/includes/classes.php';
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11+
12+
/**
13+
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
14+
*/
15+
class ProjectServiceContainer extends Container
16+
{
17+
protected $parameters = [];
18+
protected $getService;
19+
20+
public function __construct()
21+
{
22+
$this->getService = \Closure::fromCallable([$this, 'getService']);
23+
$this->services = $this->privates = [];
24+
$this->methodMap = [
25+
'bar' => 'getBarService',
26+
'baz' => 'getBazService',
27+
];
28+
29+
$this->aliases = [];
30+
}
31+
32+
public function compile(): void
33+
{
34+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
35+
}
36+
37+
public function isCompiled(): bool
38+
{
39+
return true;
40+
}
41+
42+
public function getRemovedIds(): array
43+
{
44+
return [
45+
'.service_locator.v6P4ovL' => true,
46+
'Psr\\Container\\ContainerInterface' => true,
47+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
48+
'foo' => true,
49+
];
50+
}
51+
52+
/**
53+
* Gets the public 'bar' shared service.
54+
*
55+
* @return \stdClass
56+
*/
57+
protected function getBarService()
58+
{
59+
return $this->services['bar'] = new \stdClass((new \stdClass()), (new \stdClass()));
60+
}
61+
62+
/**
63+
* Gets the public 'baz' shared service.
64+
*
65+
* @return \stdClass
66+
*/
67+
protected function getBazService()
68+
{
69+
return $this->services['baz'] = new \stdClass(new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($this->getService, [
70+
'foo' => [false, 'foo', 'getFooService', false],
71+
], [
72+
'foo' => '?',
73+
]));
74+
}
75+
76+
/**
77+
* Gets the private 'foo' service.
78+
*
79+
* @return \stdClass
80+
*/
81+
protected function getFooService()
82+
{
83+
$this->factories['service_container']['foo'] = function () {
84+
return new \stdClass();
85+
};
86+
87+
return $this->factories['service_container']['foo']();
88+
}
89+
}

0 commit comments

Comments
 (0)