Skip to content

Commit 90ce9a9

Browse files
committed
Allow excluding paths from the source directory
1 parent d7a2f6a commit 90ce9a9

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

packages/guides/src/FileCollector.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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

@@ -37,13 +39,26 @@ final class FileCollector
3739
* objects, and avoids adding files to the parse queue that have
3840
* not changed and whose direct dependencies have not changed.
3941
*/
40-
public function collect(FilesystemInterface $filesystem, string $directory, string $extension): Files
42+
public function collect(FilesystemInterface $filesystem, string $directory, string $extension, array $excludedPaths = []): Files
4143
{
4244
$directory = trim($directory, '/');
45+
$specification = new AndSpecification(new InPath(new Path($directory)), new HasExtension([$extension]));
46+
if ($excludedPaths) {
47+
$excludedSpecifications = array_map(fn (string $path) => new NotSpecification(new InPath(new Path($path))), $excludedPaths);
48+
$excludedSpecification = array_shift($excludedSpecifications);
49+
50+
$specification = new AndSpecification(
51+
$specification,
52+
array_reduce(
53+
$excludedSpecifications,
54+
fn (SpecificationInterface $carry, SpecificationInterface $spec) => new AndSpecification($carry, $spec),
55+
$excludedSpecification
56+
)
57+
);
58+
}
59+
4360
/** @var array<array<string>> $files */
44-
$files = $filesystem->find(
45-
new AndSpecification(new InPath(new Path($directory)), new HasExtension([$extension])),
46-
);
61+
$files = $filesystem->find($specification);
4762

4863
// completely populate the splFileInfos property
4964
$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)