Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HttpKernel] removed absolute paths from the generated container #10999

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -32,6 +32,7 @@
use Symfony\Component\Config\Loader\DelegatingLoader; use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\ConfigCache; use Symfony\Component\Config\ConfigCache;
use Symfony\Component\ClassLoader\ClassCollectionLoader; use Symfony\Component\ClassLoader\ClassCollectionLoader;
use Symfony\Component\Filesystem\Filesystem;


/** /**
* The Kernel is the heart of the Symfony system. * The Kernel is the heart of the Symfony system.
Expand All @@ -52,6 +53,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface
protected $bundleMap; protected $bundleMap;
protected $container; protected $container;
protected $rootDir; protected $rootDir;
protected $realRootDir;
protected $environment; protected $environment;
protected $debug; protected $debug;
protected $booted = false; protected $booted = false;
Expand Down Expand Up @@ -714,9 +716,67 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
$content = static::stripComments($content); $content = static::stripComments($content);
} }


$content = $this->removeAbsolutePathsFromContainer($content);

$cache->write($content, $container->getResources()); $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;
}

$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 = !empty($match[1]) ? "'$match[1]'.__DIR__" : "__DIR__";

if ('.' === $relativePath = rtrim($filesystem->makePathRelative($match[2], $cacheDir), '/')) {
return $prefix;
}

return $prefix.".'/".$relativePath."'";
}, $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. * Returns a loader for the container.
* *
Expand Down
@@ -0,0 +1,8 @@
'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'

'file:ROOT_DIR/app/cache/dev/profiler'
@@ -0,0 +1,8 @@
__DIR__.'/foo'
__DIR__.'/../foo'
__DIR__.'/../../../foo/bar.php'
__DIR__

'/some/where/else/foo'

'file:'.__DIR__.'/profiler'
@@ -0,0 +1 @@
{}
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php
Expand Up @@ -16,6 +16,11 @@


class KernelForTest extends Kernel class KernelForTest extends Kernel
{ {
public function setRootDir($dir)
{
$this->rootDir = $dir;
}

public function getBundleMap() public function getBundleMap()
{ {
return $this->bundleMap; return $this->bundleMap;
Expand All @@ -34,4 +39,9 @@ public function isBooted()
{ {
return $this->booted; return $this->booted;
} }

public function setRealRootDir($dir)
{
$this->realRootDir = $dir;
}
} }
33 changes: 33 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Expand Up @@ -722,6 +722,39 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
$kernel->terminate(Request::create('/'), new Response()); $kernel->terminate(Request::create('/'), new Response());
} }


/**
* @dataProvider getRemoveAbsolutePathsFromContainerData
*/
public function testRemoveAbsolutePathsFromContainer($symfonyRootDir, $realRootDir, $replacement, $replaced)
{
$kernel = new KernelForTest('dev', true);
$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', $replacement, $content);

$m = new \ReflectionMethod($kernel, 'removeAbsolutePathsFromContainer');
$m->setAccessible(true);
$newContent = $m->invoke($kernel, $content);
if ($replaced) {
$this->assertEquals(file_get_contents(__DIR__.'/Fixtures/DumpedContainers/app/cache/dev/withoutAbsolutePaths.php'), $newContent);
} else {
$this->assertEquals($newContent, $content);
}
}

public function getRemoveAbsolutePathsFromContainerData()
{
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),
);
}

/** /**
* Returns a mock for the BundleInterface * Returns a mock for the BundleInterface
* *
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/HttpKernel/composer.json
Expand Up @@ -29,6 +29,7 @@
"symfony/console": "~2.2", "symfony/console": "~2.2",
"symfony/dependency-injection": "~2.0", "symfony/dependency-injection": "~2.0",
"symfony/finder": "~2.0", "symfony/finder": "~2.0",
"symfony/filesystem": "~2.4",
"symfony/process": "~2.0", "symfony/process": "~2.0",
"symfony/routing": "~2.2", "symfony/routing": "~2.2",
"symfony/stopwatch": "~2.2", "symfony/stopwatch": "~2.2",
Expand All @@ -40,7 +41,8 @@
"symfony/config": "", "symfony/config": "",
"symfony/console": "", "symfony/console": "",
"symfony/dependency-injection": "", "symfony/dependency-injection": "",
"symfony/finder": "" "symfony/finder": "",
"symfony/filesystem": ""
}, },
"autoload": { "autoload": {
"psr-0": { "Symfony\\Component\\HttpKernel\\": "" } "psr-0": { "Symfony\\Component\\HttpKernel\\": "" }
Expand Down