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
|
|
|
use Slince\Config\Parser\XmlParser; |
15
|
|
|
use Slince\Config\Parser\YamlParser; |
16
|
|
|
|
17
|
|
|
class Config extends Collection implements ConfigInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Array of parser instances |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected static $parsers = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Array of supported file format parsers |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected static $supportedFileParsers = [ |
30
|
|
|
PHPParser::class, |
31
|
|
|
IniParser::class, |
32
|
|
|
JsonParser::class, |
33
|
|
|
XmlParser::class, |
34
|
|
|
YamlParser::class |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
public function __construct($path = null) |
38
|
|
|
{ |
39
|
|
|
parent::__construct([]); |
40
|
|
|
is_null($path) || $this->load($path); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function load($path) |
47
|
|
|
{ |
48
|
|
|
$paths = is_array($path) ? $path : [$path]; |
49
|
|
|
foreach ($paths as $path) { |
50
|
|
|
$this->merge($this->parseConfiguration($path)); |
51
|
|
|
} |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function dump($file) |
59
|
|
|
{ |
60
|
|
|
$extension = pathinfo($file, PATHINFO_EXTENSION); |
61
|
|
|
return static::getParser($extension)->dump($file, $this->toArray()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Parse a configuration file or directory to an array |
66
|
|
|
* @param string $path |
67
|
|
|
* @return array |
68
|
|
|
* @deprecated |
69
|
|
|
*/ |
70
|
|
|
public function parse($path) |
71
|
|
|
{ |
72
|
|
|
return $this->parseConfiguration($path); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Add a custom parser |
77
|
|
|
* @param ParserInterface $parser |
78
|
|
|
*/ |
79
|
|
|
public static function addParser(ParserInterface $parser) |
80
|
|
|
{ |
81
|
|
|
foreach ($parser::getSupportedExtensions() as $extension) { |
82
|
|
|
static::$parsers[$extension] = $parser; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Parse a configuration file or directory to an array |
88
|
|
|
* @param string $path |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function parseConfiguration($path) |
92
|
|
|
{ |
93
|
|
|
$files = $this->findConfigurationFiles($path); |
94
|
|
|
$data = []; |
95
|
|
|
foreach ($files as $file) { |
96
|
|
|
$extension = pathinfo($file, PATHINFO_EXTENSION); |
97
|
|
|
$data = array_merge($data, static::getParser($extension)->parse($file)); |
98
|
|
|
} |
99
|
|
|
return $data; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* finds all supported configuration files at the directory |
104
|
|
|
* @param string $path |
105
|
|
|
* @throws InvalidFileException |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
protected function findConfigurationFiles($path) |
109
|
|
|
{ |
110
|
|
|
if (is_dir($path)) { |
111
|
|
|
$files = glob($path . '/*.*'); |
112
|
|
|
if (empty($files)) { |
113
|
|
|
throw new InvalidFileException(sprintf('There is no configuration file in the directory "%s"', $path)); |
114
|
|
|
} |
115
|
|
|
} else { |
116
|
|
|
if (!file_exists($path)) { |
117
|
|
|
throw new InvalidFileException(sprintf('File "%s" does not exists', $path)); |
118
|
|
|
} |
119
|
|
|
$files = [$path]; |
120
|
|
|
} |
121
|
|
|
return $files; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Gets a file parser by its extension |
126
|
|
|
* @param string $extension |
127
|
|
|
* @throws UnsupportedFormatException |
128
|
|
|
* @return ParserInterface |
129
|
|
|
*/ |
130
|
|
|
protected static function getParser($extension) |
131
|
|
|
{ |
132
|
|
|
if (isset(static::$parsers[$extension])) { |
133
|
|
|
return static::$parsers[$extension]; |
134
|
|
|
} |
135
|
|
|
foreach (static::$supportedFileParsers as $parser) { |
136
|
|
|
if (in_array($extension, call_user_func([$parser, 'getSupportedExtensions']))) { |
137
|
|
|
static::$parsers[$extension] = new $parser(); |
138
|
|
|
return static::$parsers[$extension]; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
throw new UnsupportedFormatException($extension); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|