Passed
Push — master ( 1130e1...380591 )
by Hong
01:54
created

Loader::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Config
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Config\Util;
13
14
use Phoole\Base\Tree\Tree;
15
use Phoole\Base\Reader\Reader;
16
17
/**
18
 * Config file loader
19
 *
20
 * @package Phoole\Config
21
 */
22
class Loader
23
{
24
    /**
25
     * tree object
26
     */
27
    protected $tree;
28
29
    /**
30
     * reader object
31
     */
32
    protected $reader;
33
34
    /**
35
     * conf directory
36
     */
37
    protected $root_dir;
38
39
    /**
40
     * default environment
41
     */
42
    protected $environment;
43
44
    /**
45
     * Constructor
46
     *
47
     * @param  string $rootDir
48
     * @param  string $environment
49
     */
50
    public function __construct(string $rootDir, string $environment = '')
51
    {
52
        $this->tree = new Tree();
53
        $this->reader = new Reader();
54
        $this->environment = $environment;
55
        $this->root_dir = rtrim($rootDir, "/\\");
56
    }
57
58
    /**
59
     * Load group(specific) config(s) base on environment value.
60
     *
61
     * - if $environment == '', use the default environment
62
     * - if $group == '', load all avaiable groups
63
     *
64
     * ```php
65
     * $this->load('db')->load('db', 'production/host1');
66
     * ```
67
     *
68
     * @param  string $group
69
     * @param  string $environment
70
     * @return $this
71
     */
72
    public function load(string $group = '', string $environment = ''): object
73
    {
74
        foreach ($this->globFiles($group, $environment) as $file) {
75
            $grp  = explode('.', basename($file), 2)[0];
76
            $this->tree->add($grp, $this->reader->readFile($file));
77
        }
78
        return $this;
79
    }
80
81
    /**
82
     * @return Tree
83
     */
84
    public function getTree(): Tree
85
    {
86
        return $this->tree;
87
    }
88
89
    /**
90
     * Returns an array of conf files to read from
91
     *
92
     * @param  string $group
93
     * @param  string $environment
94
     * @return array
95
     */
96
    protected function globFiles(string $group, string $environment): array
97
    {
98
        $files = [];
99
        $grp = ($group ?: '*') . '.*';
100
        foreach ($this->searchDirs($environment) as $dir) {
101
            $globs = \glob($dir . \DIRECTORY_SEPARATOR . $grp);
102
            $files = array_merge($files, $globs ?: []);
103
        }
104
        return $files;
105
    }
106
107
    /**
108
      * Returns an array of directoris to search thru
109
      *
110
      * @param  string $environment
111
      * @return array
112
      */
113
    protected function searchDirs(string $environment): array
114
    {
115
        $envs  = preg_split(
116
            '~/~',
117
            trim($environment ?: $this->environment, '/'),
118
            -1,
119
            \PREG_SPLIT_NO_EMPTY
120
        );
121
122
        $d = $this->root_dir;
123
        $dirs = [ $d ];
124
        foreach ($envs as $p) {
125
            $d .= \DIRECTORY_SEPARATOR . $p;
126
            $dirs[] = $d;
127
        }
128
        return $dirs;
129
    }
130
}
131