Completed
Push — master ( 0a6a04...506011 )
by Taosikai
15:08
created

Config::parseConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * slince config library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Config;
7
8
use Slince\Config\Exception\UnsupportedFormatException;
9
use Slince\Config\Exception\InvalidFileException;
10
use Slince\Config\Parser\IniParser;
11
use Slince\Config\Parser\JsonParser;
12
use Slince\Config\Parser\ParserInterface;
13
use Slince\Config\Parser\PHPParser;
14
15
class Config extends Collection implements ConfigInterface
16
{
17
    /**
18
     * Array of parser instances
19
     * @var array
20
     */
21
    protected static $parsers = [];
22
23
    /**
24
     * Array of supported file format parsers
25
     * @var array
26
     */
27
    protected static $supportedFileParsers = [
28
        PHPParser::class,
29
        IniParser::class,
30
        JsonParser::class
31
    ];
32
33
    public function __construct($path = null)
34
    {
35
        parent::__construct([]);
36
        is_null($path) || $this->load($path);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function load($path)
43
    {
44
        $paths = is_array($path) ? $path : [$path];
45
        foreach ($paths as $path) {
46
            $this->merge($this->parseConfiguration($path));
47
        }
48
        return $this;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function dump($file)
55
    {
56
        $extension = pathinfo($file, PATHINFO_EXTENSION);
57
        return static::getParser($extension)->dump($file, $this->toArray());
58
    }
59
60
    /**
61
     * Parse a configuration file or directory to an array
62
     * @param string $path
63
     * @return array
64
     * @deprecated
65
     */
66
    public function parse($path)
67
    {
68
        return $this->parseConfiguration($path);
69
    }
70
71
    /**
72
     * Parse a configuration file or directory to an array
73
     * @param string $path
74
     * @return array
75
     */
76
    protected function parseConfiguration($path)
77
    {
78
        $files = $this->findConfigurationFiles($path);
79
        $data = [];
80
        foreach ($files as $file) {
81
            $extension = pathinfo($file, PATHINFO_EXTENSION);
82
            $data = array_merge($data, static::getParser($extension)->parse($file));
83
        }
84
        return $data;
85
    }
86
87
    /**
88
     * finds all supported configuration files at the directory
89
     * @param string $path
90
     * @throws InvalidFileException
91
     * @return array
92
     */
93
    protected function findConfigurationFiles($path)
94
    {
95
        if (is_dir($path)) {
96
            $files = glob($path . '/*.*');
97
            if (empty($files)) {
98
                throw new InvalidFileException(sprintf('There is no configuration file in the directory "%s"', $path));
99
            }
100
        } else {
101
            if (!file_exists($path)) {
102
                throw new InvalidFileException(sprintf('File "%s" does not exists', $path));
103
            }
104
            $files = [$path];
105
        }
106
        return $files;
107
    }
108
109
    /**
110
     * Gets a file parser by its extension
111
     * @param string $extension
112
     * @throws UnsupportedFormatException
113
     * @return ParserInterface
114
     */
115
    protected static function getParser($extension)
116
    {
117
        if (isset(static::$parsers[$extension])) {
118
            return static::$parsers[$extension];
119
        }
120
        foreach (static::$supportedFileParsers as $parser) {
121
            if (in_array($extension, call_user_func([$parser, 'getSupportedExtensions']))) {
122
                static::$parsers[$extension] = new $parser();
123
                return static::$parsers[$extension];
124
            }
125
        }
126
        throw new UnsupportedFormatException($extension);
127
    }
128
}
129