Skip to content

Commit 9a639f7

Browse files
committed
Allow excluding paths from the source directory
1 parent d7a2f6a commit 9a639f7

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

packages/guides/src/FileCollector.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
use Flyfinder\Specification\AndSpecification;
1818
use Flyfinder\Specification\HasExtension;
1919
use Flyfinder\Specification\InPath;
20+
use Flyfinder\Specification\NotSpecification;
21+
use Flyfinder\Specification\SpecificationInterface;
2022
use InvalidArgumentException;
2123
use League\Flysystem\FilesystemInterface;
2224

25+
use function array_map;
26+
use function array_reduce;
27+
use function array_shift;
2328
use function sprintf;
2429
use function strlen;
2530
use function substr;
@@ -36,14 +41,29 @@ final class FileCollector
3641
* This takes into account the presence of cached & fresh MetaEntry
3742
* objects, and avoids adding files to the parse queue that have
3843
* not changed and whose direct dependencies have not changed.
44+
*
45+
* @param string[] $excludedPaths
3946
*/
40-
public function collect(FilesystemInterface $filesystem, string $directory, string $extension): Files
47+
public function collect(FilesystemInterface $filesystem, string $directory, string $extension, array $excludedPaths = []): Files
4148
{
4249
$directory = trim($directory, '/');
50+
$specification = new AndSpecification(new InPath(new Path($directory)), new HasExtension([$extension]));
51+
if ($excludedPaths) {
52+
$excludedSpecifications = array_map(static fn (string $path) => new NotSpecification(new InPath(new Path($path))), $excludedPaths);
53+
$excludedSpecification = array_shift($excludedSpecifications);
54+
55+
$specification = new AndSpecification(
56+
$specification,
57+
array_reduce(
58+
$excludedSpecifications,
59+
static fn (SpecificationInterface $carry, SpecificationInterface $spec) => new AndSpecification($carry, $spec),
60+
$excludedSpecification,
61+
),
62+
);
63+
}
64+
4365
/** @var array<array<string>> $files */
44-
$files = $filesystem->find(
45-
new AndSpecification(new InPath(new Path($directory)), new HasExtension([$extension])),
46-
);
66+
$files = $filesystem->find($specification);
4767

4868
// completely populate the splFileInfos property
4969
$this->fileInfos = [];

packages/guides/src/Handlers/ParseDirectoryCommand.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
final class ParseDirectoryCommand
2020
{
21+
/** @param string[] $excludedPaths */
2122
public function __construct(
2223
private readonly FilesystemInterface $origin,
2324
private readonly string $directory,
2425
private readonly string $inputFormat,
2526
private readonly ProjectNode $projectNode,
27+
private readonly array $excludedPaths = [],
2628
) {
2729
}
2830

@@ -45,4 +47,10 @@ public function getProjectNode(): ProjectNode
4547
{
4648
return $this->projectNode;
4749
}
50+
51+
/** @return string[] */
52+
public function getExcludedPaths(): array
53+
{
54+
return $this->excludedPaths;
55+
}
4856
}

packages/guides/src/Handlers/ParseDirectoryHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function handle(ParseDirectoryCommand $command): array
5656
$extension,
5757
);
5858

59-
$files = $this->fileCollector->collect($origin, $currentDirectory, $extension);
59+
$files = $this->fileCollector->collect($origin, $currentDirectory, $extension, $command->getExcludedPaths());
6060

6161
$postCollectFilesForParsingEvent = $this->eventDispatcher->dispatch(
6262
new PostCollectFilesForParsingEvent($command, $files),

0 commit comments

Comments
 (0)