Make rootPath part of regex greedy

- Fixes #10977
This commit is contained in:
Arturs Vonda 2014-05-23 17:32:35 +03:00 committed by Fabien Potencier
parent 168174af08
commit 31da839447
3 changed files with 59 additions and 12 deletions

View File

@ -53,6 +53,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface
protected $bundleMap;
protected $container;
protected $rootDir;
protected $realRootDir;
protected $environment;
protected $debug;
protected $booted;
@ -731,24 +732,17 @@ abstract class Kernel implements KernelInterface, TerminableInterface
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;
@ -758,6 +752,33 @@ abstract class Kernel implements KernelInterface, TerminableInterface
}, $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.
*

View File

@ -56,4 +56,9 @@ class KernelForTest extends Kernel
{
$this->booted = (bool) $value;
}
public function setRealRootDir($dir)
{
$this->realRootDir = $dir;
}
}

View File

@ -852,6 +852,27 @@ EOF;
$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
*
* @return BundleInterface
*/
protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
{
$bundle = $this