8000 [Config] improve perf of glob discovery when GLOB_BRACE is not available · symfony/symfony@b50c36a · GitHub
[go: up one dir, main page]

Skip to content

Commit b50c36a

Browse files
[Config] improve perf of glob discovery when GLOB_BRACE is not available
1 parent 32b09a8 commit b50c36a

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/Symfony/Component/Config/Resource/GlobResource.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class GlobResource implements \IteratorAggregate, SelfCheckingResourceInterface
3131
private $hash;
3232
private $forExclusion;
3333
private $excludedPrefixes;
34+
private $globBrace;
3435

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

5153
if (false === $this->prefix) {
5254
throw new \InvalidArgumentException(sprintf('The path "%s" does not exist.', $prefix));
@@ -101,9 +103,17 @@ public function getIterator()
101103
return;
102104
}
103105
$prefix = str_replace('\\', '/', $this->prefix);
106+
$paths = null;
104107

105-
if (0 !== strpos($this->prefix, 'phar://') && false === strpos($this->pattern, '/**/') && (\defined('GLOB_BRACE') || false === strpos($this->pattern, '{'))) {
106-
$paths = glob($this->prefix.$this->pattern, GLOB_NOSORT | (\defined('GLOB_BRACE') ? GLOB_BRACE : 0));
108+
if (0 !== strpos($this->prefix, 'phar://') && false === strpos($this->pattern, '/**/')) {
109+
if ($this->globBrace || false === strpos($this->pattern, '{')) {
110+
$paths = glob($this->prefix.$this->pattern, GLOB_NOSORT | $this->globBrace);
111+
} elseif (false === strpos($this->pattern, '\\')) {
112+
$paths = $this->fallbackGlob();
113+
}
114+
}
115+
116+
if (null !== $paths) {
107117
sort($paths);
108118
foreach ($paths as $path) {
109119
if ($this->excludedPrefixes) {
@@ -187,4 +197,32 @@ private function computeHash(): string
187197

188198
return hash_final($hash);
189199
}
200+
201+
private function fallbackGlob(): ?array
202+
{
203+
$segments = preg_split('/\{([^{}]*+)\}/', $this->pattern, -1, PREG_SPLIT_DELIM_CAPTURE);
204+
$paths = [$segments[0]];
205+
206+
for ($i = 1; $i < \count($segments); $i += 2) {
207+
$patterns = [];
208+
209+
foreach (explode(',', $segments[$i]) as $s) {
210+
foreach ($paths as $p) {
211+
$patterns[] = $p.$s.$segments[1 + $i];
212+
}
213+
}
214+
215+
$paths = $patterns;
216+
}
217+
218+
foreach ($paths as $i => $p) {
219+
if (false !== strpos($p, '{') || false !== strpos($p, '}')) {
220+
return null;
221+
}
222+
223+
$paths[$i] = glob($this->prefix.$p, GLOB_NOSORT);
224+
}
225+
226+
return array_merge(...$paths);
227+
}
190228
}

src/Symfony/Component/Config/Tests/Resource/GlobResourceTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,21 @@ public function testIsFreshRecursiveDetectsNewFile()
165165
touch($dir.'/Resource/TmpGlob');
166166
$this->assertFalse($resource->isFresh(0));
167167
}
168+
169+
public function testBraceFallback()
170+
{
171+
$dir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
172+
$resource = new GlobResource($dir, '/*{/*/*.txt,.xml}', true);
173+
174+
$p = new \ReflectionProperty($resource, 'globBrace');
175+
$p->setAccessible(true);
176+
$p->setValue($resource, 0);
177+
178+
$expected = [
179+
$dir.'/Exclude/ExcludeToo/AnotheExcludedFile.txt',
180+
$dir.'/foo.xml',
181+
];
182+
183+
$this->assertSame($expected, array_keys(iterator_to_array($resource)));
184+
}
168185
}

0 commit comments

Comments
 (0)
0