Skip to content

Commit

Permalink
Make rootPath part of regex greedy
Browse files Browse the repository at this point in the history
- Fixes #10977
  • Loading branch information
artursvonda authored and fabpot committed May 26, 2014
1 parent 168174a commit 31da839
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -53,6 +53,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface
protected $bundleMap;
protected $container;
protected $rootDir;
protected $realRootDir;
protected $environment;
protected $debug;
protected $booted;
Expand Down Expand Up @@ -731,24 +732,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;
Expand All @@ -758,6 +752,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.
*
Expand Down
Expand Up @@ -56,4 +56,9 @@ public function setIsBooted($value)
{
$this->booted = (bool) $value;
}

public function setRealRootDir($dir)
{
$this->realRootDir = $dir;
}
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Expand Up @@ -852,6 +852,27 @@ 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
*
* @return BundleInterface
*/
protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
{
$bundle = $this
Expand Down

0 comments on commit 31da839

Please sign in to comment.