1 | <?php |
||
22 | class Config extends AbstractConfig |
||
23 | { |
||
24 | /** |
||
25 | * All formats supported by Config. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $supportedParsers = [ |
||
30 | 'Noodlehaus\Parser\Php', |
||
31 | 'Noodlehaus\Parser\Ini', |
||
32 | 'Noodlehaus\Parser\Json', |
||
33 | 'Noodlehaus\Parser\Xml', |
||
34 | 'Noodlehaus\Parser\Yaml' |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * All formats supported by Config. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $supportedWriters = [ |
||
43 | 'Noodlehaus\Writer\Ini', |
||
44 | 'Noodlehaus\Writer\Json', |
||
45 | 3 | 'Noodlehaus\Writer\Xml', |
|
46 | 'Noodlehaus\Writer\Yaml' |
||
47 | 3 | ]; |
|
48 | |||
49 | /** |
||
50 | * Static method for loading a Config instance. |
||
51 | * |
||
52 | * @param string|array $values Filenames or string with configuration |
||
53 | * @param ParserInterface $parser Configuration parser |
||
54 | * @param bool $string Enable loading from string |
||
55 | * |
||
56 | * @return Config |
||
57 | 39 | */ |
|
58 | public static function load($values, $parser = null, $string = false) |
||
62 | 36 | ||
63 | /** |
||
64 | * Loads a Config instance. |
||
65 | 27 | * |
|
66 | 27 | * @param string|array $values Filenames or string with configuration |
|
67 | * @param ParserInterface $parser Configuration parser |
||
68 | * @param bool $string Enable loading from string |
||
69 | */ |
||
70 | public function __construct($values, ParserInterface $parser = null, $string = false) |
||
80 | |||
81 | 30 | /** |
|
82 | 30 | * Loads configuration from file. |
|
83 | * |
||
84 | 27 | * @param string|array $path Filenames or directories with configuration |
|
85 | 27 | * @param ParserInterface $parser Configuration parser |
|
86 | 27 | * |
|
87 | * @throws EmptyDirectoryException If `$path` is an empty directory |
||
88 | */ |
||
89 | 27 | protected function loadFromFile($path, ParserInterface $parser = null) |
|
120 | 3 | ||
121 | /** |
||
122 | * Writes configuration to file. |
||
123 | * |
||
124 | * @param string $filename Filename to save configuration to |
||
125 | * @param WriterInterface $writer Configuration writer |
||
126 | * |
||
127 | * @throws WriteException if the data could not be written to the file |
||
128 | */ |
||
129 | public function toFile($filename, WriterInterface $writer = null) |
||
130 | { |
||
131 | 27 | if ($writer === null) { |
|
132 | // Get file information |
||
133 | 27 | $info = pathinfo($filename); |
|
134 | 27 | $parts = explode('.', $info['basename']); |
|
135 | 21 | $extension = array_pop($parts); |
|
136 | |||
137 | // Skip the `dist` extension |
||
138 | if ($extension === 'dist') { |
||
139 | $extension = array_pop($parts); |
||
140 | 6 | } |
|
141 | |||
142 | // Get file writer |
||
143 | $writer = $this->getWriter($extension); |
||
144 | |||
145 | // Try to save file |
||
146 | $writer->toFile($this->all(), $filename); |
||
147 | |||
148 | // Clean writer |
||
149 | $writer = null; |
||
150 | } else { |
||
151 | // Try to load file using specified writer |
||
152 | 12 | $writer->toFile($this->all(), $filename); |
|
153 | } |
||
154 | 12 | } |
|
155 | |||
156 | 12 | /** |
|
157 | * Loads configuration from string. |
||
158 | * |
||
159 | * @param string $configuration String with configuration |
||
160 | * @param ParserInterface $parser Configuration parser |
||
161 | 12 | */ |
|
162 | 12 | protected function loadFromString($configuration, ParserInterface $parser) |
|
169 | |||
170 | 6 | /** |
|
171 | 3 | * Writes configuration to string. |
|
172 | * |
||
173 | * @param WriterInterface $writer Configuration writer |
||
174 | * @param boolean $pretty Encode pretty |
||
175 | 3 | */ |
|
176 | public function toString(WriterInterface $writer, $pretty = true) |
||
177 | { |
||
178 | return $writer->toString($this->all(), $pretty); |
||
179 | 9 | } |
|
180 | |||
181 | /** |
||
182 | * Gets a parser for a given file extension. |
||
183 | * |
||
184 | * @param string $extension |
||
185 | * |
||
186 | * @return Noodlehaus\Parser\ParserInterface |
||
187 | * |
||
188 | * @throws UnsupportedFormatException If `$extension` is an unsupported file format |
||
189 | */ |
||
190 | protected function getParser($extension) |
||
201 | 33 | ||
202 | 6 | /** |
|
203 | 6 | * Gets a writer for a given file extension. |
|
204 | 3 | * |
|
205 | * @param string $extension |
||
206 | * |
||
207 | 3 | * @return Noodlehaus\Writer\WriterInterface |
|
208 | * |
||
209 | * @throws UnsupportedFormatException If `$extension` is an unsupported file format |
||
210 | */ |
||
211 | 27 | protected function getWriter($extension) |
|
222 | |||
223 | /** |
||
224 | * Gets an array of paths |
||
225 | * |
||
226 | * @param array $path |
||
227 | * |
||
228 | * @return array |
||
229 | * |
||
230 | * @throws FileNotFoundException If a file is not found at `$path` |
||
231 | */ |
||
232 | protected function getPathFromArray($path) |
||
261 | |||
262 | /** |
||
263 | * Checks `$path` to see if it is either an array, a directory, or a file. |
||
264 | * |
||
265 | * @param string|array $path |
||
266 | * |
||
267 | * @return array |
||
268 | * |
||
269 | * @throws EmptyDirectoryException If `$path` is an empty directory |
||
270 | * |
||
271 | * @throws FileNotFoundException If a file is not found at `$path` |
||
272 | */ |
||
273 | protected function getValidPath($path) |
||
297 | } |
||
298 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.