Psr4ClassFinder::getIterator()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 9
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 13
rs 9.2222
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace inroutephp\inroute\Compiler;
6
7
/**
8
 * @implements \IteratorAggregate<string>
9
 */
10
final class Psr4ClassFinder implements \IteratorAggregate
11
{
12
    /**
13
     * @var string
14
     */
15
    private $directory;
16
17
    /**
18
     * @var string
19
     */
20
    private $prefix;
21
22
    public function __construct(string $directory, string $prefix)
23
    {
24
        $this->directory = $directory;
25
26
        if (!preg_match('/\\\$/', $prefix)) {
27
            $prefix .= '\\';
28
        }
29
30
        $this->prefix = $prefix;
31
    }
32
33
    /** @return \Generator<string> */
34
    public function getIterator(): \Generator
35
    {
36
        foreach (new \DirectoryIterator($this->directory) as $fileInfo) {
37
            if ($fileInfo->isDot()) {
38
                continue;
39
            }
40
            if ($fileInfo->isFile() && $fileInfo->getExtension() == 'php') {
41
                yield $this->prefix . $fileInfo->getBasename('.php');
42
            }
43
            if ($fileInfo->isDir()) {
44
                yield from new Psr4ClassFinder(
45
                    $this->directory . '/' . $fileInfo->getBasename(),
46
                    $this->prefix . $fileInfo->getBasename()
47
                );
48
            }
49
        }
50
    }
51
}
52