Completed
Push — 1.x ( 7bb0ba...d6f2bd )
by Akihito
14s queued 12s
created

AbstractAppMeta::getGenerator()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 1
cts 1
cp 1
rs 9.6666
c 0
b 0
f 0
cc 4
nc 5
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\AppMeta;
6
7
use Koriym\Psr4List\Psr4List;
8
9
abstract class AbstractAppMeta
10
{
11
    /**
12
     * Application name "{Vendor}\{Project}"
13
     *
14
     * @var string
15
     */
16
    public $name;
17
18
    /**
19
     * @var string
20
     */
21
    public $appDir;
22
23
    /**
24
     * @var string
25
     */
26
    public $tmpDir;
27
28
    /**
29
     * @var string
30
     */
31
    public $logDir;
32
33
    public function getResourceListGenerator() : \Generator
34
    {
35
        $list = new Psr4List;
36
37
        return $list($this->name . '\Resource', $this->appDir . '/src/Resource');
38 1
    }
39
40 1
    /**
41 1
     * @param string $scheme 'app' | 'page' | '*'
42
     */
43 1
    public function getGenerator(string $scheme = '*') : \Generator
44
    {
45
        foreach ($this->getResourceListGenerator() as list($class, $file)) {
46
            $paths = explode('\\', $class);
47
            $path = array_slice($paths, 3);
48
            array_walk($path, [$this, 'camel2kebab']);
49
            if ($scheme === '*') {
50
                $uri = sprintf('%s://self/%s', $path[0], implode('/', array_slice($path, 1)));
51
52
                yield new ResMeta($uri, $class, $file);
53
            }
54
            if ($scheme === $path[0]) {
55
                $uri = sprintf('/%s', implode('/', array_slice($path, 1)));
56
57
                yield new ResMeta($uri, $class, $file);
58
            }
59
        }
60
    }
61
62
    private function camel2kebab(string &$str)
63
    {
64
        $str = ltrim(strtolower((string) preg_replace('/[A-Z]/', '-\0', $str)), '-');
65
    }
66
}
67