|
2 | 2 |
|
3 | 3 | namespace ConfigWriter; |
4 | 4 |
|
| 5 | +use ConfigWriter\Exceptions\UnsupportedFormatException; |
| 6 | +use ConfigWriter\Exceptions\WriteException; |
| 7 | +use ConfigWriter\Writers\WriterInterface; |
| 8 | + |
5 | 9 | /** |
6 | 10 | * Configuration class. |
7 | 11 | * |
|
15 | 19 | */ |
16 | 20 | class Config extends AbstractConfig |
17 | 21 | { |
| 22 | + /** |
| 23 | + * Stores the supported writers. |
| 24 | + * |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + protected $supportedWriters = []; |
| 28 | + |
18 | 29 | /** |
19 | 30 | * Writes configuration to string. |
20 | 31 | * |
21 | | - * TODO: Documentation |
| 32 | + * @param WriterInterface $writer Configuration writer |
| 33 | + * |
| 34 | + * @return string Encoded configuration string |
| 35 | + * |
| 36 | + * @throws WriteException If there is an error while writing a string |
22 | 37 | */ |
23 | | - public function toString($format) |
| 38 | + public function toString(WriterInterface $writer) |
24 | 39 | { |
25 | | - // TODO: Return configuration as a string. |
| 40 | + return $writer->write($this); |
26 | 41 | } |
27 | 42 |
|
28 | 43 | /** |
29 | 44 | * Writes configuration to file. |
30 | 45 | * |
31 | | - * TODO: Documentation |
| 46 | + * @param string $filename Configuration file name |
| 47 | + * @param WriterInterface $writer Configuration writer (optional) |
| 48 | + * |
| 49 | + * @return void |
| 50 | + * |
| 51 | + * @throws UnsupportedFormatException If file extension is unsupported |
| 52 | + * @throws WriteException If there is an error while writing a file |
32 | 53 | */ |
33 | | - public function toFile($filename, $format) |
| 54 | + public function toFile($filename, WriterInterface $writer = null) |
34 | 55 | { |
35 | | - // TODO: Write configuration to file using method `toString()`. |
| 56 | + if ($writer === null) { |
| 57 | + // Get file information |
| 58 | + $info = pathinfo($filename); |
| 59 | + $parts = explode('.', $info['basename']); |
| 60 | + $extension = array_pop($parts); |
| 61 | + |
| 62 | + // Skip the `dist` extension |
| 63 | + if ($extension === 'dist') { |
| 64 | + $extension = array_pop($parts); |
| 65 | + } |
| 66 | + |
| 67 | + // Get configuration writer |
| 68 | + $writer = $this->getWriter($extension); |
| 69 | + } |
| 70 | + |
| 71 | + $data = $this->toString($writer); |
| 72 | + |
| 73 | + // @codeCoverageIgnoreStart |
| 74 | + if (!is_dir(dirname($filename))) { |
| 75 | + mkdir(dirname($filename), 0777, true); |
| 76 | + } |
| 77 | + // @codeCoverageIgnoreEnd |
| 78 | + |
| 79 | + file_put_contents($filename, $data); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Gets a writer for a given file extension. |
| 84 | + * |
| 85 | + * @param string $extension File extension |
| 86 | + * |
| 87 | + * @return WriterInterface Writer for a given file extension |
| 88 | + * |
| 89 | + * @throws UnsupportedFormatException If `$extension` is an unsupported file format |
| 90 | + */ |
| 91 | + protected function getWriter($extension) |
| 92 | + { |
| 93 | + foreach ($this->supportedWriters as $writer) { |
| 94 | + if (in_array($extension, $writer::getSupportedExtensions())) { |
| 95 | + return new $writer(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + throw new UnsupportedFormatException('Unsupported configuration format'); |
36 | 100 | } |
37 | 101 | } |
0 commit comments