Skip to content

Commit f7dd4fb

Browse files
committed
Add basic writer support
1 parent e6ca69c commit f7dd4fb

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

src/Config.php

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace ConfigWriter;
44

5+
use ConfigWriter\Exceptions\UnsupportedFormatException;
6+
use ConfigWriter\Exceptions\WriteException;
7+
use ConfigWriter\Writers\WriterInterface;
8+
59
/**
610
* Configuration class.
711
*
@@ -15,23 +19,83 @@
1519
*/
1620
class Config extends AbstractConfig
1721
{
22+
/**
23+
* Stores the supported writers.
24+
*
25+
* @var array
26+
*/
27+
protected $supportedWriters = [];
28+
1829
/**
1930
* Writes configuration to string.
2031
*
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
2237
*/
23-
public function toString($format)
38+
public function toString(WriterInterface $writer)
2439
{
25-
// TODO: Return configuration as a string.
40+
return $writer->write($this);
2641
}
2742

2843
/**
2944
* Writes configuration to file.
3045
*
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
3253
*/
33-
public function toFile($filename, $format)
54+
public function toFile($filename, WriterInterface $writer = null)
3455
{
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');
36100
}
37101
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ConfigWriter\Exceptions;
4+
5+
use Exception;
6+
7+
class UnsupportedFormatException extends Exception
8+
{
9+
10+
}

src/Exceptions/WriteException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ConfigWriter\Exceptions;
4+
5+
use Exception;
6+
7+
class WriteException extends Exception
8+
{
9+
10+
}

0 commit comments

Comments
 (0)