diff --git a/src/Symfony/Component/Config/Resource/GlobResource.php b/src/Symfony/Component/Config/Resource/GlobResource.php index 3657f6dcb9500..57e528462cc90 100644 --- a/src/Symfony/Component/Config/Resource/GlobResource.php +++ b/src/Symfony/Component/Config/Resource/GlobResource.php @@ -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)) { @@ -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; diff --git a/src/Symfony/Component/Config/Tests/Fixtures/some.phar b/src/Symfony/Component/Config/Tests/Fixtures/some.phar new file mode 100644 index 0000000000000..93d4e87c0b89b Binary files /dev/null and b/src/Symfony/Component/Config/Tests/Fixtures/some.phar differ diff --git a/src/Symfony/Component/Config/Tests/Resource/GlobResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/GlobResourceTest.php index fd317eb55c067..96953f3e32b8c 100644 --- a/src/Symfony/Component/Config/Tests/Resource/GlobResourceTest.php +++ b/src/Symfony/Component/Config/Tests/Resource/GlobResourceTest.php @@ -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); + } }