diff --git a/src/ClassMapGenerator.php b/src/ClassMapGenerator.php index 53f9a42..4e63236 100644 --- a/src/ClassMapGenerator.php +++ b/src/ClassMapGenerator.php @@ -97,10 +97,11 @@ public function getClassMap(): ClassMap * @param non-empty-string|null $excluded Regex that matches file paths to be excluded from the classmap * @param 'classmap'|'psr-0'|'psr-4' $autoloadType Optional autoload standard to use mapping rules with the namespace instead of purely doing a classmap * @param string|null $namespace Optional namespace prefix to filter by, only for psr-0/psr-4 autoloading + * @param array $excludedDirs Optional dirs to exclude from search relative to $path * * @throws \RuntimeException When the path is neither an existing file nor directory */ - public function scanPaths($path, ?string $excluded = null, string $autoloadType = 'classmap', ?string $namespace = null): void + public function scanPaths($path, ?string $excluded = null, string $autoloadType = 'classmap', ?string $namespace = null, array $excludedDirs = []): void { if (!in_array($autoloadType, ['psr-0', 'psr-4', 'classmap'], true)) { throw new \InvalidArgumentException('$autoloadType must be one of: "psr-0", "psr-4" or "classmap"'); @@ -124,7 +125,8 @@ public function scanPaths($path, ?string $excluded = null, string $autoloadType ->files() ->followLinks() ->name('/\.(?:'.implode('|', array_map('preg_quote', $this->extensions)).')$/') - ->in($path); + ->in($path) + ->exclude($excludedDirs); } else { throw new \RuntimeException( 'Could not scan for classes inside "'.$path.'" which does not appear to be a file nor a folder' diff --git a/tests/ClassMapGeneratorTest.php b/tests/ClassMapGeneratorTest.php index 393a69e..d671380 100644 --- a/tests/ClassMapGeneratorTest.php +++ b/tests/ClassMapGeneratorTest.php @@ -240,6 +240,18 @@ public function testCreateMapDoesNotHitRegexBacktraceLimit(): void self::assertEqualsNormalized($expected, $result); } + public function testCreateMapWithDirectoryExcluded(): void + { + $expected = array( + 'PrefixCollision_A_B_Bar' => realpath(__DIR__) . '/Fixtures/beta/PrefixCollision/A/B/Bar.php', + 'PrefixCollision_A_B_Foo' => realpath(__DIR__) . '/Fixtures/beta/PrefixCollision/A/B/Foo.php', + ); + + $this->generator->scanPaths(realpath(__DIR__) . '/Fixtures/beta', null, 'classmap', null, ['NamespaceCollision']); + $result = $this->generator->getClassMap(); + self::assertEqualsNormalized($expected, $result->getMap()); + } + /** * @param array $expected * @param array $actual