Completed
Push — 1.x ( 91505e...7bb0ba )
by Akihito
21s queued 10s
created

AbstractAppMeta::camel2snake()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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