From 4dcf9e7d13a25932b2ab15d1264f6e9420ad97f9 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 10 Sep 2020 11:46:19 +0200 Subject: [PATCH] [DI] dump OS-indepent paths in the compiled container --- .../DependencyInjection/Dumper/PhpDumper.php | 10 +++++++++- .../Tests/Dumper/PhpDumperTest.php | 12 ++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 520a886deb..92d039c9ed 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -2017,7 +2017,15 @@ EOF; $suffix = $matches[0][1] + \strlen($matches[0][0]); $matches[0][1] += \strlen($matches[1][0]); $prefix = $matches[0][1] ? $this->doExport(substr($value, 0, $matches[0][1]), true).'.' : ''; - $suffix = isset($value[$suffix]) ? '.'.$this->doExport(substr($value, $suffix), true) : ''; + + if ('\\' === \DIRECTORY_SEPARATOR && isset($value[$suffix])) { + $cookie = '\\'.random_int(100000, \PHP_INT_MAX); + $suffix = '.'.$this->doExport(str_replace('\\', $cookie, substr($value, $suffix)), true); + $suffix = str_replace('\\'.$cookie, "'.\\DIRECTORY_SEPARATOR.'", $suffix); + } else { + $suffix = isset($value[$suffix]) ? '.'.$this->doExport(substr($value, $suffix), true) : ''; + } + $dirname = $this->asFiles ? '$this->containerDir' : '__DIR__'; $offset = 2 + $this->targetDirMaxMatches - \count($matches); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index de88e8b22f..5875253d86 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -233,7 +233,7 @@ class PhpDumperTest extends TestCase $dumper = new PhpDumper($container); $dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot']), true); if ('\\' === \DIRECTORY_SEPARATOR) { - $dump = str_replace('\\\\Fixtures\\\\includes\\\\foo.php', '/Fixtures/includes/foo.php', $dump); + $dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump); } $this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_as_files.txt', $dump); } @@ -260,7 +260,7 @@ class PhpDumperTest extends TestCase $dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'build_time' => 1563381341]), true); if ('\\' === \DIRECTORY_SEPARATOR) { - $dump = str_replace('\\\\Fixtures\\\\includes\\\\', '/Fixtures/includes/', $dump); + $dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump); } $this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_inlined_factories.txt', $dump); } @@ -286,7 +286,7 @@ class PhpDumperTest extends TestCase $dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'build_time' => 1563381341]), true); if ('\\' === \DIRECTORY_SEPARATOR) { - $dump = str_replace('\\\\Fixtures\\\\includes\\\\', '/Fixtures/includes/', $dump); + $dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump); } $this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_lazy_inlined_factories.txt', $dump); } @@ -304,7 +304,7 @@ class PhpDumperTest extends TestCase $dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__]), true); if ('\\' === \DIRECTORY_SEPARATOR) { - $dump = str_replace('\\\\Fixtures\\\\includes\\\\foo_lazy.php', '/Fixtures/includes/foo_lazy.php', $dump); + $dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump); } $this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services_non_shared_lazy_as_files.txt', $dump); } @@ -980,7 +980,7 @@ class PhpDumperTest extends TestCase $dumper = new PhpDumper($container); - $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_array_params.php', str_replace('\\\\Dumper', '/Dumper', $dumper->dump(['file' => self::$fixturesPath.'/php/services_array_params.php']))); + $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_array_params.php', str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dumper->dump(['file' => self::$fixturesPath.'/php/services_array_params.php']))); } public function testExpressionReferencingPrivateService() @@ -1143,7 +1143,7 @@ class PhpDumperTest extends TestCase $dump = $dumper->dump(['hot_path_tag' => 'container.hot_path', 'inline_class_loader_parameter' => 'inline_requires', 'file' => self::$fixturesPath.'/php/services_inline_requires.php']); if ('\\' === \DIRECTORY_SEPARATOR) { - $dump = str_replace("'\\\\includes\\\\HotPath\\\\", "'/includes/HotPath/", $dump); + $dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump); } $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_inline_requires.php', $dump);