Skip to content
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

Add support to resolve globstar when creating FileIterator #82

Closed
nicDamours opened this issue Aug 13, 2024 · 1 comment · Fixed by #83
Closed

Add support to resolve globstar when creating FileIterator #82

nicDamours opened this issue Aug 13, 2024 · 1 comment · Fixed by #83

Comments

@nicDamours
Copy link

Summary

Currently, glob are supported, but only using the default glob php function, which is quite limited. The default glob function doesn't allow for the usage of "globstar", which is a pretty common practice when targeting files using glob patterns.

Problem

Without globstar, we are limited to a single level of "unspecified" directory, when using filter in libraries such as phpunit. Take the following structure as example.

root_directory
├── directory_1
│   └── target_directory
└── directory_2
    └── directory_2_1
        └── directory_2_1_1
            └── target_directory

Using the current implementation of the Factory::resolveWildcards function, we can use the following pattern to target the directory target_directory right under directory_1:

root_directory/*/target_directory

However, it is not possible to target the target_directory under directory_2_2_1, since the glob pattern is only looking at the first level. It basically translates to root_directory/{any directory}/target_directory.

This means we need to pass multiple patterns to the filter in order to target all target_directory.

root_directory/*/target_directory
root_directory/*/*/*/target_directory
...

This solution is problematic when the directory structure changes a lot, which is pretty common when building complex projects.

Solution.

There are a few solutions, online, to extend the glob function behaviour to include globstar. One such solution can be found here..

I tried to implements this particular solution and it seams to be working fine in my local environment.

I will try to submit a PR that could fix the issue.

nicDamours pushed a commit to nicDamours/php-file-iterator that referenced this issue Aug 13, 2024
Added support for globstar when resolving paths in the FileIterator factory.
The extended glob function was adapted from funkjedi's implementation: https://gist.github.com/funkjedi/3feee27d873ae2297b8e2370a7082aad

Fixed facade test to include newly created fixture directories.
nicDamours pushed a commit to nicDamours/php-file-iterator that referenced this issue Aug 13, 2024
Removed unnecessary formatting changes.
nicDamours pushed a commit to nicDamours/php-file-iterator that referenced this issue Aug 14, 2024
Fixed static code analysis errors.
nicDamours pushed a commit to nicDamours/php-file-iterator that referenced this issue Aug 14, 2024
Added check to make sure $files isn't false when sorting the array. Otherwise, we return an empty array.
nicDamours pushed a commit to nicDamours/php-file-iterator that referenced this issue Aug 15, 2024
Ignore coverage for the last line of the globstar function, since it's only used if the glob function returns an error.
Added a test to validate the behaviour of the globstar function when passing non existant glob pattern.
Fixed code using php-cs-fixer.
Fixed error reported by phpstan.
@Wulfheart
Copy link

What a coincidence that I had the same problem today ^^

sebastianbergmann pushed a commit that referenced this issue Aug 27, 2024
Added support for globstar when resolving paths in the FileIterator factory.
The extended glob function was adapted from funkjedi's implementation: https://gist.github.com/funkjedi/3feee27d873ae2297b8e2370a7082aad

Fixed facade test to include newly created fixture directories.
sebastianbergmann pushed a commit that referenced this issue Aug 27, 2024
Removed unnecessary formatting changes.
sebastianbergmann pushed a commit that referenced this issue Aug 27, 2024
Fixed static code analysis errors.
sebastianbergmann pushed a commit that referenced this issue Aug 27, 2024
Added check to make sure $files isn't false when sorting the array. Otherwise, we return an empty array.
sebastianbergmann pushed a commit that referenced this issue Aug 27, 2024
Ignore coverage for the last line of the globstar function, since it's only used if the glob function returns an error.
Added a test to validate the behaviour of the globstar function when passing non existant glob pattern.
Fixed code using php-cs-fixer.
Fixed error reported by phpstan.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants