-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
FileInfoPharExpandingIterator.php
47 lines (43 loc) · 1.27 KB
/
FileInfoPharExpandingIterator.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
<?php
namespace Phpactor\Indexer\Adapter\Php;
use Iterator;
use IteratorAggregate;
use Phar;
use RecursiveIteratorIterator;
use SplFileInfo;
use Traversable;
use UnexpectedValueException;
/**
* @implements IteratorAggregate<SplFileInfo>
*/
class FileInfoPharExpandingIterator implements IteratorAggregate
{
/**
* @param Iterator<SplFileInfo> $innerIterator
* @param list<string> $supportedExtensions
*/
public function __construct(private Iterator $innerIterator, private array $supportedExtensions = ['php'])
{
}
public function getIterator(): Traversable
{
foreach ($this->innerIterator as $fileInfo) {
if ($fileInfo->getExtension() !== 'phar') {
yield $fileInfo;
}
try {
$phar = new Phar($fileInfo->getPathname());
} catch (UnexpectedValueException) {
continue;
}
$iterator = new RecursiveIteratorIterator($phar);
foreach ($iterator as $fileInfo) {
assert($fileInfo instanceof SplFileInfo);
if (!in_array($fileInfo->getExtension(), $this->supportedExtensions)) {
continue;
}
yield $fileInfo;
}
}
}
}