This repository has been archived by the owner on Jun 22, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
173 additions
and
4 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
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,52 @@ | ||
<?php | ||
|
||
namespace Bavix\SDK\FileLoader; | ||
|
||
class IniLoader implements DataInterface | ||
{ | ||
|
||
use DataTrait; | ||
|
||
/** | ||
* @var IniWriter | ||
*/ | ||
protected $writer; | ||
|
||
/** | ||
* @return IniWriter | ||
*/ | ||
protected function writer() | ||
{ | ||
if (!$this->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); | ||
} | ||
|
||
} |
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,118 @@ | ||
<?php | ||
|
||
namespace Bavix\SDK\FileLoader; | ||
|
||
use Bavix\Exceptions\Runtime; | ||
use Bavix\Helpers\File; | ||
|
||
class IniWriter | ||
{ | ||
|
||
/** | ||
* @param string $filename | ||
* @param array $config | ||
* @param string $header | ||
* | ||
* @return int | ||
*/ | ||
public function toFile($filename, array $config, $header = null) | ||
{ | ||
$ini = $this->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; | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -2,8 +2,6 @@ | |
|
||
namespace Bavix\SDK\FileLoader; | ||
|
||
use Bavix\Slice\Slice; | ||
|
||
class PHPLoader implements DataInterface | ||
{ | ||
|
||
|