Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 93cbf96

Browse files
[Config] Fix looking for single files in phars with GlobResource
1 parent 265d58a commit 93cbf96

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ public function getIterator()
111111
$prefix = str_replace('\\', '/', $this->prefix);
112112
$paths = null;
113113

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

177+
if (is_file($prefix = $this->prefix)) {
178+
$prefix = \dirname($prefix);
179+
$pattern = basename($prefix).$this->pattern;
180+
} else {
181+
$pattern = $this->pattern;
182+
}
183+
175184
$finder = new Finder();
176-
$regex = Glob::toRegex($this->pattern);
185+
$regex = Glob::toRegex($pattern);
177186
if ($this->recursive) {
178187
$regex = substr_replace($regex, '(/|$)', -2, 1);
179188
}
180189

181-
$prefixLen = \strlen($this->prefix);
182-
foreach ($finder->followLinks()->sortByName()->in($this->prefix) as $path => $info) {
190+
$prefixLen = \strlen($prefix);
191+
foreach ($finder->followLinks()->sortByName()->in($prefix) as $path => $info) {
183192
$normalizedPath = str_replace('\\', '/', $path);
184193
if (!preg_match($regex, substr($normalizedPath, $prefixLen)) || !$info->isFile()) {
185194
continue;
Binary file not shown.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,29 @@ public function testSerializeUnserialize()
208208

209209
$this->assertEquals($p->getValue($resource), $p->getValue($newResource));
210210
}
211+
212+
public function testPhar()
213+
{
214+
$s = \DIRECTORY_SEPARATOR
215+
$cwd = getcwd();
216+
chdir(\dirname(__DIR__).'/Fixtures');
217+
try {
218+
$resource = new GlobResource('phar://some.phar', '*', true);
219+
$files = array_keys(iterator_to_array($resource));
220+
$this->assertSame(["phar://some.phar{$s}ProjectWithXsdExtensionInPhar.php", "phar://some.phar{$s}schema{$s}project-1.0.xsd"], $files);
221+
222+
$resource = new GlobResource('phar://some.phar/ProjectWithXsdExtensionInPhar.php', '', true);
223+
$files = array_keys(iterator_to_array($resource));
224+
$this->assertSame(["phar://some.phar{$s}ProjectWithXsdExtensionInPhar.php"], $files);
225+
} finally {
226+
chdir($cwd);
227+
}
228+
}
229+
230+
public function testFilePrefix()
231+
{
232+
$resource = new GlobResource(__FILE__, '/**/', true);
233+
$files = array_keys(iterator_to_array($resource));
234+
$this->assertSame([], $files);
235+
}
211236
}

0 commit comments

Comments
 (0)