diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index b4de1a058d20e..305f8176ba498 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -1687,23 +1687,19 @@ private function dumpValue($value, $interpolate = true) private function dumpServiceClosure(Reference $reference, $interpolate, $oneLine) { - $type = ''; - if (PHP_VERSION_ID >= 70000 && $reference instanceof TypedReference) { - $type = $reference->getType(); - if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $reference->getInvalidBehavior()) { - $type = ': \\'.$type; - } elseif (PHP_VERSION_ID >= 70100) { - $type = ': ?\\'.$type; - } else { - $type = ''; - } + $code = $this->dumpValue($reference, $interpolate); + + if ($reference instanceof TypedReference) { + $code = sprintf('$f = function (\\%s $v%s) { return $v; }; return $f(%s);', $reference->getType(), ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior() ? ' = null' : '', $code); + } else { + $code = sprintf('return %s;', $code); } if ($oneLine) { - return sprintf('function ()%s { return %s; }', $type, $this->dumpValue($reference, $interpolate)); + return sprintf('function () { %s }', $code); } - return sprintf("function ()%s {\n return %s;\n }", $type, $this->dumpValue($reference, $interpolate)); + return sprintf("function () {\n %s\n }", $code); } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 1a51c8f44caef..bd77908367d51 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -648,7 +648,6 @@ public function testServiceLocator() $dumper = new PhpDumper($container); - $suffix = PHP_VERSION_ID >= 70100 ? '71' : (PHP_VERSION_ID >= 70000 ? '70' : '55'); - $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_locator_php'.$suffix.'.php', $dumper->dump()); + $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_locator.php', $dumper->dump()); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php55.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php similarity index 94% rename from src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php55.php rename to src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php index 75f87de16cc34..53582cfe5a5b1 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php55.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php @@ -87,7 +87,7 @@ protected function getFooServiceService() return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(array('bar' => function () { return ${($_ = isset($this->services['bar_service']) ? $this->services['bar_service'] : $this->get('bar_service')) && false ?: '_'}; }, 'baz' => function () { - return ${($_ = isset($this->services['baz_service']) ? $this->services['baz_service'] : $this->getBazServiceService()) && false ?: '_'}; + $f = function (\stdClass $v) { return $v; }; return $f(${($_ = isset($this->services['baz_service']) ? $this->services['baz_service'] : $this->getBazServiceService()) && false ?: '_'}); })); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php70.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php70.php deleted file mode 100644 index ef9bda512d3fd..0000000000000 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php70.php +++ /dev/null @@ -1,110 +0,0 @@ -services = array(); - $this->normalizedIds = array( - 'psr\\container\\containerinterface' => 'Psr\\Container\\ContainerInterface', - 'symfony\\component\\dependencyinjection\\container' => 'Symfony\\Component\\DependencyInjection\\Container', - 'symfony\\component\\dependencyinjection\\containerinterface' => 'Symfony\\Component\\DependencyInjection\\ContainerInterface', - ); - $this->methodMap = array( - 'bar_service' => 'getBarServiceService', - 'baz_service' => 'getBazServiceService', - 'foo_service' => 'getFooServiceService', - ); - $this->privates = array( - 'baz_service' => true, - ); - - $this->aliases = array(); - } - - /** - * {@inheritdoc} - */ - public function compile() - { - throw new LogicException('You cannot compile a dumped frozen container.'); - } - - /** - * {@inheritdoc} - */ - public function isFrozen() - { - return true; - } - - /** - * Gets the 'bar_service' service. - * - * This service is shared. - * This method always returns the same instance of the service. - * - * @return \stdClass A stdClass instance - */ - protected function getBarServiceService() - { - return $this->services['bar_service'] = new \stdClass(${($_ = isset($this->services['baz_service']) ? $this->services['baz_service'] : $this->getBazServiceService()) && false ?: '_'}); - } - - /** - * Gets the 'foo_service' service. - * - * This service is shared. - * This method always returns the same instance of the service. - * - * @return \Symfony\Component\DependencyInjection\ServiceLocator A Symfony\Component\DependencyInjection\ServiceLocator instance - */ - protected function getFooServiceService() - { - return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(array('bar' => function () { - return ${($_ = isset($this->services['bar_service']) ? $this->services['bar_service'] : $this->get('bar_service')) && false ?: '_'}; - }, 'baz' => function (): \stdClass { - return ${($_ = isset($this->services['baz_service']) ? $this->services['baz_service'] : $this->getBazServiceService()) && false ?: '_'}; - })); - } - - /** - * Gets the 'baz_service' service. - * - * This service is shared. - * This method always returns the same instance of the service. - * - * This service is private. - * If you want to be able to request this service from the container directly, - * make it public, otherwise you might end up with broken code. - * - * @return \stdClass A stdClass instance - */ - protected function getBazServiceService() - { - return $this->services['baz_service'] = new \stdClass(); - } -} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php71.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php71.php deleted file mode 100644 index ef9bda512d3fd..0000000000000 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator_php71.php +++ /dev/null @@ -1,110 +0,0 @@ -services = array(); - $this->normalizedIds = array( - 'psr\\container\\containerinterface' => 'Psr\\Container\\ContainerInterface', - 'symfony\\component\\dependencyinjection\\container' => 'Symfony\\Component\\DependencyInjection\\Container', - 'symfony\\component\\dependencyinjection\\containerinterface' => 'Symfony\\Component\\DependencyInjection\\ContainerInterface', - ); - $this->methodMap = array( - 'bar_service' => 'getBarServiceService', - 'baz_service' => 'getBazServiceService', - 'foo_service' => 'getFooServiceService', - ); - $this->privates = array( - 'baz_service' => true, - ); - - $this->aliases = array(); - } - - /** - * {@inheritdoc} - */ - public function compile() - { - throw new LogicException('You cannot compile a dumped frozen container.'); - } - - /** - * {@inheritdoc} - */ - public function isFrozen() - { - return true; - } - - /** - * Gets the 'bar_service' service. - * - * This service is shared. - * This method always returns the same instance of the service. - * - * @return \stdClass A stdClass instance - */ - protected function getBarServiceService() - { - return $this->services['bar_service'] = new \stdClass(${($_ = isset($this->services['baz_service']) ? $this->services['baz_service'] : $this->getBazServiceService()) && false ?: '_'}); - } - - /** - * Gets the 'foo_service' service. - * - * This service is shared. - * This method always returns the same instance of the service. - * - * @return \Symfony\Component\DependencyInjection\ServiceLocator A Symfony\Component\DependencyInjection\ServiceLocator instance - */ - protected function getFooServiceService() - { - return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(array('bar' => function () { - return ${($_ = isset($this->services['bar_service']) ? $this->services['bar_service'] : $this->get('bar_service')) && false ?: '_'}; - }, 'baz' => function (): \stdClass { - return ${($_ = isset($this->services['baz_service']) ? $this->services['baz_service'] : $this->getBazServiceService()) && false ?: '_'}; - })); - } - - /** - * Gets the 'baz_service' service. - * - * This service is shared. - * This method always returns the same instance of the service. - * - * This service is private. - * If you want to be able to request this service from the container directly, - * make it public, otherwise you might end up with broken code. - * - * @return \stdClass A stdClass instance - */ - protected function getBazServiceService() - { - return $this->services['baz_service'] = new \stdClass(); - } -}