Skip to content

Commit

Permalink
Add allow exclude dir when composer class finder is used
Browse files Browse the repository at this point in the history
  • Loading branch information
jlabno committed Jul 20, 2022
1 parent 0c45bab commit f7447d5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion src/Finder/ComposerClassFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 1 addition & 6 deletions src/Finder/DirectoryIteratorClassFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DirectoryIteratorClassFinder implements ClassFinder
{
private ?string $excludeRegex = null;
use ExcludeTrait;

public function getClassesNames(string $basePath): array
{
Expand All @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/Finder/ExcludeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace AnnotationsScanner\Finder;

trait ExcludeTrait
{
private ?string $excludeRegex = null;

public function setExcludeRegex(string $regex): void
{
$this->excludeRegex = $regex;
}
}
8 changes: 7 additions & 1 deletion src/Scanner/AbstractAnnotationScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 0 additions & 11 deletions src/Scanner/ExcludeDirsNotAvailableException.php

This file was deleted.

4 changes: 0 additions & 4 deletions src/Scanner/ScannerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public function build(): AnnotationScanner
throw BasePathMustBeSetException::create();
}

if ($this->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) {
Expand Down

0 comments on commit f7447d5

Please sign in to comment.