Get package classmap without vendor
Gashmob opened this issue · comments
In a project I'm trying to get classmap of my package without all the classes (many) present in vendor directory. For that I use this code:
$package_path = '/some/path/vendor/composer/../../'
$generator = new ClassMapGenerator();
$generator->scanPaths($package_path, "/$package_path/vendor/**/");
$map = $generator->getClassMap();
Second argument of ClassMapGenerator::scanPath
is a string used as a regex (it's why i've added ** at the end). But when I run this snippet I got this error: Composer\Pcre\PcreException: preg_match(): failed executing "//some/path/vendor/composer/../..//vendor/**/": Internal error
.
I don't know why preg_match don't like this regex, so I'm blocked. Anyone here have an idea of how I can fix that ?
Because **
is not a valid regex. You should use "{^$package_path/vendor/.*}"
that should work I think.
Yes, indeed, regex not shell path. So thanks it works!
Note: It takes some time to do the scan (300ms) as it gets all files and then filter with the regex. Symfony class Finder
has a method exclude(string|array $dirs)
, what do you think of pre-filtering file list?
Yeah I am aware of that, not sure anymore why I did not use it, I think there was a reason but maybe it's just that we have a regex to work with and it's not easy to convert that to a list of dirs.
There is also method notPath
that works with 'pattern'
If you allow it, I can create a pull-request to change signature to:
public function scanPaths(
$path,
?string $excluded = null,
string $autoloadType = 'classmap',
?string $namespace = null,
+ ?string|array $excludedDirs = []
): void
And use $excludedDirs
with Finder
. What do you think?
You can send a PR and I'll evaluate it with the Composer codebase as well see how it all interacts, but it'll be a while probably as I got some time off coming up.