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

Skip to content

[Finder] Incorrect regexp from Glob::toRegex with windows paths #23307

Closed
@shude

Description

@shude
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.3
OS Windows

How to reproduce:
We have a some directory structue that looks like:

etc/
|--dev/
----| dev_conf.yaml
|-- conf.yaml

Try to load all yaml files from etc/:

 $loader = new GlobFileLoader(new FileLocator());

 $collections = $loader->load("path_to/etc/**/*.{php,xml,yaml,yml}","glob");
 var_dump($collections);

In this case we have a trouble in Windows Paths when using /**/ wildcard.
Loader loads nothing. Let's take a look at Config/Resources/GlobResource::getIterator()

$regex = Glob::toRegex($this->pattern);
// ...
if (preg_match($regex, substr($path, $prefixLen)) && $info->isFile()) {
        yield $path => $info;
}

We have a #^(?=[^\.])/(?:(?=[^\.])[^/]++/)*(?=[^\.])[^/]*\.(php|xml|yaml|yml)$# regex
for '/**/*.{php,xml,yaml,yml}' pattern.

But $path in Windows contains a backslashes instead of slashes and preg_match always not matching.

To fix this we can modify Glob::toRegex() to something like this:

//Line 73
$car = (DIRECTORY_SEPARATOR === '/') ? '/(?:'.$car.')*' : '\\\\(?:'.$car.')*';

Another way is to use str_replace before preg_match in Config/Resources/GlobResource::getIterator():

$regex = Glob::toRegex($this->pattern);
// ...
$path = str_replace("\\","/",$path);
if (preg_match($regex, substr($path, $prefixLen)) && $info->isFile()) {
        yield $path => $info;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions