Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Config] Fix looking for single files in phars with GlobResource #46364

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
May 17, 2022
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
[Config] Fix looking for single files in phars with GlobResource
  • Loading branch information
nicolas-grekas committed May 17, 2022
commit d4ea07272644d89a04149a10d0b93d73aefcfd0d
17 changes: 13 additions & 4 deletions 17 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 25 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);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.