Skip to content

[Config] fix perf of glob discovery when GLOB_BRACE is not available #35015

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

Merged
merged 1 commit into from
Dec 18, 2019
Merged
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
47 changes: 45 additions & 2 deletions src/Symfony/Component/Config/Resource/GlobResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class GlobResource implements \IteratorAggregate, SelfCheckingResourceInterface
private $hash;
private $forExclusion;
private $excludedPrefixes;
private $globBrace;

/**
* @param string $prefix A directory prefix
Expand All @@ -47,6 +48,7 @@ public function __construct(string $prefix, string $pattern, bool $recursive, bo
$this->recursive = $recursive;
$this->forExclusion = $forExclusion;
$this->excludedPrefixes = $excludedPrefixes;
$this->globBrace = \defined('GLOB_BRACE') ? GLOB_BRACE : 0;

if (false === $this->prefix) {
throw new \InvalidArgumentException(sprintf('The path "%s" does not exist.', $prefix));
Expand Down Expand Up @@ -101,9 +103,20 @@ public function getIterator()
return;
}
$prefix = str_replace('\\', '/', $this->prefix);
$paths = null;

if (0 !== strpos($this->prefix, 'phar://') && false === strpos($this->pattern, '/**/')) {
if ($this->globBrace || false === strpos($this->pattern, '{')) {
$paths = glob($this->prefix.$this->pattern, GLOB_NOSORT | $this->globBrace);
} elseif (false === strpos($this->pattern, '\\') || !preg_match('/\\\\[,{}]/', $this->pattern)) {
foreach ($this->expandGlob($this->pattern) as $p) {
$paths[] = glob($this->prefix.$p, GLOB_NOSORT);
}
$paths = array_merge(...$paths);
}
}

if (0 !== strpos($this->prefix, 'phar://') && false === strpos($this->pattern, '/**/') && (\defined('GLOB_BRACE') || false === strpos($this->pattern, '{'))) {
$paths = glob($this->prefix.$this->pattern, GLOB_NOSORT | (\defined('GLOB_BRACE') ? GLOB_BRACE : 0));
if (null !== $paths) {
sort($paths);
foreach ($paths as $path) {
if ($this->excludedPrefixes) {
Expand Down Expand Up @@ -187,4 +200,34 @@ private function computeHash(): string

return hash_final($hash);
}

private function expandGlob(string $pattern): array
{
$segments = preg_split('/\{([^{}]*+)\}/', $pattern, -1, PREG_SPLIT_DELIM_CAPTURE);
$paths = [$segments[0]];
$patterns = [];

for ($i = 1; $i < \count($segments); $i += 2) {
$patterns = [];

foreach (explode(',', $segments[$i]) as $s) {
foreach ($paths as $p) {
$patterns[] = $p.$s.$segments[1 + $i];
}
}

$paths = $patterns;
}

$j = 0;
foreach ($patterns as $i => $p) {
if (false !== strpos($p, '{')) {
$p = $this->expandGlob($p);
array_splice($paths, $i + $j, 1, $p);
$j += \count($p) - 1;
}
}

return $paths;
}
}
29 changes: 29 additions & 0 deletions src/Symfony/Component/Config/Tests/Resource/GlobResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,33 @@ public function testIsFreshRecursiveDetectsNewFile()
touch($dir.'/Resource/TmpGlob');
$this->assertFalse($resource->isFresh(0));
}

public function testBraceFallback()
{
$dir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
$resource = new GlobResource($dir, '/*{/*/*.txt,.x{m,n}l}', true);

$p = new \ReflectionProperty($resource, 'globBrace');
$p->setAccessible(true);
$p->setValue($resource, 0);

$expected = [
$dir.'/Exclude/ExcludeToo/AnotheExcludedFile.txt',
$dir.'/foo.xml',
];

$this->assertSame($expected, array_keys(iterator_to_array($resource)));
}

public function testUnbalancedBraceFallback()
{
$dir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
$resource = new GlobResource($dir, '/*{/*/*.txt,.x{m,nl}', true);

$p = new \ReflectionProperty($resource, 'globBrace');
$p->setAccessible(true);
$p->setValue($resource, 0);

$this->assertSame([], array_keys(iterator_to_array($resource)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this actually what happens with glob, or does it report an error ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no error, just no matches

}
}