-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from markdegrootnl/writer
Support for writing Config back to a file
- Loading branch information
Showing
19 changed files
with
913 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
language: php | ||
dist: trusty | ||
|
||
php: | ||
- 5.5 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Noodlehaus\Exception; | ||
|
||
use Noodlehaus\ErrorException; | ||
|
||
class WriteException extends ErrorException | ||
{ | ||
public function __construct(array $error) | ||
{ | ||
$message = isset($error['message']) ? $error['message'] : 'There was an error writing the file'; | ||
$code = isset($error['code']) ? $error['code'] : 0; | ||
$severity = isset($error['type']) ? $error['type'] : 1; | ||
$filename = isset($error['file']) ? $error['file'] : __FILE__; | ||
$exception = isset($error['exception']) ? $error['exception'] : null; | ||
|
||
parent::__construct($message, $code, $severity, $filename, $exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Noodlehaus\Writer; | ||
|
||
use Noodlehaus\Exception\WriteException; | ||
|
||
/** | ||
* Base Writer. | ||
* | ||
* @package Config | ||
* @author Jesus A. Domingo <[email protected]> | ||
* @author Hassan Khan <[email protected]> | ||
* @author Filip Š <[email protected]> | ||
* @author Mark de Groot <[email protected]> | ||
* @link https://github.com/noodlehaus/config | ||
* @license MIT | ||
*/ | ||
abstract class AbstractWriter implements WriterInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function toFile($config, $filename) | ||
{ | ||
$contents = $this->toString($config); | ||
$success = @file_put_contents($filename, $contents); | ||
if ($success === false) { | ||
throw new WriteException(['file' => $filename]); | ||
} | ||
|
||
return $contents; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Noodlehaus\Writer; | ||
|
||
/** | ||
* Ini Writer. | ||
* | ||
* @package Config | ||
* @author Jesus A. Domingo <[email protected]> | ||
* @author Hassan Khan <[email protected]> | ||
* @author Filip Š <[email protected]> | ||
* @author Mark de Groot <[email protected]> | ||
* @link https://github.com/noodlehaus/config | ||
* @license MIT | ||
*/ | ||
class Ini extends AbstractWriter | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* Writes an array to a Ini string. | ||
*/ | ||
public function toString($config, $pretty = true) | ||
{ | ||
return $this->toINI($config); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSupportedExtensions() | ||
{ | ||
return ['ini', 'properties']; | ||
} | ||
|
||
/** | ||
* Converts array to INI string. | ||
* @param array $arr Array to be converted | ||
* @param array $parent Parent array | ||
* | ||
* @return string Converted array as INI | ||
* | ||
* @see https://stackoverflow.com/a/17317168/6523409/ | ||
*/ | ||
protected function toINI(array $arr, array $parent = []) | ||
{ | ||
$converted = ''; | ||
|
||
foreach ($arr as $k => $v) { | ||
if (is_array($v)) { | ||
$sec = array_merge((array) $parent, (array) $k); | ||
$converted .= '['.implode('.', $sec).']'.PHP_EOL; | ||
$converted .= $this->toINI($v, $sec); | ||
} else { | ||
$converted .= $k.'='.$v.PHP_EOL; | ||
} | ||
} | ||
|
||
return $converted; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Noodlehaus\Writer; | ||
|
||
use Noodlehaus\Exception\WriteException; | ||
|
||
/** | ||
* JSON Writer. | ||
* | ||
* @package Config | ||
* @author Jesus A. Domingo <[email protected]> | ||
* @author Hassan Khan <[email protected]> | ||
* @author Filip Š <[email protected]> | ||
* @author Mark de Groot <[email protected]> | ||
* @link https://github.com/noodlehaus/config | ||
* @license MIT | ||
*/ | ||
class Json extends AbstractWriter | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* Writes an array to a JSON file. | ||
*/ | ||
public function toFile($config, $filename) | ||
{ | ||
$data = $this->toString($config); | ||
$success = @file_put_contents($filename, $data.PHP_EOL); | ||
if ($success === false) { | ||
throw new WriteException(['file' => $filename]); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* Writes an array to a JSON string. | ||
*/ | ||
public function toString($config, $pretty = true) | ||
{ | ||
return json_encode($config, $pretty ? (JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) : 0); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSupportedExtensions() | ||
{ | ||
return ['json']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Noodlehaus\Writer; | ||
|
||
use Noodlehaus\Exception\WriteException; | ||
|
||
/** | ||
* Config file parser interface. | ||
* | ||
* @package Config | ||
* @author Jesus A. Domingo <[email protected]> | ||
* @author Hassan Khan <[email protected]> | ||
* @author Filip Š <[email protected]> | ||
* @author Mark de Groot <[email protected]> | ||
* @link https://github.com/noodlehaus/config | ||
* @license MIT | ||
*/ | ||
interface WriterInterface | ||
{ | ||
/** | ||
* Writes a configuration from `$config` to `$filename`. | ||
* | ||
* @param array $config | ||
* @param string $filename | ||
* | ||
* @throws WriteException if the data could not be written to the file | ||
* | ||
* @return array | ||
*/ | ||
public function toFile($config, $filename); | ||
|
||
/** | ||
* Writes a configuration from `$config` to a string. | ||
* | ||
* @param array $config | ||
* @param bool $pretty | ||
* | ||
* @return array | ||
*/ | ||
public function toString($config, $pretty = true); | ||
|
||
/** | ||
* Returns an array of allowed file extensions for this writer. | ||
* | ||
* @return array | ||
*/ | ||
public static function getSupportedExtensions(); | ||
} |
Oops, something went wrong.