forked from phpactor/phpactor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilesystemFileListProvider.php
50 lines (42 loc) · 1.52 KB
/
FilesystemFileListProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Phpactor\Indexer\Adapter\Filesystem;
use Phpactor\Filesystem\Domain\FilePath;
use Phpactor\Filesystem\Domain\Filesystem;
use Phpactor\Indexer\Model\FileList;
use Phpactor\Indexer\Model\FileListProvider;
use SplFileInfo;
use Phpactor\Indexer\Model\Index;
class FilesystemFileListProvider implements FileListProvider
{
/**
* @param list<string> $excludePatterns
* @param list<string> $includePatterns
* @param list<string> $supportedExtensions
*/
public function __construct(
private Filesystem $filesystem,
private array $includePatterns = [],
private array $excludePatterns = [],
private array $supportedExtensions = ['php'],
) {
}
public function provideFileList(Index $index, ?string $subPath = null): FileList
{
if (null !== $subPath && $this->filesystem->exists($subPath) && is_file($subPath)) {
return FileList::fromSingleFilePath($subPath);
}
$files = $this->filesystem->fileList()
->byExtensions($this->supportedExtensions)
->includeAndExclude($this->includePatterns, $this->excludePatterns)
;
if ($subPath) {
$files = $files->within(FilePath::fromString($subPath));
}
if (!$subPath) {
$files = $files->filter(function (SplFileInfo $fileInfo) use ($index) {
return false === $index->isFresh($fileInfo);
});
}
return FileList::fromInfoIterator($files->getSplFileInfoIterator());
}
}