alekitto / class-finder

Utility classes to help discover other classes/namespaces
https://alekitto.github.io/class-finder/
MIT License
27 stars 6 forks source link

Add filter for RecursiveIterator to not require all files in the selected path, but only with specific pattern in filename #9

Closed alleknalle closed 9 months ago

alleknalle commented 10 months ago

Hi there!

I'm currently working on some WordPress plugins/themes and I'm using this library to easily require and find certain classes. I was using ComposerFinder, but since I don't always use Composer (for my themes), I switched to RecursiveFinder. When using this I noticed that it was requiring some 'non-class' files as well, causing some output. Since WordPress has their own coding standards, this can be easily fixed by only requiring files that start with class. First I tried to fix this by using filter on the Finder, but when the filter is called, the files are already included. So I checked where the files are included and that is in RecursiveIterator. Since there was no way to filter files here (except for creating a PR), I created my own ClassIterator (and Finder to make sure my own iterator was called) where I check if the file starts with class. This works perfectly and I thought it would be nice to add this to the package as well. I will try to create a PR somewhere soon, this issue is more so you can maybe think of this as well and as a reminder to myself ;-)

alekitto commented 10 months ago

Hi, Thanks for sharing your use case here. I think that it will be a great addition to the project!

A filename-based filter method could be added to all the iterators (and finders), allowing to use them in codebases with different code styles.

Feel free to open a PR with your additions. We could try to generalise them after.