GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a41471...693b31 )
by Hong
06:13
created

ConfigFileLoader::buildSearchDirs()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Config
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Config\Loader;
16
17
use Phossa2\Shared\Reader\Reader;
18
use Phossa2\Config\Message\Message;
19
use Phossa2\Shared\Base\ObjectAbstract;
20
use Phossa2\Config\Exception\InvalidArgumentException;
21
22
/**
23
 * ConfigFileLoader
24
 *
25
 * Read configs from file.
26
 *
27
 * @package Phossa2\Config
28
 * @author  Hong Zhang <[email protected]>
29
 * @version 2.0.8
30
 * @since   2.0.0 added
31
 * @since   2.0.8 updated
32
 */
33
class ConfigFileLoader extends ObjectAbstract implements ConfigLoaderInterface
34
{
35
    /**
36
     * Config root directory
37
     *
38
     * @var    string
39
     * @access protected
40
     */
41
    protected $root_dir;
42
43
    /**
44
     * config file type
45
     *
46
     * @var    string
47
     * @access protected
48
     */
49
    protected $file_type;
50
51
    /**
52
     * cached subdirs to load files
53
     *
54
     * @var    array
55
     * @access protected
56
     */
57
    protected $sub_dirs = [];
58
59
    /**
60
     * default environment
61
     *
62
     * @var    string
63
     * @access protected
64
     */
65
    protected $environment;
66
67
    /**
68
     * Constructor
69
     *
70
     * @param  string $rootDir
71
     * @param  string $environment
72
     * @param  string $fileType
73
     * @throws InvalidArgumentException if any argument invalid
74
     * @access public
75
     * @api
76
     */
77
    public function __construct(
78
        /*# string */ $rootDir,
79
        /*# string */ $environment = '',
80
        /*# string */ $fileType = 'php'
81
    ) {
82
        $this
83
            ->setRootDir($rootDir)
84
            ->setFileType($fileType)
85
            ->setEnvironment($environment);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91
    public function load(
92
        /*# string */ $group,
93
        /*# string */ $environment = ''
94
    )/*# : array */ {
95
        $data = [];
96
        $env  = $environment ?: $this->environment;
97
98
        foreach ($this->globFiles($group, $env) as $file) {
99
            $grp = basename($file, '.' . $this->file_type);
100
            if (!isset($data[$grp])) {
101
                $data[$grp] = [];
102
            }
103
            $data[$grp] = array_replace_recursive(
104
                $data[$grp], (array) Reader::readFile($file)
105
            );
106
        }
107
        return $data;
108
    }
109
110
    /**
111
     * Set config file root directory
112
     *
113
     * @param  string $rootDir
114
     * @return $this
115
     * @throws InvalidArgumentException if root dir is unknown
116
     * @access public
117
     * @api
118
     */
119
    public function setRootDir(/*# string */ $rootDir)
120
    {
121
        $dir = realpath($rootDir);
122
123
        if (false === $dir) {
124
            throw new InvalidArgumentException(
125
                Message::get(Message::CONFIG_ROOT_INVALID, $rootDir),
126
                Message::CONFIG_ROOT_INVALID
127
            );
128
        }
129
130
        $this->root_dir = $dir . \DIRECTORY_SEPARATOR;
131
132
        return $this;
133
    }
134
135
    /**
136
     * Set config file type
137
     *
138
     * @param  string $fileType
139
     * @return $this
140
     * @throws InvalidArgumentException if unsupported file type
141
     * @access public
142
     * @api
143
     */
144
    public function setFileType(/*# string */ $fileType)
145
    {
146
        if (!Reader::isSupported($fileType)) {
147
            throw new InvalidArgumentException(
148
                Message::get(Message::CONFIG_FILE_TYPE_UNKNOWN, $fileType),
149
                Message::CONFIG_FILE_TYPE_UNKNOWN
150
            );
151
        }
152
153
        $this->file_type = $fileType;
154
        return $this;
155
    }
156
157
    /**
158
     * Set default environment
159
     *
160
     * @param  string $environment
161
     * @return $this
162
     * @access public
163
     * @api
164
     */
165
    public function setEnvironment(/*# string */ $environment)
166
    {
167
        $this->environment = $environment;
168
        $this->getSearchDirs($environment);
169
        return $this;
170
    }
171
172
    /**
173
     * Returns an array of files to read from
174
     *
175
     * @param  string $group
176
     * @param  string $environment
177
     * @return array
178
     * @access protected
179
     */
180
    protected function globFiles(
181
        /*# string */ $group,
182
        /*# string */ $environment
183
    )/*# : array */ {
184
        $files = [];
185
        $group = '' === $group ? '*' : $group;
186
        foreach ($this->getSearchDirs($environment) as $dir) {
187
            $file  = $dir . $group . '.' . $this->file_type;
188
            $files = array_merge($files, glob($file));
189
        }
190
        return $files;
191
    }
192
193
    /**
194
     * Get the search directories
195
     *
196
     * @param  string $env
197
     * @return array
198
     * @access protected
199
     */
200
    protected function getSearchDirs(/*# string */ $env)/*# : array */
201
    {
202
        if (!isset($this->sub_dirs[$env])) {
203
            $this->sub_dirs[$env] = $this->buildSearchDirs($env);
204
        }
205
        return $this->sub_dirs[$env];
206
    }
207
208
    /**
209
     * Build search directories
210
     *
211
     * @param  string $env
212
     * @return array
213
     * @access protected
214
     */
215
    protected function buildSearchDirs(/*# string */ $env)/*# : array */
216
    {
217
        // root
218
        $path = $this->root_dir;
219
        $subdirs = [$path];
220
221
        // part
222
        $parts = preg_split(
223
            '/[\/\\\]/', trim($env, '/\\'), 0, \PREG_SPLIT_NO_EMPTY
224
        );
225
226
        foreach ($parts as $dir) {
227
            $path .= $dir . \DIRECTORY_SEPARATOR;
228
            if (false === file_exists($path)) {
229
                trigger_error(
230
                    Message::get(Message::CONFIG_ENV_UNKNOWN, $env),
231
                    \E_USER_WARNING
232
                );
233
                break;
234
            }
235
            $subdirs[] = $path;
236
        }
237
238
        return $subdirs;
239
    }
240
}
241