-
Notifications
You must be signed in to change notification settings - Fork 1
/
Files.php
59 lines (51 loc) · 1.35 KB
/
Files.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
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace PhpStyler;
use Generator;
use IteratorAggregate;
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
use Traversable;
/**
* @implements IteratorAggregate<int, string>
*/
class Files implements IteratorAggregate
{
/**
* @var string[]
*/
protected array $paths = [];
public function __construct(string ...$paths)
{
$this->paths = $paths;
}
public function getIterator() : Generator
{
foreach ($this->paths as $path) {
if (is_file($path) && str_ends_with($path, '.php')) {
yield $path;
continue;
}
$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator($path),
fn ($c, $k, $i) => $this->filter($c, $k, $i),
),
);
/** @var SplFileInfo $file */
foreach ($files as $file) {
yield $file->getPathname();
}
}
}
protected function filter(
SplFileInfo $current,
string $key,
RecursiveDirectoryIterator $iterator,
) : bool
{
return $iterator->hasChildren() || str_ends_with((string) $current, '.php');
}
}