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

Skip to content

Make rootPath part of regex greedy #10979

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

Closed
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
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 = false;
Expand Down Expand Up @@ -729,24 +730,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 @@ -756,6 +750,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
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public function isBooted()
{
return $this->booted;
}

public function setRealRootDir($dir)
{
$this->realRootDir = $dir;
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,22 @@ 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
*
Expand Down