Passed
Branch master (432690)
by Hong
02:58 queued 49s
created

Loader::getTree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
            $files = array_merge($files, glob($dir . \DIRECTORY_SEPARATOR . $grp));
0 ignored issues
show
Bug introduced by
It seems like glob($dir . DIRECTORY_SEPARATOR . $grp) can also be of type false; however, parameter $array2 of array_merge() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
            $files = array_merge($files, /** @scrutinizer ignore-type */ glob($dir . \DIRECTORY_SEPARATOR . $grp));
Loading history...
102
        }
103
        return $files;
104
    }
105
106
    /**
107
      * Returns an array of directoris to search thru
108
      *
109
      * @param  string $environment
110
      * @return array
111
      */
112
    protected function searchDirs(string $environment): array
113
    {
114
        $envs  = preg_split(
115
            '~/~',
116
            trim($environment ?: $this->environment, '/'),
117
            -1,
118
            \PREG_SPLIT_NO_EMPTY
119
        );
120
121
        $d  = $this->root_dir;
122
        $dirs[] = $d;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$dirs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dirs = array(); before regardless.
Loading history...
123
        foreach ($envs as $p) {
124
            $d .= \DIRECTORY_SEPARATOR . $p;
125
            $dirs[] = $d;
126
        }
127
        return $dirs;
128
    }
129
130
    /**
131
     * clone tree also
132
     */
133
    public function __clone()
134
    {
135
        $this->tree = clone $this->tree;
136
        $this->reader = clone $this->reader;
137
    }
138
}
139