From d60eb1aa338d6a0ff98c43e8f27de28dc634cf62 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 2 Oct 2018 08:41:34 +0200 Subject: [PATCH] [DI] fix dumping setters before their inlined instances --- .../DependencyInjection/Dumper/PhpDumper.php | 3 +- .../Fixtures/php/services_deep_graph.php | 1 + .../Tests/Fixtures/php/services_tsantos.php | 89 +++++++++++++++++++ .../Tests/Fixtures/xml/services_tsantos.xml | 65 ++++++++++++++ .../Tests/Loader/XmlFileLoaderTest.php | 13 +++ 5 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_tsantos.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_tsantos.xml diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 9c1e22f638..c1916936bc 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -780,8 +780,9 @@ EOTXT $code = ''; $arguments = array($definition->getProperties(), $definition->getMethodCalls(), $definition->getConfigurator()); - $hasSelfRef = $this->addInlineVariables($code, $tail, $id, $arguments, false) || $hasSelfRef; + $hasSelfRef = $this->addInlineVariables($code, $code, $id, $arguments, false) || $hasSelfRef; + $code .= '' !== $code ? "\n" : ''; $code .= $this->addServiceProperties($definition, $name); $code .= $this->addServiceMethodCalls($definition, $name); $code .= $this->addServiceConfigurator($definition, $name); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_deep_graph.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_deep_graph.php index 4bfa86e211..03441e6a49 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_deep_graph.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_deep_graph.php @@ -85,6 +85,7 @@ class Symfony_DI_PhpDumper_Test_Deep_Graph extends Container $b = new \stdClass(); $c = new \stdClass(); $c->p3 = new \stdClass(); + $b->p2 = $c; return $this->services['foo'] = new \Symfony\Component\DependencyInjection\Tests\Dumper\FooForDeepGraph($a, $b); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_tsantos.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_tsantos.php new file mode 100644 index 0000000000..c8b3f9d50d --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_tsantos.php @@ -0,0 +1,89 @@ +services = array(); + $this->normalizedIds = array( + 'tsantos\\serializer\\serializerinterface' => 'TSantos\\Serializer\\SerializerInterface', + ); + $this->methodMap = array( + 'tsantos_serializer' => 'getTsantosSerializerService', + ); + $this->aliases = array( + 'TSantos\\Serializer\\SerializerInterface' => 'tsantos_serializer', + ); + } + + public function getRemovedIds() + { + return array( + 'Psr\\Container\\ContainerInterface' => true, + 'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true, + ); + } + + public function compile() + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled() + { + return true; + } + + public function isFrozen() + { + @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); + + return true; + } + + /** + * Gets the public 'tsantos_serializer' shared service. + * + * @return \TSantos\Serializer\EventEmitterSerializer + */ + protected function getTsantosSerializerService() + { + $a = new \TSantos\Serializer\NormalizerRegistry(); + + $d = new \TSantos\Serializer\EventDispatcher\EventDispatcher(); + $d->addSubscriber(new \TSantos\SerializerBundle\EventListener\StopwatchListener(new \Symfony\Component\Stopwatch\Stopwatch(true))); + + $this->services['tsantos_serializer'] = $instance = new \TSantos\Serializer\EventEmitterSerializer(new \TSantos\Serializer\Encoder\JsonEncoder(), $a, $d); + + $b = new \TSantos\Serializer\Normalizer\CollectionNormalizer(); + + $b->setSerializer($instance); + + $c = new \TSantos\Serializer\Normalizer\JsonNormalizer(); + + $c->setSerializer($instance); + + $a->add(new \TSantos\Serializer\Normalizer\ObjectNormalizer(new \TSantos\SerializerBundle\Serializer\CircularReferenceHandler())); + $a->add($b); + $a->add($c); + + return $instance; + } +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_tsantos.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_tsantos.xml new file mode 100644 index 0000000000..bb310b279d --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_tsantos.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php index 969bb09f0c..153f7f7fb6 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php @@ -18,6 +18,7 @@ use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Config\Resource\GlobResource; use Symfony\Component\DependencyInjection\Argument\IteratorArgument; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Dumper\PhpDumper; use Symfony\Component\DependencyInjection\Loader\IniFileLoader; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; @@ -807,4 +808,16 @@ class XmlFileLoaderTest extends TestCase '$factory' => 'factory', ), array_map(function ($v) { return $v->getValues()[0]; }, $definition->getBindings())); } + + public function testTsantosContainer() + { + $container = new ContainerBuilder(); + $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')); + $loader->load('services_tsantos.xml'); + $container->compile(); + + $dumper = new PhpDumper($container); + $dump = $dumper->dump(); + $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_tsantos.php', $dumper->dump()); + } }