From 9477e96da2d3dd2f3ee69931897fe93c610048fa Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 12 May 2014 16:13:41 +0200 Subject: [PATCH 1/5] [HttpKernel] removed absolute paths from the generated container --- src/Symfony/Component/HttpKernel/Kernel.php | 35 +++++++++++++++++++ .../app/cache/dev/withAbsolutePaths.php | 7 ++++ .../app/cache/dev/withoutAbsolutePaths.php | 7 ++++ .../Fixtures/DumpedContainers/composer.json | 1 + .../Tests/Fixtures/KernelForTest.php | 5 +++ .../Component/HttpKernel/Tests/KernelTest.php | 28 +++++++++++++++ .../Component/HttpKernel/composer.json | 4 ++- 7 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php create mode 100644 src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withoutAbsolutePaths.php create mode 100644 src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/composer.json diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index b5e3744de6f0e..fae1eb39376dc 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -32,6 +32,7 @@ use Symfony\Component\Config\Loader\DelegatingLoader; use Symfony\Component\Config\ConfigCache; use Symfony\Component\ClassLoader\ClassCollectionLoader; +use Symfony\Component\Filesystem\Filesystem; /** * The Kernel is the heart of the Symfony system. @@ -714,9 +715,43 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container $content = static::stripComments($content); } + $content = $this->removeAbsolutePathsFromContainer($content); + $cache->write($content, $container->getResources()); } + /** + * Converts absolute paths to relative ones in the dumped container. + */ + private function removeAbsolutePathsFromContainer($content) + { + if (!class_exists('Symfony\Component\Filesystem\Filesystem')) { + return $content; + } + + // find the "real" root dir (by finding the composer.json file) + $rootDir = $this->getRootDir(); + $previous = $rootDir; + while (!file_exists($rootDir.'/composer.json')) { + if ($previous === $rootDir = realpath($rootDir.'/..')) { + // unable to detect the project root, give up + return $content; + } + + $previous = $rootDir; + } + + $rootDir = rtrim($rootDir, '/'); + $cacheDir = $this->getCacheDir(); + $filesystem = new Filesystem(); + + return preg_replace_callback("{'([^']*)(".preg_quote($rootDir)."[^']*)'}", function ($match) use ($filesystem, $cacheDir) { + $prefix = isset($match[1]) && $match[1] ? "'$match[1]'.__DIR__.'/" : "__DIR__.'/"; + + return $prefix.rtrim($filesystem->makePathRelative($match[2], $cacheDir), '/')."'"; + }, $content); + } + /** * Returns a loader for the container. * diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php b/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php new file mode 100644 index 0000000000000..54e0dedc4bfd5 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php @@ -0,0 +1,7 @@ +'ROOT_DIR/app/cache/dev/foo' +'ROOT_DIR/app/cache/foo' +'ROOT_DIR/foo/bar.php' + +'/some/where/else/foo' + +'file:ROOT_DIR/app/cache/dev/profiler' diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withoutAbsolutePaths.php b/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withoutAbsolutePaths.php new file mode 100644 index 0000000000000..09ce8b5683927 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withoutAbsolutePaths.php @@ -0,0 +1,7 @@ +__DIR__.'/foo' +__DIR__.'/../foo' +__DIR__.'/../../../foo/bar.php' + +'/some/where/else/foo' + +'file:'.__DIR__.'/profiler' diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/composer.json b/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/composer.json new file mode 100644 index 0000000000000..0967ef424bce6 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/composer.json @@ -0,0 +1 @@ +{} diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php b/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php index 5fd61bbc7e7ce..c7396dd8a74bf 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php @@ -16,6 +16,11 @@ class KernelForTest extends Kernel { + public function setRootDir($dir) + { + $this->rootDir = $dir; + } + public function getBundleMap() { return $this->bundleMap; diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index bc9876df3b12c..30e201fee5907 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -722,6 +722,34 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface() $kernel->terminate(Request::create('/'), new Response()); } + public function testRemoveAbsolutePathsFromContainer() + { + $kernel = new KernelForTest('dev', true); + $kernel->setRootDir($symfonyRootDir = __DIR__.'/Fixtures/DumpedContainers/app'); + + $content = file_get_contents($symfonyRootDir.'/cache/dev/withAbsolutePaths.php'); + $content = str_replace('ROOT_DIR', __DIR__.'/Fixtures/DumpedContainers', $content); + + $m = new \ReflectionMethod($kernel, 'removeAbsolutePathsFromContainer'); + $m->setAccessible(true); + $content = $m->invoke($kernel, $content); + $this->assertEquals(file_get_contents($symfonyRootDir.'/cache/dev/withoutAbsolutePaths.php'), $content); + } + + public function testRemoveAbsolutePathsFromContainerGiveUpWhenComposerJsonPathNotGuessable() + { + $kernel = new KernelForTest('dev', true); + $kernel->setRootDir($symfonyRootDir = sys_get_temp_dir()); + + $content = file_get_contents(__DIR__.'/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php'); + $content = str_replace('ROOT_DIR', __DIR__.'/Fixtures/DumpedContainers', $content); + + $m = new \ReflectionMethod($kernel, 'removeAbsolutePathsFromContainer'); + $m->setAccessible(true); + $newContent = $m->invoke($kernel, $content); + $this->assertEquals($newContent, $content); + } + /** * Returns a mock for the BundleInterface * diff --git a/src/Symfony/Component/HttpKernel/composer.json b/src/Symfony/Component/HttpKernel/composer.json index 24bed4a2d1df2..7105b5cb12817 100644 --- a/src/Symfony/Component/HttpKernel/composer.json +++ b/src/Symfony/Component/HttpKernel/composer.json @@ -29,6 +29,7 @@ "symfony/console": "~2.2", "symfony/dependency-injection": "~2.0", "symfony/finder": "~2.0", + "symfony/filesystem": "~2.4", "symfony/process": "~2.0", "symfony/routing": "~2.2", "symfony/stopwatch": "~2.2", @@ -40,7 +41,8 @@ "symfony/config": "", "symfony/console": "", "symfony/dependency-injection": "", - "symfony/finder": "" + "symfony/finder": "", + "symfony/filesystem": "" }, "autoload": { "psr-0": { "Symfony\\Component\\HttpKernel\\": "" } From 47634ccb32797f640310e55714e133b3c370e651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20L=C3=A9v=C3=AAque?= Date: Sun, 18 May 2014 15:17:34 +0200 Subject: [PATCH 2/5] Fix "absolute path" when we look to the cache directory We should get __DIR__ instead of __DIR__ . '/.' --- src/Symfony/Component/HttpKernel/Kernel.php | 10 ++++++++-- .../app/cache/dev/withAbsolutePaths.php | 1 + .../app/cache/dev/withoutAbsolutePaths.php | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index fae1eb39376dc..11e3a42a4b041 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -746,9 +746,15 @@ private function removeAbsolutePathsFromContainer($content) $filesystem = new Filesystem(); return preg_replace_callback("{'([^']*)(".preg_quote($rootDir)."[^']*)'}", function ($match) use ($filesystem, $cacheDir) { - $prefix = isset($match[1]) && $match[1] ? "'$match[1]'.__DIR__.'/" : "__DIR__.'/"; + $prefix = isset($match[1]) && $match[1] ? "'$match[1]'.__DIR__" : "__DIR__"; - return $prefix.rtrim($filesystem->makePathRelative($match[2], $cacheDir), '/')."'"; + $relativePath = rtrim($filesystem->makePathRelative($match[2], $cacheDir), '/'); + + if ($relativePath === '.') { + return $prefix; + } + + return $prefix . ".'/" . $relativePath . "'"; }, $content); } diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php b/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php index 54e0dedc4bfd5..b8644ecb9fe00 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php @@ -1,6 +1,7 @@ 'ROOT_DIR/app/cache/dev/foo' 'ROOT_DIR/app/cache/foo' 'ROOT_DIR/foo/bar.php' +'ROOT_DIR/app/cache/dev' '/some/where/else/foo' diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withoutAbsolutePaths.php b/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withoutAbsolutePaths.php index 09ce8b5683927..36823dfc160c8 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withoutAbsolutePaths.php +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/DumpedContainers/app/cache/dev/withoutAbsolutePaths.php @@ -1,6 +1,7 @@ __DIR__.'/foo' __DIR__.'/../foo' __DIR__.'/../../../foo/bar.php' +__DIR__ '/some/where/else/foo' From fc208acd4919aea505058174c1cebd65daf69b17 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 18 May 2014 17:36:43 +0200 Subject: [PATCH 3/5] fixed CS --- src/Symfony/Component/HttpKernel/Kernel.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 11e3a42a4b041..293b474f07b5f 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -748,13 +748,11 @@ private function removeAbsolutePathsFromContainer($content) return preg_replace_callback("{'([^']*)(".preg_quote($rootDir)."[^']*)'}", function ($match) use ($filesystem, $cacheDir) { $prefix = isset($match[1]) && $match[1] ? "'$match[1]'.__DIR__" : "__DIR__"; - $relativePath = rtrim($filesystem->makePathRelative($match[2], $cacheDir), '/'); - - if ($relativePath === '.') { + if ('.' === $relativePath = rtrim($filesystem->makePathRelative($match[2], $cacheDir), '/')) { return $prefix; } - return $prefix . ".'/" . $relativePath . "'"; + return $prefix.".'/".$relativePath."'"; }, $content); } From 9da641d8f63ec9cf89f11535f12e045bc60d21c9 Mon Sep 17 00:00:00 2001 From: Arturs Vonda Date: Fri, 23 May 2014 17:32:35 +0300 Subject: [PATCH 4/5] Make rootPath part of regex greedy - Fixes #10977 --- src/Symfony/Component/HttpKernel/Kernel.php | 45 ++++++++++++++----- .../Tests/Fixtures/KernelForTest.php | 5 +++ .../Component/HttpKernel/Tests/KernelTest.php | 16 +++++++ 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 293b474f07b5f..6ddc9b375b1af 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -53,6 +53,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface protected $bundleMap; protected $container; protected $rootDir; + protected $realRootDir; protected $environment; protected $debug; protected $booted = false; @@ -729,24 +730,17 @@ private function removeAbsolutePathsFromContainer($content) return $content; } - // find the "real" root dir (by finding the composer.json file) - $rootDir = $this->getRootDir(); - $previous = $rootDir; - while (!file_exists($rootDir.'/composer.json')) { - if ($previous === $rootDir = realpath($rootDir.'/..')) { - // unable to detect the project root, give up - return $content; - } - - $previous = $rootDir; + $rootDir = $this->getRealRootDir(); + if (!$rootDir) { + return $content; } $rootDir = rtrim($rootDir, '/'); $cacheDir = $this->getCacheDir(); $filesystem = new Filesystem(); - return preg_replace_callback("{'([^']*)(".preg_quote($rootDir)."[^']*)'}", function ($match) use ($filesystem, $cacheDir) { - $prefix = isset($match[1]) && $match[1] ? "'$match[1]'.__DIR__" : "__DIR__"; + return preg_replace_callback("{'([^']*?)(".preg_quote($rootDir)."[^']*)'}", function ($match) use ($filesystem, $cacheDir) { + $prefix = !empty($match[1]) ? "'$match[1]'.__DIR__" : "__DIR__"; if ('.' === $relativePath = rtrim($filesystem->makePathRelative($match[2], $cacheDir), '/')) { return $prefix; @@ -756,6 +750,33 @@ private function removeAbsolutePathsFromContainer($content) }, $content); } + /** + * Find the "real" root dir (by finding the composer.json file) + * + * @return null|string + */ + private function getRealRootDir() + { + if (null !== $this->realRootDir) { + return $this->realRootDir; + } + + $rootDir = $this->getRootDir(); + $previous = $rootDir; + while (!file_exists($rootDir.'/composer.json')) { + if ($previous === $rootDir = realpath($rootDir.'/..')) { + // unable to detect the project root, give up + return $this->realRootDir = false; + } + + $previous = $rootDir; + } + + $this->realRootDir = $rootDir; + + return $this->realRootDir; + } + /** * Returns a loader for the container. * diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php b/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php index c7396dd8a74bf..000f823c6ada3 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php @@ -39,4 +39,9 @@ public function isBooted() { return $this->booted; } + + public function setRealRootDir($dir) + { + $this->realRootDir = $dir; + } } diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 30e201fee5907..c84e94962f906 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -750,6 +750,22 @@ public function testRemoveAbsolutePathsFromContainerGiveUpWhenComposerJsonPathNo $this->assertEquals($newContent, $content); } + public function testRemoveAbsolutePathsFromContainerWithSpecialCase() + { + $kernel = new KernelForTest('dev', true); + $kernel->setRootDir('/app/app'); + $kernel->setRealRootDir('/app'); + $symfonyRootDir = __DIR__.'/Fixtures/DumpedContainers/app'; + + $content = file_get_contents($symfonyRootDir.'/cache/dev/withAbsolutePaths.php'); + $content = str_replace('ROOT_DIR', '/app', $content); + + $m = new \ReflectionMethod($kernel, 'removeAbsolutePathsFromContainer'); + $m->setAccessible(true); + $content = $m->invoke($kernel, $content); + $this->assertEquals(file_get_contents($symfonyRootDir.'/cache/dev/withoutAbsolutePaths.php'), $content); + } + /** * Returns a mock for the BundleInterface * From d8cc22f4fb74f75e056197b64691dc431d3c5717 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 26 May 2014 18:12:56 +0200 Subject: [PATCH 5/5] [HttpKernel] simplified some tests --- .../Component/HttpKernel/Tests/KernelTest.php | 51 ++++++++----------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index c84e94962f906..8571dddd87a97 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -722,48 +722,37 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface() $kernel->terminate(Request::create('/'), new Response()); } - public function testRemoveAbsolutePathsFromContainer() - { - $kernel = new KernelForTest('dev', true); - $kernel->setRootDir($symfonyRootDir = __DIR__.'/Fixtures/DumpedContainers/app'); - - $content = file_get_contents($symfonyRootDir.'/cache/dev/withAbsolutePaths.php'); - $content = str_replace('ROOT_DIR', __DIR__.'/Fixtures/DumpedContainers', $content); - - $m = new \ReflectionMethod($kernel, 'removeAbsolutePathsFromContainer'); - $m->setAccessible(true); - $content = $m->invoke($kernel, $content); - $this->assertEquals(file_get_contents($symfonyRootDir.'/cache/dev/withoutAbsolutePaths.php'), $content); - } - - public function testRemoveAbsolutePathsFromContainerGiveUpWhenComposerJsonPathNotGuessable() + /** + * @dataProvider getRemoveAbsolutePathsFromContainerData + */ + public function testRemoveAbsolutePathsFromContainer($symfonyRootDir, $realRootDir, $replacement, $replaced) { $kernel = new KernelForTest('dev', true); - $kernel->setRootDir($symfonyRootDir = sys_get_temp_dir()); + $kernel->setRootDir($symfonyRootDir); + if (null !== $realRootDir) { + $kernel->setRealRootDir($realRootDir); + } $content = file_get_contents(__DIR__.'/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php'); - $content = str_replace('ROOT_DIR', __DIR__.'/Fixtures/DumpedContainers', $content); + $content = str_replace('ROOT_DIR', $replacement, $content); $m = new \ReflectionMethod($kernel, 'removeAbsolutePathsFromContainer'); $m->setAccessible(true); $newContent = $m->invoke($kernel, $content); - $this->assertEquals($newContent, $content); + if ($replaced) { + $this->assertEquals(file_get_contents(__DIR__.'/Fixtures/DumpedContainers/app/cache/dev/withoutAbsolutePaths.php'), $newContent); + } else { + $this->assertEquals($newContent, $content); + } } - public function testRemoveAbsolutePathsFromContainerWithSpecialCase() + public function getRemoveAbsolutePathsFromContainerData() { - $kernel = new KernelForTest('dev', true); - $kernel->setRootDir('/app/app'); - $kernel->setRealRootDir('/app'); - $symfonyRootDir = __DIR__.'/Fixtures/DumpedContainers/app'; - - $content = file_get_contents($symfonyRootDir.'/cache/dev/withAbsolutePaths.php'); - $content = str_replace('ROOT_DIR', '/app', $content); - - $m = new \ReflectionMethod($kernel, 'removeAbsolutePathsFromContainer'); - $m->setAccessible(true); - $content = $m->invoke($kernel, $content); - $this->assertEquals(file_get_contents($symfonyRootDir.'/cache/dev/withoutAbsolutePaths.php'), $content); + return array( + array(__DIR__.'/Fixtures/DumpedContainers/app', null, __DIR__.'/Fixtures/DumpedContainers', true), + array(sys_get_temp_dir(), null, __DIR__.'/Fixtures/DumpedContainers', false), + array('/app/app', '/app', '/app', true), + ); } /**