brenoroosevelt /
flex-fqcn-finder
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace FlexFqcnFinder; |
||
| 5 | |||
| 6 | use FlexFqcnFinder\Filter\FqcnSpecification; |
||
| 7 | use FlexFqcnFinder\Finder\ComposerClassMap; |
||
| 8 | use FlexFqcnFinder\Finder\Decorator\CachedFqcnFinder; |
||
| 9 | use FlexFqcnFinder\Finder\Decorator\FilteringFqcnFinder; |
||
| 10 | use FlexFqcnFinder\Finder\FqcnFinder; |
||
| 11 | use FlexFqcnFinder\Finder\GetDeclaredClasses; |
||
| 12 | use FlexFqcnFinder\Finder\GetDeclaredInterfaces; |
||
| 13 | use FlexFqcnFinder\Finder\GetDeclaredTraits; |
||
| 14 | use FlexFqcnFinder\Repository\FilesFromDir; |
||
| 15 | use Psr\SimpleCache\CacheInterface; |
||
| 16 | |||
| 17 | class Fqcn implements FqcnFinderInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var FqcnSpecification|null |
||
| 21 | */ |
||
| 22 | protected $filter; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var CacheInterface |
||
| 26 | */ |
||
| 27 | private $cache; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $cacheKey; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var FqcnFinderInterface |
||
| 36 | */ |
||
| 37 | protected $finders = []; |
||
| 38 | |||
| 39 | public static function new(): Fqcn |
||
| 40 | { |
||
| 41 | return new self(); |
||
| 42 | } |
||
| 43 | |||
| 44 | public static function fromDir(string $dir, bool $recursive = true): Fqcn |
||
| 45 | { |
||
| 46 | return (new self())->addDirectory($dir, $recursive); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function addDirectory(string $dir, bool $recursive = true): Fqcn |
||
| 50 | { |
||
| 51 | $this->finders[] = new FqcnFinder(new FilesFromDir($dir, $recursive)); |
||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | public function includeDeclaredClasses(): Fqcn |
||
| 56 | { |
||
| 57 | $this->finders[] = new GetDeclaredClasses(); |
||
| 58 | return $this; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function includeDeclaredTraits(): Fqcn |
||
| 62 | { |
||
| 63 | $this->finders[] = new GetDeclaredTraits(); |
||
| 64 | return $this; |
||
| 65 | } |
||
| 66 | |||
| 67 | public function includeDeclaredInterfaces(): Fqcn |
||
| 68 | { |
||
| 69 | $this->finders[] = new GetDeclaredInterfaces(); |
||
| 70 | return $this; |
||
| 71 | } |
||
| 72 | |||
| 73 | public function includeComposerClassMap(): Fqcn |
||
| 74 | { |
||
| 75 | $this->finders[] = new ComposerClassMap(); |
||
| 76 | return $this; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function withFilter(FqcnSpecification $filter): Fqcn |
||
| 80 | { |
||
| 81 | $this->filter = $filter; |
||
| 82 | return $this; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function withCache(CacheInterface $cache, string $cacheKey): Fqcn |
||
| 86 | { |
||
| 87 | $this->cache = $cache; |
||
| 88 | $this->cacheKey = $cacheKey; |
||
| 89 | return $this; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function compose(FqcnFinderInterface ...$finders): Fqcn |
||
| 93 | { |
||
| 94 | foreach ($finders as $finder) { |
||
| 95 | $this->finders[] = $finder; |
||
| 96 | } |
||
| 97 | |||
| 98 | return $this; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | public function find(): array |
||
| 105 | { |
||
| 106 | $finder = new FqcnFinderComposite(...$this->finders); |
||
| 107 | |||
| 108 | if ($this->filter instanceof FqcnSpecification) { |
||
| 109 | $finder = new FilteringFqcnFinder($finder, $this->filter); |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($this->cache instanceof CacheInterface) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 113 | $finder = new CachedFqcnFinder($finder, $this->cache, $this->cacheKey); |
||
| 114 | } |
||
| 115 | |||
| 116 | return $finder->find(); |
||
| 117 | } |
||
| 118 | } |
||
| 119 |