From 5acd4dcb17647eb2a618f8213d684caba93512b2 Mon Sep 17 00:00:00 2001 From: Babichev Maxim Date: Thu, 12 Oct 2017 10:54:19 +0300 Subject: [PATCH] update fileloader --- src/SDK/FileLoader.php | 1 + src/SDK/FileLoader/IniLoader.php | 52 +++++++++++++ src/SDK/FileLoader/IniWriter.php | 118 ++++++++++++++++++++++++++++++ src/SDK/FileLoader/JSONLoader.php | 4 +- src/SDK/FileLoader/PHPLoader.php | 2 - 5 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 src/SDK/FileLoader/IniLoader.php create mode 100644 src/SDK/FileLoader/IniWriter.php diff --git a/src/SDK/FileLoader.php b/src/SDK/FileLoader.php index 177187e..5cd5ca4 100644 --- a/src/SDK/FileLoader.php +++ b/src/SDK/FileLoader.php @@ -20,6 +20,7 @@ class FileLoader 'yaml' => FileLoader\YamlLoader::class, 'json' => FileLoader\JSONLoader::class, 'xml' => FileLoader\XmlLoader::class, + 'ini' => FileLoader\IniLoader::class, ]; /** diff --git a/src/SDK/FileLoader/IniLoader.php b/src/SDK/FileLoader/IniLoader.php new file mode 100644 index 0000000..6792863 --- /dev/null +++ b/src/SDK/FileLoader/IniLoader.php @@ -0,0 +1,52 @@ +writer) + { + $this->writer = new IniWriter(); + } + + return $this->writer; + } + + /** + * @inheritdoc + */ + public function asArray() + { + if (!$this->data) + { + $this->data = \parse_ini_file($this->path, true); + } + + return $this->data; + } + + /** + * @inheritdoc + */ + public function save($data) + { + $data = $this->_fromArray($data); + + return (int)$this->writer() + ->toFile($this->path, $data); + } + +} diff --git a/src/SDK/FileLoader/IniWriter.php b/src/SDK/FileLoader/IniWriter.php new file mode 100644 index 0000000..1a8a7ab --- /dev/null +++ b/src/SDK/FileLoader/IniWriter.php @@ -0,0 +1,118 @@ +toString($config, $header); + return File::put($filename, $ini); + } + + /** + * @param array $config + * @param null $header + * + * @return string + */ + public function toString(array $config, $header = null) + { + $ini = !empty($header) ? $header . PHP_EOL : ''; + + uasort($config, function ($first, $second) { + if (is_array($first)) { + return 1; + } + + if (is_array($second)) + { + return -1; + } + + return 0; + }); + + $names = array_keys($config); + + foreach ($names as $name) + { + $section = $config[$name]; + + if (!is_array($section)) + { + $ini .= $name . ' = ' . $this->encodeValue($section) . PHP_EOL; + continue; + } + + if (empty($section)) + { + continue; + } + + if (!empty($ini)) + { + $ini .= PHP_EOL; + } + + $ini .= "[$name]" . PHP_EOL; + + foreach ($section as $option => $value) + { + if (is_numeric($option)) + { + $option = $name; + $value = (array)$value; + } + + if (is_array($value)) + { + foreach ($value as $key => $currentValue) + { + $ini .= $option . '[' . $key . '] = ' . $this->encodeValue($currentValue) . PHP_EOL; + } + } + else + { + $ini .= $option . ' = ' . $this->encodeValue($value) . PHP_EOL; + } + } + + $ini .= "\n"; + } + + return $ini; + } + + /** + * @param $value + * + * @return int|string + */ + protected function encodeValue($value) + { + if (is_bool($value)) + { + return (int)$value; + } + + if (is_string($value)) + { + return '"' . $value . '"'; + } + + return $value; + } + +} diff --git a/src/SDK/FileLoader/JSONLoader.php b/src/SDK/FileLoader/JSONLoader.php index 6342008..17badef 100644 --- a/src/SDK/FileLoader/JSONLoader.php +++ b/src/SDK/FileLoader/JSONLoader.php @@ -17,8 +17,8 @@ public function asArray() { if (!$this->data) { - $yml = \file_get_contents($this->path); - $this->data = JSON::decode($yml); + $data = \file_get_contents($this->path); + $this->data = JSON::decode($data); } return $this->data; diff --git a/src/SDK/FileLoader/PHPLoader.php b/src/SDK/FileLoader/PHPLoader.php index e4afd36..2f9dfd3 100644 --- a/src/SDK/FileLoader/PHPLoader.php +++ b/src/SDK/FileLoader/PHPLoader.php @@ -2,8 +2,6 @@ namespace Bavix\SDK\FileLoader; -use Bavix\Slice\Slice; - class PHPLoader implements DataInterface {