Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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

Merged
merged 1 commit into from
May 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -716,9 +717,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.'/..')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoudln't $previous be updated in the loop too ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, fixed it now and added a test about that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot I think this stuff (the preg_replace_callback below) breaks the console. kernel.rootpath is now at app/cache/dev/../src can be reproduced by generating a bundle

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tvlooy this is already reverted see #10943

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wouterj thanks for the pointer

// 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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__DIR__.'/foo'
__DIR__.'/../foo'
__DIR__.'/../../../foo/bar.php'

'/some/where/else/foo'

'file:'.__DIR__.'/profiler'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

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

public function getBundleMap()
{
return $this->bundleMap;
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,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);
}

protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
{
$bundle = $this
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -40,7 +41,8 @@
"symfony/config": "",
"symfony/console": "",
"symfony/dependency-injection": "",
"symfony/finder": ""
"symfony/finder": "",
"symfony/filesystem": ""
},
"autoload": {
"psr-0": { "Symfony\\Component\\HttpKernel\\": "" }
Expand Down