|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Noodlehaus; |
|
4
|
|
|
|
|
5
|
|
|
use Noodlehaus\Exception\FileNotFoundException; |
|
6
|
|
|
use Noodlehaus\Exception\UnsupportedFormatException; |
|
7
|
|
|
use Noodlehaus\Exception\EmptyDirectoryException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Config |
|
11
|
|
|
* |
|
12
|
|
|
* @package Config |
|
13
|
|
|
* @author Jesus A. Domingo <[email protected]> |
|
14
|
|
|
* @author Hassan Khan <[email protected]> |
|
15
|
|
|
* @link https://github.com/noodlehaus/config |
|
16
|
|
|
* @license MIT |
|
17
|
|
|
*/ |
|
18
|
|
|
class Config extends AbstractConfig |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* All file formats supported by Config |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $supportedFileParsers = array( |
|
26
|
|
|
'Noodlehaus\FileParser\Php', |
|
27
|
|
|
'Noodlehaus\FileParser\Ini', |
|
28
|
|
|
'Noodlehaus\FileParser\Json', |
|
29
|
|
|
'Noodlehaus\FileParser\Xml', |
|
30
|
|
|
'Noodlehaus\FileParser\Yaml' |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Static method for loading a Config instance. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string|array $path |
|
37
|
|
|
* |
|
38
|
|
|
* @return Config |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public static function load($path) |
|
41
|
|
|
{ |
|
42
|
3 |
|
return new static($path); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Loads a supported configuration file format. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string|array $path |
|
49
|
|
|
* |
|
50
|
|
|
* @throws EmptyDirectoryException If `$path` is an empty directory |
|
51
|
|
|
*/ |
|
52
|
33 |
|
public function __construct($path) |
|
53
|
|
|
{ |
|
54
|
33 |
|
$paths = $this->getValidPath($path); |
|
55
|
24 |
|
$this->data = array(); |
|
56
|
|
|
|
|
57
|
24 |
|
foreach ($paths as $path) { |
|
58
|
|
|
|
|
59
|
|
|
// Get file information |
|
60
|
24 |
|
$info = pathinfo($path); |
|
61
|
24 |
|
$parts = explode('.', $info['basename']); |
|
62
|
24 |
|
$extension = array_pop($parts); |
|
63
|
24 |
|
if ($extension === 'dist') { |
|
64
|
3 |
|
$extension = array_pop($parts); |
|
65
|
2 |
|
} |
|
66
|
24 |
|
$parser = $this->getParser($extension); |
|
67
|
|
|
|
|
68
|
|
|
// Try and load file |
|
69
|
21 |
|
$this->data = array_replace_recursive($this->data, (array) $parser->parse($path)); |
|
70
|
14 |
|
} |
|
71
|
|
|
|
|
72
|
21 |
|
parent::__construct($this->data); |
|
73
|
21 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Gets a parser for a given file extension |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $extension |
|
79
|
|
|
* |
|
80
|
|
|
* @return Noodlehaus\FileParser\FileParserInterface |
|
81
|
|
|
* |
|
82
|
|
|
* @throws UnsupportedFormatException If `$path` is an unsupported file format |
|
83
|
|
|
*/ |
|
84
|
27 |
|
private function getParser($extension) |
|
85
|
|
|
{ |
|
86
|
27 |
|
foreach ($this->supportedFileParsers as $fileParser) { |
|
87
|
27 |
|
if (in_array($extension, $fileParser::getSupportedExtensions($extension))) { |
|
88
|
23 |
|
return new $fileParser(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
18 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
// If none exist, then throw an exception |
|
94
|
6 |
|
throw new UnsupportedFormatException('Unsupported configuration format'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Gets an array of paths |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $path |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
* |
|
104
|
|
|
* @throws FileNotFoundException If a file is not found at `$path` |
|
105
|
|
|
*/ |
|
106
|
12 |
|
private function getPathFromArray($path) |
|
107
|
|
|
{ |
|
108
|
12 |
|
$paths = array(); |
|
109
|
|
|
|
|
110
|
12 |
|
foreach ($path as $unverifiedPath) { |
|
111
|
|
|
try { |
|
112
|
|
|
// Check if `$unverifiedPath` is optional |
|
113
|
|
|
// If it exists, then it's added to the list |
|
114
|
|
|
// If it doesn't, it throws an exception which we catch |
|
115
|
12 |
|
if ($unverifiedPath[0] !== '?') { |
|
116
|
12 |
|
$paths = array_merge($paths, $this->getValidPath($unverifiedPath)); |
|
117
|
12 |
|
continue; |
|
118
|
|
|
} |
|
119
|
6 |
|
$optionalPath = ltrim($unverifiedPath, '?'); |
|
120
|
6 |
|
$paths = array_merge($paths, $this->getValidPath($optionalPath)); |
|
121
|
|
|
|
|
122
|
8 |
|
} catch (FileNotFoundException $e) { |
|
123
|
|
|
// If `$unverifiedPath` is optional, then skip it |
|
124
|
6 |
|
if ($unverifiedPath[0] === '?') { |
|
125
|
3 |
|
continue; |
|
126
|
|
|
} |
|
127
|
|
|
// Otherwise rethrow the exception |
|
128
|
4 |
|
throw $e; |
|
129
|
|
|
} |
|
130
|
6 |
|
} |
|
131
|
|
|
|
|
132
|
9 |
|
return $paths; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Checks `$path` to see if it is either an array, a directory, or a file |
|
137
|
|
|
* |
|
138
|
|
|
* @param string|array $path |
|
139
|
|
|
* |
|
140
|
|
|
* @return array |
|
141
|
|
|
* |
|
142
|
|
|
* @throws EmptyDirectoryException If `$path` is an empty directory |
|
143
|
|
|
* |
|
144
|
|
|
* @throws FileNotFoundException If a file is not found at `$path` |
|
145
|
|
|
*/ |
|
146
|
30 |
|
private function getValidPath($path) |
|
147
|
|
|
{ |
|
148
|
|
|
// If `$path` is array |
|
149
|
30 |
|
if (is_array($path)) { |
|
150
|
12 |
|
return $this->getPathFromArray($path); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// If `$path` is a directory |
|
154
|
30 |
|
if (is_dir($path)) { |
|
155
|
6 |
|
$paths = glob($path . '/*.*'); |
|
156
|
6 |
|
if (empty($paths)) { |
|
157
|
3 |
|
throw new EmptyDirectoryException("Configuration directory: [$path] is empty"); |
|
158
|
|
|
} |
|
159
|
3 |
|
return $paths; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// If `$path` is not a file, throw an exception |
|
163
|
24 |
|
if (!file_exists($path)) { |
|
164
|
9 |
|
throw new FileNotFoundException("Configuration file: [$path] cannot be found"); |
|
165
|
|
|
} |
|
166
|
21 |
|
return array($path); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|