From f7447d530e954e52684e474b7dcfb7a0d515980e Mon Sep 17 00:00:00 2001 From: jlabno Date: Wed, 20 Jul 2022 14:15:15 +0200 Subject: [PATCH] Add allow exclude dir when composer class finder is used --- composer.json | 2 +- src/Finder/ComposerClassFinder.php | 8 +++++++- src/Finder/DirectoryIteratorClassFinder.php | 7 +------ src/Finder/ExcludeTrait.php | 15 +++++++++++++++ src/Scanner/AbstractAnnotationScanner.php | 8 +++++++- src/Scanner/ExcludeDirsNotAvailableException.php | 11 ----------- src/Scanner/ScannerBuilder.php | 4 ---- 7 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 src/Finder/ExcludeTrait.php delete mode 100644 src/Scanner/ExcludeDirsNotAvailableException.php diff --git a/composer.json b/composer.json index 82a94dc..d19ead3 100755 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ } ], "require": { - "php": "^7.4||^8.01", + "php": "^7.4||^8.0", "doctrine/annotations": "^1.13", "psr/simple-cache": "^1.0", "symfony/cache": "^5.4", diff --git a/src/Finder/ComposerClassFinder.php b/src/Finder/ComposerClassFinder.php index fd52511..87dc759 100644 --- a/src/Finder/ComposerClassFinder.php +++ b/src/Finder/ComposerClassFinder.php @@ -8,8 +8,14 @@ class ComposerClassFinder implements ClassFinder { + use ExcludeTrait; + public function getClassesNames(string $basePath): array { - return array_keys(ClassMapGenerator::createMap($basePath)); + $map = $this->excludeRegex + ? ClassMapGenerator::createMap($basePath, $this->excludeRegex) + : ClassMapGenerator::createMap($basePath); + + return array_keys($map); } } diff --git a/src/Finder/DirectoryIteratorClassFinder.php b/src/Finder/DirectoryIteratorClassFinder.php index 7bd9785..a48bc93 100644 --- a/src/Finder/DirectoryIteratorClassFinder.php +++ b/src/Finder/DirectoryIteratorClassFinder.php @@ -10,7 +10,7 @@ class DirectoryIteratorClassFinder implements ClassFinder { - private ?string $excludeRegex = null; + use ExcludeTrait; public function getClassesNames(string $basePath): array { @@ -36,11 +36,6 @@ public function getClassesNames(string $basePath): array return $classesNames; } - public function setExcludeRegex(string $regex): void - { - $this->excludeRegex = $regex; - } - private function getClassNameFromFile(string $fileName): ?string { return ClassNameFromPathGenerator::getFullClassNameFromFile($fileName); diff --git a/src/Finder/ExcludeTrait.php b/src/Finder/ExcludeTrait.php new file mode 100644 index 0000000..1c09513 --- /dev/null +++ b/src/Finder/ExcludeTrait.php @@ -0,0 +1,15 @@ +excludeRegex = $regex; + } +} diff --git a/src/Scanner/AbstractAnnotationScanner.php b/src/Scanner/AbstractAnnotationScanner.php index f92d321..a84666c 100644 --- a/src/Scanner/AbstractAnnotationScanner.php +++ b/src/Scanner/AbstractAnnotationScanner.php @@ -24,6 +24,12 @@ protected function getClassFinder(string $scanMethod): ClassFinder return $scanner; } - return new ComposerClassFinder(); + $scanner = new ComposerClassFinder(); + + if ($this->excludeDirectoriesRegex) { + $scanner->setExcludeRegex($this->excludeDirectoriesRegex); + } + + return $scanner; } } diff --git a/src/Scanner/ExcludeDirsNotAvailableException.php b/src/Scanner/ExcludeDirsNotAvailableException.php deleted file mode 100644 index 95f5823..0000000 --- a/src/Scanner/ExcludeDirsNotAvailableException.php +++ /dev/null @@ -1,11 +0,0 @@ -excludeDirsRegex && $this->scanMethod == ScanMethod::COMPOSER) { - throw new ExcludeDirsNotAvailableException('Exclude function available only when directory scan method is in use'); - } - $collection = new AnnotationsToSearchCollection(new ArrayIterator($this->annotationsToSearchFor)); if ($this->reader) {