diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index f12d813890615..16c319fe7644d 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -1917,7 +1917,13 @@ private function export($value) private function doExport($value) { - $export = var_export($value, true); + if (is_string($value) && false !== strpos($value, "\n")) { + $cleanParts = explode("\n", $value); + $cleanParts = array_map(function ($part) { return var_export($part, true); }, $cleanParts); + $export = implode('."\n".', $cleanParts); + } else { + $export = var_export($value, true); + } if ("'" === $export[0] && $export !== $resolvedExport = $this->container->resolveEnvPlaceholders($export, "'.\$this->getEnv('string:%s').'")) { $export = $resolvedExport; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 014ce3c16f885..70203bd4d9a1a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -187,6 +187,15 @@ public function testServicesWithAnonymousFactories() $this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories'); } + public function testDumpAsFilesWhenServicePropertiesContainSpaces() + { + $container = include self::$fixturesPath.'/containers/container_nl_in_argument.php'; + $container->compile(); + $dumper = new PhpDumper($container); + $dump = print_r($dumper->dump(array('as_files' => true, 'file' => __DIR__)), true); + $this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/container_nl_in_argument_as_files.txt', $dump); + } + public function testAddServiceIdWithUnsupportedCharacters() { $class = 'Symfony_DI_PhpDumper_Test_Unsupported_Characters'; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_nl_in_argument.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_nl_in_argument.php new file mode 100644 index 0000000000000..5b0f9ebd76474 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_nl_in_argument.php @@ -0,0 +1,19 @@ +register('foo', 'Foo') + ->addArgument("string with\nnew line") + ->setPublic(true) +; + +$container + ->register('foo2', 'Foo') + ->addArgument("string with\nnl") + ->setPublic(true) +; + +return $container; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/container_nl_in_argument_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/container_nl_in_argument_as_files.txt new file mode 100644 index 0000000000000..de17e9af62628 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/container_nl_in_argument_as_files.txt @@ -0,0 +1,106 @@ +Array +( + [ContainerAx0roru/removed-ids.php] => true, + 'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true, +); + + [ContainerAx0roru/getFooService.php] => services['foo'] = new \Foo('string with'."\n".'new line'); + + [ContainerAx0roru/getFoo2Service.php] => services['foo2'] = new \Foo('string with'."\n".'nl'); + + [ContainerAx0roru/Container.php] => targetDirs[0] = dirname(__DIR__); + for ($i = 1; $i <= 5; ++$i) { + $this->targetDirs[$i] = $dir = dirname($dir); + } + $this->services = array(); + $this->fileMap = array( + 'foo' => __DIR__.'/getFooService.php', + 'foo2' => __DIR__.'/getFoo2Service.php', + ); + + $this->aliases = array(); + } + + public function getRemovedIds() + { + return require __DIR__.'/removed-ids.php'; + } + + 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 version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED); + + return true; + } + + protected function load($file, $lazyLoad = true) + { + return require $file; + } +} + + [ProjectServiceContainer.php] =>