|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of phpDocumentor. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @link https://phpdoc.org |
| 12 | + */ |
| 13 | + |
| 14 | +namespace phpDocumentor\FileSystem\Finder; |
| 15 | + |
| 16 | +use Flyfinder\Path as FlyFinderPath; |
| 17 | +use Flyfinder\Specification\Glob; |
| 18 | +use Flyfinder\Specification\HasExtension; |
| 19 | +use Flyfinder\Specification\InPath; |
| 20 | +use Flyfinder\Specification\IsHidden; |
| 21 | +use Flyfinder\Specification\NotSpecification; |
| 22 | +use Flyfinder\Specification\SpecificationInterface; |
| 23 | +use phpDocumentor\FileSystem\Path; |
| 24 | + |
| 25 | +/** |
| 26 | + * Factory class to build Specification used by FlyFinder when reading files to process. |
| 27 | + */ |
| 28 | +final class SpecificationFactory implements SpecificationFactoryInterface |
| 29 | +{ |
| 30 | + /** |
| 31 | + * Creates a SpecificationInterface object based on the ignore and extension parameters. |
| 32 | + * |
| 33 | + * @param list<string|Path> $paths |
| 34 | + * @param list<string> $extensions |
| 35 | + */ |
| 36 | + public function create(array $paths, Exclude $ignore, array $extensions): SpecificationInterface |
| 37 | + { |
| 38 | + /** @var ?Glob $pathSpec */ |
| 39 | + $pathSpec = null; |
| 40 | + foreach ($paths as $path) { |
| 41 | + if ($path instanceof Path) { |
| 42 | + $condition = new InPath(new FlyFinderPath((string) $path)); |
| 43 | + } else { |
| 44 | + $condition = new Glob($path); |
| 45 | + } |
| 46 | + |
| 47 | + if ($pathSpec === null) { |
| 48 | + $pathSpec = $condition; |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + $pathSpec = $pathSpec->orSpecification($condition); |
| 53 | + } |
| 54 | + |
| 55 | + /** @var ?Glob $ignoreSpec */ |
| 56 | + $ignoreSpec = null; |
| 57 | + foreach ($ignore->getPaths() as $path) { |
| 58 | + if ($ignoreSpec === null) { |
| 59 | + $ignoreSpec = new Glob($path); |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + $ignoreSpec = $ignoreSpec->orSpecification(new Glob($path)); |
| 64 | + } |
| 65 | + |
| 66 | + if ($ignore->excludeHidden()) { |
| 67 | + $ignoreSpec = $ignoreSpec === null |
| 68 | + ? new IsHidden() |
| 69 | + : $ignoreSpec->orSpecification(new IsHidden()); |
| 70 | + } |
| 71 | + |
| 72 | + $result = new HasExtension($extensions); |
| 73 | + if ($ignoreSpec !== null) { |
| 74 | + $result = $result->andSpecification(new NotSpecification($ignoreSpec)); |
| 75 | + } |
| 76 | + |
| 77 | + if ($pathSpec !== null) { |
| 78 | + $result = $result->andSpecification($pathSpec); |
| 79 | + } |
| 80 | + |
| 81 | + return $result; |
| 82 | + } |
| 83 | +} |
0 commit comments