[DI] tighten detection of local dirs to prevent false positives

This commit is contained in:
Nicolas Grekas 2020-06-14 14:27:25 +02:00
parent b30f4c1537
commit b746dd900c
3 changed files with 7 additions and 6 deletions

View File

@ -191,7 +191,7 @@ class PhpDumper extends Dumper
$regex = preg_quote(\DIRECTORY_SEPARATOR.$dir[$i], '#').$regex;
} while (0 < --$i);
$this->targetDirRegex = '#'.preg_quote($dir[0], '#').$regex.'#';
$this->targetDirRegex = '#(^|file://|[:;, \|\r\n])'.preg_quote($dir[0], '#').$regex.'#';
}
}
@ -1993,11 +1993,12 @@ EOF;
private function export($value)
{
if (null !== $this->targetDirRegex && \is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) {
$prefix = $matches[0][1] ? $this->doExport(substr($value, 0, $matches[0][1]), true).'.' : '';
$suffix = $matches[0][1] + \strlen($matches[0][0]);
$matches[0][1] += \strlen($matches[1][0]);
$prefix = $matches[0][1] ? $this->doExport(substr($value, 0, $matches[0][1]), true).'.' : '';
$suffix = isset($value[$suffix]) ? '.'.$this->doExport(substr($value, $suffix), true) : '';
$dirname = $this->asFiles ? '$this->containerDir' : '__DIR__';
$offset = 1 + $this->targetDirMaxMatches - \count($matches);
$offset = 2 + $this->targetDirMaxMatches - \count($matches);
if ($this->asFiles || 0 < $offset) {
$dirname = sprintf('$this->targetDirs[%d]', $offset);

View File

@ -98,7 +98,7 @@ class PhpDumperTest extends TestCase
$container = new ContainerBuilder();
$container->setDefinition('test', $definition);
$container->setParameter('foo', 'wiz'.\dirname(__DIR__));
$container->setParameter('foo', 'file://'.\dirname(__DIR__));
$container->setParameter('bar', __DIR__);
$container->setParameter('baz', '%bar%/PhpDumperTest.php');
$container->setParameter('buz', \dirname(\dirname(__DIR__)));

View File

@ -67,7 +67,7 @@ class ProjectServiceContainer extends Container
*/
protected function getTestService()
{
return $this->services['test'] = new \stdClass(('wiz'.$this->targetDirs[1]), [('wiz'.$this->targetDirs[1]) => ($this->targetDirs[2].'/')]);
return $this->services['test'] = new \stdClass(('file://'.$this->targetDirs[1]), [('file://'.$this->targetDirs[1]) => ($this->targetDirs[2].'/')]);
}
public function getParameter($name)
@ -131,7 +131,7 @@ class ProjectServiceContainer extends Container
private function getDynamicParameter($name)
{
switch ($name) {
case 'foo': $value = ('wiz'.$this->targetDirs[1]); break;
case 'foo': $value = ('file://'.$this->targetDirs[1]); break;
case 'buz': $value = $this->targetDirs[2]; break;
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
}