1 | <?php |
||
20 | final class Konfig extends AbstractKonfig |
||
21 | { |
||
22 | /** |
||
23 | * @var FileParser[] $fileParsers Array of file parsers objects |
||
24 | * @since 0.1.0 |
||
25 | */ |
||
26 | protected $fileParsers; |
||
27 | |||
28 | /** |
||
29 | * Stores loaded configuration files |
||
30 | * |
||
31 | * @var array $loadedFiles Array of loaded configuration files |
||
32 | * @since 0.1.0 |
||
33 | */ |
||
34 | static protected $loadedFiles = []; |
||
35 | |||
36 | static protected $loadedData = null; |
||
37 | |||
38 | /** |
||
39 | * Loads a supported configuration file format. |
||
40 | * |
||
41 | * @param string|array|mixed $path String file | configuration array | Konfig instance |
||
42 | * @throws EmptyDirectoryException If `$path` is an empty directory |
||
43 | */ |
||
44 | public function __construct($path = null, array $parsers = []) |
||
45 | { |
||
46 | 33 | $this->setFileParsers($parsers); |
|
47 | |||
48 | 33 | $paths = $this->getValidPath($path); |
|
49 | |||
50 | 33 | $this->data = []; |
|
51 | |||
52 | 24 | foreach ($paths as $path) { |
|
53 | // Get file information |
||
54 | 24 | $info = pathinfo($path); |
|
55 | // $info = pathinfo($path, PATHINFO_EXTENSION); |
||
56 | 24 | $parts = explode('.', $info['basename']); |
|
57 | $ext = array_pop($parts); |
||
58 | 24 | ||
59 | 24 | if ($ext === 'dist') { |
|
60 | $ext = array_pop($parts); |
||
61 | 24 | } |
|
62 | 3 | ||
63 | 2 | $parser = $this->getParser($ext); |
|
64 | |||
65 | 24 | // Try and load file |
|
66 | $this->data = array_replace_recursive($this->data, (array) $parser->parse($path)); |
||
67 | |||
68 | 21 | self::$loadedFiles[$path] = true; |
|
69 | } |
||
70 | 21 | ||
71 | 14 | self::$loadedData = $this->data; |
|
72 | |||
73 | 21 | parent::__construct($this->data); |
|
74 | } |
||
75 | 21 | ||
76 | 21 | /** |
|
77 | * Static method for loading a Konfig instance. |
||
78 | * |
||
79 | * @param string|array|mixed $path string file | configuration array | Konfig instance |
||
80 | * @param array $parsers Parsers to use with Konfig |
||
81 | * @param bool $overwrite Whether to overwrite existing values |
||
82 | * @param bool $cache Allow caching |
||
83 | * @return Konfig |
||
84 | */ |
||
85 | 3 | public static function load($path = null, array $parsers = [], $overwrite = false, $cache = true) |
|
86 | { |
||
87 | 3 | return new static($path, $parsers); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Static method for getting loaded Konfig files. |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public static function loaded() |
||
99 | |||
100 | /** |
||
101 | * @return FileParser[] |
||
102 | * @since 0.1.0 |
||
103 | */ |
||
104 | public function getFileParsers() |
||
108 | |||
109 | /** |
||
110 | * @return void |
||
111 | * @since 0.1.0 |
||
112 | */ |
||
113 | protected function addFileParser(FileParser $fileParser) |
||
117 | |||
118 | /** |
||
119 | * @return void |
||
120 | * @since 0.1.0 |
||
121 | */ |
||
122 | protected function setFileParsers(array $fileParsers = []) |
||
143 | |||
144 | /** |
||
145 | * Gets a parser for a given file extension |
||
146 | * |
||
147 | * @param string $ext File extension |
||
148 | * @return Konfig\FileParser |
||
149 | * @throws Exception If `$ext` is empty |
||
150 | * @throws UnsupportedFileFormatException If `$path` is an unsupported file format |
||
151 | */ |
||
152 | private function getParser($ext = null) |
||
176 | |||
177 | 18 | /** |
|
178 | * Gets an array of paths |
||
179 | * |
||
180 | 27 | * @param array $path Path to analyze and handle |
|
181 | 6 | * @return array |
|
182 | * @throws FileNotFoundException If a file is not found in `$path` |
||
183 | */ |
||
184 | 21 | private function pathFromArray($path) |
|
214 | |||
215 | /** |
||
216 | * Checks `$path` to see if it is either an array, a directory, or a file |
||
217 | * |
||
218 | * @param string|array $path Path to analyze and handle |
||
219 | * @return array |
||
220 | * @throws EmptyDirectoryException If `$path` is an empty directory |
||
221 | * @throws FileNotFoundException If a file is not found at `$path` |
||
222 | */ |
||
223 | private function getValidPath($path) |
||
248 | 3 | ||
249 | /** |
||
250 | * @return string |
||
251 | * @codeCoverageIgnore |
||
252 | 24 | * @since 0.1.2 |
|
253 | 9 | */ |
|
254 | public function __toString() |
||
258 | } |
||
259 | |||
261 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.