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

AbstractAppMeta   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 58
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResourceListGenerator() 0 6 1
A getGenerator() 0 18 4
A camel2kebab() 0 4 1
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