Passed
Pull Request — master (#54)
by Thierry
02:37
created

Reader::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Reader.php - Jaxon config reader
5
 *
6
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
7
 * @author Thierry Feuzeu <[email protected]>
8
 * @copyright 2016 Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-core
11
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
12
13
namespace Jaxon\Utils\Config;
14
15
class Reader
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Reader
Loading history...
16
{
17
    /**
18
     * Read options from a config file
19
     *
20
     * @param string        $sConfigFile        The full path to the config file
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 8 found
Loading history...
21
     *
22
     * @return array
23
     */
24
    public function read($sConfigFile)
25
    {
26
        $sExt = pathinfo($sConfigFile, PATHINFO_EXTENSION);
27
        switch($sExt)
28
        {
29
        case 'php':
30
            $aConfigOptions = Php::read($sConfigFile);
31
            break;
32
        case 'yaml':
33
        case 'yml':
34
            $aConfigOptions = Yaml::read($sConfigFile);
35
            break;
36
        case 'json':
37
            $aConfigOptions = Json::read($sConfigFile);
38
            break;
39
        default:
40
            $sErrorMsg = jaxon_trans('config.errors.file.extension', ['path' => $sConfigFile]);
41
            throw new \Jaxon\Utils\Config\Exception\File($sErrorMsg);
42
        }
43
44
        return $aConfigOptions;
45
    }
46
47
    /**
48
     * Read options from a config file and setup the library
49
     *
50
     * @param string        $sConfigFile        The full path to the config file
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 8 found
Loading history...
51
     * @param string        $sConfigSection     The section of the config file to be loaded
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
52
     *
53
     * @return void
54
     */
55
    public function load($sConfigFile, $sConfigSection = '')
56
    {
57
        $aConfigOptions = $this->read($sConfigFile);
58
        // Setup the lib config options.
59
        jaxon()->di()->getConfig()->setOptions($aConfigOptions, $sConfigSection);
60
    }
61
}
62