Skip to content

Commit 31da839

Browse files
artursvondafabpot
authored andcommitted
Make rootPath part of regex greedy
- Fixes #10977
1 parent 168174a commit 31da839

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface
5353
protected $bundleMap;
5454
protected $container;
5555
protected $rootDir;
56+
protected $realRootDir;
5657
protected $environment;
5758
protected $debug;
5859
protected $booted;
@@ -731,24 +732,17 @@ private function removeAbsolutePathsFromContainer($content)
731732
return $content;
732733
}
733734

734-
// find the "real" root dir (by finding the composer.json file)
735-
$rootDir = $this->getRootDir();
736-
$previous = $rootDir;
737-
while (!file_exists($rootDir.'/composer.json')) {
738-
if ($previous === $rootDir = realpath($rootDir.'/..')) {
739-
// unable to detect the project root, give up
740-
return $content;
741-
}
742-
743-
$previous = $rootDir;
735+
$rootDir = $this->getRealRootDir();
736+
if (!$rootDir) {
737+
return $content;
744738
}
745739

746740
$rootDir = rtrim($rootDir, '/');
747741
$cacheDir = $this->getCacheDir();
748742
$filesystem = new Filesystem();
749743

750-
return preg_replace_callback("{'([^']*)(".preg_quote($rootDir)."[^']*)'}", function ($match) use ($filesystem, $cacheDir) {
751-
$prefix = isset($match[1]) && $match[1] ? "'$match[1]'.__DIR__" : "__DIR__";
744+
return preg_replace_callback("{'([^']*?)(".preg_quote($rootDir)."[^']*)'}", function ($match) use ($filesystem, $cacheDir) {
745+
$prefix = !empty($match[1]) ? "'$match[1]'.__DIR__" : "__DIR__";
752746

753747
if ('.' === $relativePath = rtrim($filesystem->makePathRelative($match[2], $cacheDir), '/')) {
754748
return $prefix;
@@ -758,6 +752,33 @@ private function removeAbsolutePathsFromContainer($content)
758752
}, $content);
759753
}
760754

755+
/**
756+
* Find the "real" root dir (by finding the composer.json file)
757+
*
758+
* @return null|string
759+
*/
760+
private function getRealRootDir()
761+
{
762+
if (null !== $this->realRootDir) {
763+
return $this->realRootDir;
764+
}
765+
766+
$rootDir = $this->getRootDir();
767+
$previous = $rootDir;
768+
while (!file_exists($rootDir.'/composer.json')) {
769+
if ($previous === $rootDir = realpath($rootDir.'/..')) {
770+
// unable to detect the project root, give up
771+
return $this->realRootDir = false;
772+
}
773+
774+
$previous = $rootDir;
775+
}
776+
777+
$this->realRootDir = $rootDir;
778+
779+
return $this->realRootDir;
780+
}
781+
761782
/**
762783
* Returns a loader for the container.
763784
*

src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public function setIsBooted($value)
5656
{
5757
$this->booted = (bool) $value;
5858
}
59+
60+
public function setRealRootDir($dir)
61+
{
62+
$this->realRootDir = $dir;
63+
}
5964
}

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,27 @@ public function testRemoveAbsolutePathsFromContainerGiveUpWhenComposerJsonPathNo
852852
$this->assertEquals($newContent, $content);
853853
}
854854

855+
public function testRemoveAbsolutePathsFromContainerWithSpecialCase()
856+
{
857+
$kernel = new KernelForTest('dev', true);
858+
$kernel->setRootDir('/app/app');
859+
$kernel->setRealRootDir('/app');
860+
$symfonyRootDir = __DIR__.'/Fixtures/DumpedContainers/app';
861+
862+
$content = file_get_contents($symfonyRootDir.'/cache/dev/withAbsolutePaths.php');
863+
$content = str_replace('ROOT_DIR', '/app', $content);
864+
865+
$m = new \ReflectionMethod($kernel, 'removeAbsolutePathsFromContainer');
866+
$m->setAccessible(true);
867+
$content = $m->invoke($kernel, $content);
868+
$this->assertEquals(file_get_contents($symfonyRootDir.'/cache/dev/withoutAbsolutePaths.php'), $content);
869+
}
870+
871+
/**
872+
* Returns a mock for the BundleInterface
873+
*
874+
* @return BundleInterface
875+
*/
855876
protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
856877
{
857878
$bundle = $this

0 commit comments

Comments
 (0)