o2system /
filesystem
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Filesystem\Files; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use O2System\Filesystem\File; |
||
| 19 | use O2System\Filesystem\Files\Abstracts\AbstractFile; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Class IniFile |
||
| 23 | * |
||
| 24 | * @package O2System\Filesystem\Factory |
||
| 25 | */ |
||
| 26 | class IniFile extends AbstractFile |
||
| 27 | {
|
||
| 28 | protected $fileExtension = '.ini'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * IniFile::readFile |
||
| 32 | * |
||
| 33 | * @param string $filePath Path to the file. |
||
| 34 | * @param array $options Read file options. |
||
| 35 | * |
||
| 36 | * @return mixed |
||
| 37 | */ |
||
| 38 | public function readFile($filePath = null, array $options = []) |
||
| 39 | {
|
||
| 40 | $items = parse_ini_file( |
||
| 41 | $filePath, |
||
| 42 | (empty($options) |
||
| 43 | ? true |
||
| 44 | : $options[ 'sections' ]) |
||
| 45 | ); |
||
| 46 | |||
| 47 | $this->merge($items); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 48 | |||
| 49 | return $items; |
||
| 50 | } |
||
| 51 | |||
| 52 | // ------------------------------------------------------------------------ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * IniFile::writeFile |
||
| 56 | * |
||
| 57 | * @param string $filePath Path to the file. |
||
| 58 | * @param array $options Write file options. |
||
| 59 | * |
||
| 60 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 61 | */ |
||
| 62 | public function writeFile($filePath = null, array $options = []) |
||
| 63 | {
|
||
| 64 | $filePath = empty($filePath) |
||
| 65 | ? $this->filePath |
||
| 66 | : $filePath; |
||
| 67 | |||
| 68 | $sections = (empty($options) |
||
| 69 | ? true |
||
| 70 | : $options[ 'sections' ]); |
||
| 71 | |||
| 72 | $content = null; |
||
| 73 | |||
| 74 | if ($sections) {
|
||
| 75 | foreach ($this->getArrayCopy() as $section => $data) {
|
||
| 76 | if (is_array($data)) {
|
||
| 77 | $content .= '[' . $section . ']' . PHP_EOL; |
||
| 78 | |||
| 79 | foreach ($data as $key => $value) {
|
||
| 80 | if (is_array($value)) {
|
||
| 81 | foreach ($value as $valueChild) {
|
||
| 82 | $content .= $key . '[] = ' . (is_numeric($valueChild) |
||
| 83 | ? $valueChild |
||
| 84 | : '"' . $valueChild . '"') . PHP_EOL; |
||
| 85 | } |
||
| 86 | } elseif (strpos($key, ';') !== false) {
|
||
| 87 | $content .= '; ' . trim(ucfirst($key), ';') . ' ' . $value . PHP_EOL; |
||
| 88 | } elseif (empty($value)) {
|
||
| 89 | $content .= $key . ' = ' . PHP_EOL; |
||
| 90 | } else {
|
||
| 91 | $content .= $key . ' = ' . (is_numeric($value) |
||
| 92 | ? $value |
||
| 93 | : '"' . $value . '"') . PHP_EOL; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | $content .= PHP_EOL; |
||
| 98 | } elseif (strpos($section, ';') !== false) {
|
||
| 99 | $content .= '; ' . trim(ucfirst($section), ';') . ' ' . $data . PHP_EOL; |
||
| 100 | } elseif (empty($data)) {
|
||
| 101 | $content .= $section . ' = ' . PHP_EOL; |
||
| 102 | } else {
|
||
| 103 | $content .= $section . ' = ' . (is_numeric($data) |
||
| 104 | ? $data |
||
| 105 | : '"' . $data . '"') . PHP_EOL; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } else {
|
||
| 109 | foreach ($this->getArrayCopy() as $key => $value) {
|
||
| 110 | if (is_array($value)) {
|
||
| 111 | foreach ($value as $valueChild) {
|
||
| 112 | $content .= $key . '[] = ' . (is_numeric($valueChild) |
||
| 113 | ? $valueChild |
||
| 114 | : '"' . $valueChild . '"') . PHP_EOL; |
||
| 115 | } |
||
| 116 | } elseif (strpos($key, ';') !== false) {
|
||
| 117 | $content .= '; ' . trim(ucfirst($key), ';') . ' ' . $value . PHP_EOL; |
||
| 118 | } elseif (empty($value)) {
|
||
| 119 | $content .= $key . ' = ' . PHP_EOL; |
||
| 120 | } else {
|
||
| 121 | $content .= $key . ' = ' . (is_numeric($value) |
||
| 122 | ? $value |
||
| 123 | : '"' . $value . '"') . PHP_EOL; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | return (new File())->write($filePath, $content); |
||
| 129 | } |
||
| 130 | } |