1 | <?php |
||
35 | final class Konfig extends AbstractKonfig |
||
36 | { |
||
37 | /** |
||
38 | * Array of file parsers objects. |
||
39 | * |
||
40 | * @var array|null |
||
41 | * |
||
42 | * @since 0.1.0 |
||
43 | */ |
||
44 | protected $fileParsers; |
||
45 | |||
46 | /** |
||
47 | * Stores loaded configuration files. |
||
48 | * |
||
49 | * @var array Array of loaded configuration files |
||
50 | * |
||
51 | * @since 0.1.0 |
||
52 | */ |
||
53 | protected static $loadedFiles = []; |
||
54 | |||
55 | /** |
||
56 | * Array of loaded data. |
||
57 | * |
||
58 | * @var array|null |
||
59 | * |
||
60 | * @since 0.1.0 |
||
61 | */ |
||
62 | protected static $loadedData = null; |
||
63 | |||
64 | /** |
||
65 | * Loads a supported configuration file format. |
||
66 | * |
||
67 | * @param string|array|mixed $path String file | configuration array |
||
68 | * | Konfig instance | configuration array | Konfig instance |
||
69 | * @param array $parsers Parsers |
||
70 | * @param bool $overwrite Whether to overwrite existing values |
||
71 | * @param bool $cache Allow caching |
||
72 | * |
||
73 | * @throws EmptyDirectoryException If `$path` is an empty directory |
||
74 | */ |
||
75 | 33 | public function __construct( |
|
76 | $path = null, |
||
77 | array $parsers = [], |
||
78 | $overwrite = false, |
||
|
|||
79 | $cache = true |
||
80 | ) { |
||
81 | 33 | $this->setFileParsers($parsers); |
|
82 | |||
83 | 33 | $paths = $this->getValidPath($path); |
|
84 | |||
85 | 24 | $this->data = []; |
|
86 | |||
87 | 24 | foreach ($paths as $path) { |
|
88 | // Get file information |
||
89 | 24 | $info = pathinfo($path); |
|
90 | // $info = pathinfo($path, PATHINFO_EXTENSION); |
||
91 | 24 | $parts = explode('.', $info['basename']); |
|
92 | 24 | $ext = array_pop($parts); |
|
93 | |||
94 | 24 | if ($ext === 'dist') { |
|
95 | 3 | $ext = array_pop($parts); |
|
96 | 1 | } |
|
97 | |||
98 | 24 | $parser = $this->getParser($ext); |
|
99 | |||
100 | // Try and load file |
||
101 | 21 | $this->data = array_replace_recursive( |
|
102 | 21 | $this->data, |
|
103 | 21 | (array) $parser->parse($path) |
|
104 | 7 | ); |
|
105 | |||
106 | 21 | self::$loadedFiles[$path] = true; |
|
107 | 7 | } |
|
108 | |||
109 | 21 | self::$loadedData = $this->data; |
|
110 | |||
111 | 21 | parent::__construct($this->data); |
|
112 | 21 | } |
|
113 | |||
114 | /** |
||
115 | * Static method for loading a Konfig instance. |
||
116 | * |
||
117 | * @param string|array|mixed $path string file | configuration array |
||
118 | * | Konfig instance |
||
119 | * @param array $parsers Parsers to use with Konfig |
||
120 | * @param bool $overwrite Whether to overwrite existing values |
||
121 | * @param bool $cache Allow caching |
||
122 | * |
||
123 | * @return Konfig |
||
124 | */ |
||
125 | 3 | public static function load( |
|
126 | $path = null, |
||
127 | array $parsers = [], |
||
128 | $overwrite = false, |
||
129 | $cache = true |
||
130 | ) { |
||
131 | 3 | return new static($path, $parsers); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Static method for getting loaded Konfig files. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | public static function loaded() |
||
140 | { |
||
141 | return self::$loadedFiles; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Get file parsers. |
||
146 | * |
||
147 | * @return FileParser[] |
||
148 | * |
||
149 | * @since 0.1.0 |
||
150 | * @codeCoverageIgnore |
||
151 | */ |
||
152 | public function getFileParsers() |
||
156 | |||
157 | /** |
||
158 | * Add file parsers. |
||
159 | * |
||
160 | * @param FileParser $fileParser Parser |
||
161 | * |
||
162 | * @return void Void |
||
163 | * @since 0.1.0 |
||
164 | * @codeCoverageIgnore |
||
165 | */ |
||
166 | protected function addFileParser(FileParser $fileParser) |
||
170 | |||
171 | /** |
||
172 | * Set file parsers. |
||
173 | * |
||
174 | * @param array $fileParsers Parsers array |
||
175 | * |
||
176 | * @return void Void |
||
177 | * @since 0.1.0 |
||
178 | * @codeCoverageIgnore |
||
179 | */ |
||
180 | protected function setFileParsers(array $fileParsers = []) |
||
201 | |||
202 | /** |
||
203 | * Gets a parser for a given file extension. |
||
204 | * |
||
205 | * @param string|null $ext File extension |
||
206 | * |
||
207 | * @return FileParser |
||
208 | * |
||
209 | * @throws Exception If `$ext` is empty |
||
210 | * @throws UnsupportedFileFormatException If `$path` |
||
211 | * is an unsupported file format |
||
212 | */ |
||
213 | private function getParser($ext = null) |
||
239 | |||
240 | 21 | /** |
|
241 | * Gets an array of paths. |
||
242 | * |
||
243 | * @param array $path Path to analyze and handle |
||
244 | * |
||
245 | * @return array |
||
246 | * |
||
247 | * @throws FileNotFoundException If a file is not found in `$path` |
||
248 | * @codeCoverageIgnore |
||
249 | */ |
||
250 | private function pathFromArray($path) |
||
284 | |||
285 | /** |
||
286 | * Checks `$path` to see if it is either an array, a directory, or a file. |
||
287 | * |
||
288 | * @param string|array $path Path to analyze and handle |
||
289 | * |
||
290 | * @return array |
||
291 | * |
||
292 | * @throws EmptyDirectoryException If `$path` is an empty directory |
||
293 | * @throws FileNotFoundException If a file is not found at `$path` |
||
294 | */ |
||
295 | private function getValidPath($path) |
||
324 | |||
325 | 21 | /** |
|
326 | * __toString. |
||
327 | * |
||
328 | * @return string |
||
329 | * @since 0.1.2 |
||
330 | * @codeCoverageIgnore |
||
331 | */ |
||
332 | public function __toString() |
||
336 | } |
||
337 | |||
339 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.