|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ConfigWriter\Writers; |
| 4 | + |
| 5 | +use ConfigWriter\Exceptions\WriteException; |
| 6 | +use ConfigWriter\AbstractConfig; |
| 7 | +use ConfigWriter\Record; |
| 8 | + |
| 9 | +/** |
| 10 | + * Configuration writer for PHP. |
| 11 | + * |
| 12 | + * @since 2.0.0 |
| 13 | + * |
| 14 | + * @author Filip Š <[email protected]> |
| 15 | + * |
| 16 | + * @license MIT |
| 17 | + * |
| 18 | + * @package ConfigWriter |
| 19 | + */ |
| 20 | +class PhpWriter implements WriterInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Stores default configuration options. |
| 24 | + * |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + protected $options = [ |
| 28 | + 'indentation' => ' ', |
| 29 | + 'eol' => "\n", |
| 30 | + 'boolean' => [ |
| 31 | + 'true' => 'true', |
| 32 | + 'false' => 'false', |
| 33 | + ], |
| 34 | + 'string' => [ 'quotes' => '\'' ], |
| 35 | + 'array' => [ |
| 36 | + 'open' => '[', |
| 37 | + 'close' => ']', |
| 38 | + 'comma' => true, |
| 39 | + ], |
| 40 | + 'comments' => [ |
| 41 | + 'section' => 'c-style', |
| 42 | + 'record' => 'c-style', |
| 43 | + ], |
| 44 | + ]; |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns parsable string representation of a variable. |
| 48 | + * |
| 49 | + * @param mixed $variable Variable for exporting |
| 50 | + * @param array $options Options for exporting |
| 51 | + * * indentation: (string, default) - Indenttion string |
| 52 | + * * eol: \n (string, default) - End of line string |
| 53 | + * * boolean (array) |
| 54 | + * * true: true (string, default) - The true keyword |
| 55 | + * * false: false (string, default) - The false keyword |
| 56 | + * * string (array) |
| 57 | + * * quotes: ' (string, default) - The string quotes |
| 58 | + * * array (array) |
| 59 | + * * open: [ (string, default) - The array open tag |
| 60 | + * * close: ] (string, default) - The array close tag |
| 61 | + * * comma: true (boolean, default) - Comma at the end of array |
| 62 | + * * comments (array) |
| 63 | + * * section: c-style ((c-style, perl-style, multi-line, doc-block), default) - The comment style for sections |
| 64 | + * * record: c-style ((c-style, perl-style, multi-line, doc-block), default) - The comment style for records |
| 65 | + * @param string $level Indentation level |
| 66 | + * @param boolean $comma If comma should be added to result |
| 67 | + * @param string $section Comment of section |
| 68 | + * |
| 69 | + * @return string Exported data |
| 70 | + */ |
| 71 | + protected function export($variable, $options = [], $level = 0, $comma = true, $section = '') |
| 72 | + { |
| 73 | + if ($variable instanceof AbstractConfig) { |
| 74 | + $value = $variable->data; |
| 75 | + $comment = $variable->comment; |
| 76 | + } elseif ($variable instanceof Record) { |
| 77 | + $value = $variable->value; |
| 78 | + $comment = $variable->comment; |
| 79 | + } else { |
| 80 | + $value = $variable; |
| 81 | + $comment = null; |
| 82 | + } |
| 83 | + |
| 84 | + switch (gettype($value)) { |
| 85 | + case 'boolean': |
| 86 | + $result = $value |
| 87 | + ? $options['boolean']['true'] |
| 88 | + : $options['boolean']['false']; |
| 89 | + |
| 90 | + $result .= $comma |
| 91 | + ? ',' |
| 92 | + : ''; |
| 93 | + |
| 94 | + $result .= $comment |
| 95 | + ? ' ' . $this->comment($comment, $options['comments']['record']) |
| 96 | + : ''; |
| 97 | + |
| 98 | + return $result; |
| 99 | + |
| 100 | + case 'string': |
| 101 | + if ($section) { |
| 102 | + $result = $this->comment($section, $options['comments']['section']) |
| 103 | + . $options['eol'] |
| 104 | + . str_repeat($options['indentation'], $level); |
| 105 | + } else { |
| 106 | + $result = ''; |
| 107 | + } |
| 108 | + |
| 109 | + $result .= $options['string']['quotes'] |
| 110 | + . addcslashes($value, "\\\$\"\r\n\t\v\f") |
| 111 | + . $options['string']['quotes']; |
| 112 | + |
| 113 | + $result .= $comma |
| 114 | + ? ',' |
| 115 | + : ''; |
| 116 | + |
| 117 | + if (!$variable instanceof AbstractConfig && $comment) { |
| 118 | + $result .= $comment |
| 119 | + ? ' ' . $this->comment($comment, $options['comments']['record']) |
| 120 | + : ''; |
| 121 | + } |
| 122 | + |
| 123 | + return $result; |
| 124 | + |
| 125 | + case 'array': |
| 126 | + $indexed = array_keys($value) === range(0, (count($value) - 1)); |
| 127 | + $keys = array_keys($value); |
| 128 | + $last = end($keys); |
| 129 | + $eol = $options['eol']; |
| 130 | + |
| 131 | + $arr = []; |
| 132 | + foreach ($value as $key => $val) { |
| 133 | + if ($key !== $last || $options['array']['comma'] !== false) { |
| 134 | + $comma = true; |
| 135 | + } else { |
| 136 | + $comma = false; |
| 137 | + } |
| 138 | + |
| 139 | + $arr[] = str_repeat($options['indentation'], $level) |
| 140 | + . ( |
| 141 | + $indexed ? '' : $this->export( |
| 142 | + $key, |
| 143 | + $options, |
| 144 | + $level, |
| 145 | + false, |
| 146 | + ($val instanceof AbstractConfig ? $val->comment : '') |
| 147 | + ) . ' => ' |
| 148 | + ) |
| 149 | + . $this->export($val, $options, ($level + 1), true); |
| 150 | + } |
| 151 | + |
| 152 | + return '[' |
| 153 | + . $eol |
| 154 | + . implode( |
| 155 | + $eol, |
| 156 | + $arr |
| 157 | + ) |
| 158 | + . $eol |
| 159 | + . str_repeat($options['indentation'], ($level - 1)) |
| 160 | + . ']' |
| 161 | + . ( |
| 162 | + $options['array']['comma'] && !is_array($variable) |
| 163 | + ? ',' |
| 164 | + : '' |
| 165 | + ); |
| 166 | + |
| 167 | + default: |
| 168 | + $result = var_export($value, true); |
| 169 | + |
| 170 | + $result .= $comma |
| 171 | + ? ',' |
| 172 | + : ''; |
| 173 | + |
| 174 | + $result .= $comment |
| 175 | + ? ' ' . $this->comment($comment, $options['comments']['record']) |
| 176 | + : ''; |
| 177 | + |
| 178 | + return $result; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Returns comment of chosen style. |
| 184 | + * |
| 185 | + * @param string $data Comment data. |
| 186 | + * @param array $style Comment style. |
| 187 | + * |
| 188 | + * @return string Styled comment |
| 189 | + */ |
| 190 | + protected function comment($data, $style) |
| 191 | + { |
| 192 | + if ($style === 'c-style') { |
| 193 | + $format = '// %s'; |
| 194 | + } elseif ($style === 'perl-style') { |
| 195 | + $format = '# %s'; |
| 196 | + } elseif ($style === 'multi-line') { |
| 197 | + $format = '/* %s */'; |
| 198 | + } elseif ($style === 'doc-block') { |
| 199 | + $format = '/** %s */'; |
| 200 | + } else { |
| 201 | + $format = '// %s'; |
| 202 | + } |
| 203 | + |
| 204 | + return sprintf($format, $data); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * {@inheritDoc} |
| 209 | + * |
| 210 | + * Returns configuration as PHP array. |
| 211 | + */ |
| 212 | + public function write(AbstractConfig $config, $options = []) |
| 213 | + { |
| 214 | + $options = array_replace_recursive($this->options, $options); |
| 215 | + $eol = $options['eol']; |
| 216 | + |
| 217 | + $converted = '<?php' . $eol . $eol; |
| 218 | + $converted .= (!empty($config->comment) ? $config->comment . $eol . $eol : ''); |
| 219 | + $converted .= 'return '; |
| 220 | + $converted .= $this->export($config->data, $options, 1); |
| 221 | + $converted .= ';' . $eol; |
| 222 | + |
| 223 | + return $converted; |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * {@inheritDoc} |
| 228 | + */ |
| 229 | + public static function getSupportedExtensions() |
| 230 | + { |
| 231 | + return ['php']; |
| 232 | + } |
| 233 | +} |
0 commit comments