8000 bug #10894 [HttpKernel] removed absolute paths from the generated con… · symfony/symfony@735e9a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 735e9a4

Browse files
committed
bug #10894 [HttpKernel] removed absolute paths from the generated container (fabpot)
This PR was merged into the 2.3 branch. Discussion ---------- [HttpKernel] removed absolute paths from the generated container | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | first step to resolve #6484, #3079, and #9238 | License | MIT | Doc PR | n/a This PR converts absolute paths to relative ones in the dumped container. The code is a bit "ugly", but it gets the job done and I'm not sure that there is a more elegant way without breaking everything. Commits ------- c1450b4 [HttpKernel] removed absolute paths from the generated container
2 parents 5c91dc1 + c1450b4 commit 735e9a4

File tree

7 files changed

+86
-1
lines changed

7 files changed

+86
-1
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\Config\Loader\DelegatingLoader;
3333
use Symfony\Component\Config\ConfigCache;
3434
use Symfony\Component\ClassLoader\ClassCollectionLoader;
35+
use Symfony\Component\Filesystem\Filesystem;
3536

3637
/**
3738
* The Kernel is the heart of the Symfony system.
@@ -716,9 +717,43 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
716717
$content = static::stripComments($content);
717718
}
718719

720+
$content = $this->removeAbsolutePathsFromContainer($content);
721+
719722
$cache->write($content, $container->getResources());
720723
}
721724

725+
/**
726+
* Converts absolute paths to relative ones in the dumped container.
727+
*/
728+
private function removeAbsolutePathsFromContainer($content)
729+
{
730+
if (!class_exists('Symfony\Component\Filesystem\Filesystem')) {
731+
return $content;
732+
}
733+
734+
// find the "real" root dir (by finding the composer.json file)
735+
$rootDir = $this->getRootDir();
736+
$previous = $rootDir;
737+
while (!file_exists($rootDir.'/composer.json')) {
738+
if ($previous === $rootDir = realpath($rootDir.'/..')) {
739+
// unable to detect the project root, give up
740+
return $content;
741+
}
742+
743+
$previous = $rootDir;
744+
}
745+
746+
$rootDir = rtrim($rootDir, '/');
747+
$cacheDir = $this->getCacheDir();
748+
$filesystem = new Filesystem();
749+
750+
return preg_replace_callback("{'([^']*)(".preg_quote($rootDir)."[^']*)'}", function ($match) use ($filesystem, $cacheDir) {
751+
$prefix = isset($match[1]) && $match[1] ? "'$match[1]'.__DIR__.'/" : "__DIR__.'/";
752+
753+
return $prefix.rtrim($filesystem->makePathRelative($match[2], $cacheDir), '/')."'";
754+
}, $content);
755+
}
756+
722757
/**
723758
* Returns a loader for the container.
724759
*
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'ROOT_DIR/app/cache/dev/foo'
2+
'ROOT_DIR/app/cache/foo'
3+
'ROOT_DIR/foo/bar.php'
4+
5+
'/some/where/else/foo'
6+
7+
'file:ROOT_DIR/app/cache/dev/profiler'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__DIR__.'/foo'
2+
__DIR__.'/../foo'
3+
__DIR__.'/../../../foo/bar.php'
4+
5+
'/some/where/else/foo'
6+
7+
'file:'.__DIR__.'/profiler'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
class KernelForTest extends Kernel
1818
{
19+
public function setRootDir($dir)
20+
{
21+
$this->rootDir = $dir;
22+
}
23+
1924
public function getBundleMap()
2025
{
2126
return $this->bundleMap;

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,34 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
824824
$kernel->terminate(Request::create('/'), new Response());
825825
}
826826

827+
public function testRemoveAbsolutePathsFromContainer()
828+
{
829+
$kernel = new KernelForTest('dev', true);
830+
$kernel->setRootDir($symfonyRootDir = __DIR__.'/Fixtures/DumpedContainers/app');
831+
832+
$content = file_get_contents($symfonyRootDir.'/cache/dev/withAbsolutePaths.php');
833+
$content = str_replace('ROOT_DIR', __DIR__.'/Fixtures/DumpedContainers', $content);
834+
835+
$m = new \ReflectionMethod($kernel, 'removeAbsolutePathsFromContainer');
836+
$m->setAccessible(true);
837+
$content = $m->invoke($kernel, $content);
838+
$this->assertEquals(file_get_contents($symfonyRootDir.'/cache/dev/withoutAbsolutePaths.php'), $content);
839+
}
840+
841+
public function testRemoveAbsolutePathsFromContainerGiveUpWhenComposerJsonPathNotGuessable()
842+
{
843+
$kernel = new KernelForTest('dev', true);
844+
$kernel->setRootDir($symfonyRootDir = sys_get_temp_dir());
845+
846+
$content = file_get_contents(__DIR__.'/Fixtures/DumpedContainers/app/cache/dev/withAbsolutePaths.php');
847+
$content = str_replace('ROOT_DIR', __DIR__.'/Fixtures/DumpedContainers', $content);
848+
849+
$m = new \ReflectionMethod($kernel, 'removeAbsolutePathsFromContainer');
850+
$m->setAccessible(true);
851+
$newContent = $m->invoke($kernel, $content);
852+
$this->assertEquals($newContent, $content);
853+
}
854+
827855
protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
828856
{
829857
$bundle = $this

src/Symfony/Component/HttpKernel/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"symfony/console": "~2.2",
3030
"symfony/dependency-injection": "~2.0",
3131
"symfony/finder": "~2.0",
32+
"symfony/filesystem": "~2.4",
3233
"symfony/process": "~2.0",
3334
"symfony/routing": "~2.2",
3435
"symfony/stopwatch": "~2.2",
@@ -40,7 +41,8 @@
4041
"symfony/config": "",
4142
"symfony/console": "",
4243
"symfony/dependency-injection": "",
43-
"symfony/finder": ""
44+
"symfony/finder": "",
45+
"symfony/filesystem": ""
4446
},
4547
"autoload": {
4648
"psr-0": { "Symfony\\Component\\HttpKernel\\": "" }

0 commit comments

Comments
 (0)
0