Skip to content
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
17 changes: 13 additions & 4 deletions src/Symfony/Component/Config/Resource/GlobResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public function getIterator()
$prefix = str_replace('\\', '/', $this->prefix);
$paths = null;

if (!str_starts_with($this->prefix, 'phar://') && !str_contains($this->pattern, '/**/')) {
if ('' === $this->pattern && is_file($prefix)) {
$paths = [$this->prefix];
} elseif (!str_starts_with($this->prefix, 'phar://') && !str_contains($this->pattern, '/**/')) {
if ($this->globBrace || !str_contains($this->pattern, '{')) {
$paths = glob($this->prefix.$this->pattern, \GLOB_NOSORT | $this->globBrace);
} elseif (!str_contains($this->pattern, '\\') || !preg_match('/\\\\[,{}]/', $this->pattern)) {
Expand Down Expand Up @@ -172,14 +174,21 @@ function (\SplFileInfo $file, $path) {
throw new \LogicException(sprintf('Extended glob pattern "%s" cannot be used as the Finder component is not installed.', $this->pattern));
}

if (is_file($prefix = $this->prefix)) {
$prefix = \dirname($prefix);
$pattern = basename($prefix).$this->pattern;
} else {
$pattern = $this->pattern;
}

$finder = new Finder();
$regex = Glob::toRegex($this->pattern);
$regex = Glob::toRegex($pattern);
if ($this->recursive) {
$regex = substr_replace($regex, '(/|$)', -2, 1);
}

$prefixLen = \strlen($this->prefix);
foreach ($finder->followLinks()->sortByName()->in($this->prefix) as $path => $info) {
$prefixLen = \strlen($prefix);
foreach ($finder->followLinks()->sortByName()->in($prefix) as $path => $info) {
$normalizedPath = str_replace('\\', '/', $path);
if (!preg_match($regex, substr($normalizedPath, $prefixLen)) || !$info->isFile()) {
continue;
Expand Down
Binary file not shown.
25 changes: 25 additions & 0 deletions src/Symfony/Component/Config/Tests/Resource/GlobResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,29 @@ public function testSerializeUnserialize()

$this->assertEquals($p->getValue($resource), $p->getValue($newResource));
}

public function testPhar()
{
$s = \DIRECTORY_SEPARATOR;
$cwd = getcwd();
chdir(\dirname(__DIR__).'/Fixtures');
try {
$resource = new GlobResource('phar://some.phar', '*', true);
$files = array_keys(iterator_to_array($resource));
$this->assertSame(["phar://some.phar{$s}ProjectWithXsdExtensionInPhar.php", "phar://some.phar{$s}schema{$s}project-1.0.xsd"], $files);

$resource = new GlobResource("phar://some.phar{$s}ProjectWithXsdExtensionInPhar.php", '', true);
$files = array_keys(iterator_to_array($resource));
$this->assertSame(["phar://some.phar{$s}ProjectWithXsdExtensionInPhar.php"], $files);
} finally {
chdir($cwd);
}
}

public function testFilePrefix()
{
$resource = new GlobResource(__FILE__, '/**/', true);
$files = array_keys(iterator_to_array($resource));
$this->assertSame([], $files);
}
}