Skip to content

Commit 8e1f381

Browse files
bug symfony#28678 [DI] fix dumping setters before their inlined instances (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [DI] fix dumping setters before their inlined instances | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#28654 | License | MIT | Doc PR | - Commits ------- d60eb1a [DI] fix dumping setters before their inlined instances
2 parents 22aec24 + d60eb1a commit 8e1f381

File tree

5 files changed

+170
-1
lines changed

5 files changed

+170
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,9 @@ private function addInlineService(&$head, &$tail, $id, Definition $definition, $
780780

781781
$code = '';
782782
$arguments = array($definition->getProperties(), $definition->getMethodCalls(), $definition->getConfigurator());
783-
$hasSelfRef = $this->addInlineVariables($code, $tail, $id, $arguments, false) || $hasSelfRef;
783+
$hasSelfRef = $this->addInlineVariables($code, $code, $id, $arguments, false) || $hasSelfRef;
784784

785+
$code .= '' !== $code ? "\n" : '';
785786
$code .= $this->addServiceProperties($definition, $name);
786787
$code .= $this->addServiceMethodCalls($definition, $name);
787788
$code .= $this->addServiceConfigurator($definition, $name);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ protected function getFooService()
8585
$b = new \stdClass();
8686
$c = new \stdClass();
8787
$c->p3 = new \stdClass();
88+
8889
$b->p2 = $c;
8990

9091
return $this->services['foo'] = new \Symfony\Component\DependencyInjection\Tests\Dumper\FooForDeepGraph($a, $b);
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+
11+
/**
12+
* This class has been auto-generated
13+
* by the Symfony Dependency Injection Component.
14+
*
15+
* @final since Symfony 3.3
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->normalizedIds = array(
26+
'tsantos\\serializer\\serializerinterface' => 'TSantos\\Serializer\\SerializerInterface',
27+
);
28+
$this->methodMap = array(
29+
'tsantos_serializer' => 'getTsantosSerializerService',
30+
);
31+
$this->aliases = array(
32+
'TSantos\\Serializer\\SerializerInterface' => 'tsantos_serializer',
33+
);
34+
}
35+
36+
public function getRemovedIds()
37+
{
38+
return array(
39+
'Psr\\Container\\ContainerInterface' => true,
40+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
41+
);
42+
}
43+
44+
public function compile()
45+
{
46+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
47+
}
48+
49+
public function isCompiled()
50+
{
51+
return true;
52+
}
53+
54+
public function isFrozen()
55+
{
56+
@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);
57+
58+
return true;
59+
}
60+
61+
/**
62+
* Gets the public 'tsantos_serializer' shared service.
63+
*
64+
* @return \TSantos\Serializer\EventEmitterSerializer
65+
*/
66+
protected function getTsantosSerializerService()
67+
{
68+
$a = new \TSantos\Serializer\NormalizerRegistry();
69+
70+
$d = new \TSantos\Serializer\EventDispatcher\EventDispatcher();
71+
$d->addSubscriber(new \TSantos\SerializerBundle\EventListener\StopwatchListener(new \Symfony\Component\Stopwatch\Stopwatch(true)));
72+
73+
$this->services['tsantos_serializer'] = $instance = new \TSantos\Serializer\EventEmitterSerializer(new \TSantos\Serializer\Encoder\JsonEncoder(), $a, $d);
74+
75+
$b = new \TSantos\Serializer\Normalizer\CollectionNormalizer();
76+
77+
$b->setSerializer($instance);
78+
79+
$c = new \TSantos\Serializer\Normalizer\JsonNormalizer();
80+
81+
$c->setSerializer($instance);
82+
83+
$a->add(new \TSantos\Serializer\Normalizer\ObjectNormalizer(new \TSantos\SerializerBundle\Serializer\CircularReferenceHandler()));
84+
$a->add($b);
85+
$a->add($c);
86+
87+
return $instance;
88+
}
89+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/>
5+
<service id="tsantos_serializer" class="TSantos\Serializer\EventEmitterSerializer" public="true">
6+
<argument type="service">
7+
<service class="TSantos\Serializer\Encoder\JsonEncoder" public="false">
8+
<tag name="tsantos_serializer.encoder" format="json"/>
9+
</service>
10+
</argument>
11+
<argument type="service">
12+
<service class="TSantos\Serializer\NormalizerRegistry" public="false">
13+
<call method="add">
14+
<argument type="service">
15+
<service class="TSantos\Serializer\Normalizer\ObjectNormalizer" public="false">
16+
<tag name="tsantos_serializer.normalizer" priority="-800"/>
17+
<argument type="service">
18+
<service class="TSantos\SerializerBundle\Serializer\CircularReferenceHandler" public="false"/>
19+
</argument>
20+
</service>
21+
</argument>
22+
</call>
23+
<call method="add">
24+
<argument type="service">
25+
<service class="TSantos\Serializer\Normalizer\CollectionNormalizer" public="false">
26+
<tag name="tsantos_serializer.normalizer" priority="-900"/>
27+
<call method="setSerializer">
28+
<argument type="service" id="tsantos_serializer"/>
29+
</call>
30+
</service>
31+
</argument>
32+
</call>
33+
<call method="add">
34+
<argument type="service">
35+
<service class="TSantos\Serializer\Normalizer\JsonNormalizer" public="false">
36+
<tag name="tsantos_serializer.normalizer" priority="-1000"/>
37+
<call method="setSerializer">
38+
<argument type="service" id="tsantos_serializer"/>
39+
</call>
40+
</service>
41+
</argument>
42+
</call>
43+
</service>
44+
</argument>
45+
<argument type="service">
46+
<service class="TSantos\Serializer\EventDispatcher\EventDispatcher" public="false">
47+
<call method="addSubscriber">
48+
<argument type="service">
49+
<service class="TSantos\SerializerBundle\EventListener\StopwatchListener" public="false">
50+
<tag name="tsantos_serializer.event_subscriber"/>
51+
<argument type="service">
52+
<service class="Symfony\Component\Stopwatch\Stopwatch" public="false">
53+
<tag name="kernel.reset" method="reset"/>
54+
<argument>true</argument>
55+
</service>
56+
</argument>
57+
</service>
58+
</argument>
59+
</call>
60+
</service>
61+
</argument>
62+
</service>
63+
<service id="TSantos\Serializer\SerializerInterface" alias="tsantos_serializer" public="true"/>
64+
</services>
65+
</container>

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Config\Resource\GlobResource;
1919
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
2020
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
2122
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
2223
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2324
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
@@ -807,4 +808,16 @@ public function testBindings()
807808
'$factory' => 'factory',
808809
), array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings()));
809810
}
811+
812+
public function testTsantosContainer()
813+
{
814+
$container = new ContainerBuilder();
815+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
816+
$loader->load('services_tsantos.xml');
817+
$container->compile();
818+
819+
$dumper = new PhpDumper($container);
820+
$dump = $dumper->dump();
821+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_tsantos.php', $dumper->dump());
822+
}
810823
}

0 commit comments

Comments
 (0)