|
14 | 14 | class PhpArrayGenerator extends PhpArray { |
15 | 15 | public static $options = [ |
16 | 16 | 'includeHeaders' => false, |
| 17 | + 'prettyPrint' => false, |
17 | 18 | ]; |
18 | 19 |
|
19 | 20 | /** |
20 | 21 | * {@inheritdoc} |
21 | 22 | */ |
22 | 23 | public static function toString( Translations $translations, array $options = [] ) { |
23 | | - $array = static::generate( $translations, $options ); |
| 24 | + $options = array_merge( static::$options, $options ); |
| 25 | + $array = static::generate( $translations, $options ); |
24 | 26 |
|
25 | | - return '<?php' . PHP_EOL . 'return ' . static::var_export( $array ) . ';'; |
| 27 | + return '<?php' . PHP_EOL . 'return ' . static::var_export( $array, $options['prettyPrint'] ) . ';'; |
26 | 28 | } |
27 | 29 |
|
28 | 30 | /** |
@@ -146,22 +148,39 @@ private static function array_is_list( array $arr ) { |
146 | 148 | * |
147 | 149 | * @since 4.0.0 |
148 | 150 | * |
149 | | - * @param mixed $value The variable you want to export. |
| 151 | + * @param mixed $value The variable you want to export. |
| 152 | + * @param bool $pretty_print Whether to pretty-print the output. |
| 153 | + * @param int $indent_level Current indentation level (used for recursion). |
150 | 154 | * @return string The variable representation. |
151 | 155 | */ |
152 | | - private static function var_export( $value ) { |
| 156 | + private static function var_export( $value, $pretty_print = false, $indent_level = 0 ) { |
153 | 157 | if ( ! is_array( $value ) ) { |
154 | 158 | return var_export( $value, true ); |
155 | 159 | } |
156 | 160 |
|
157 | 161 | $entries = array(); |
158 | | - |
159 | 162 | $is_list = self::array_is_list( $value ); |
160 | 163 |
|
161 | | - foreach ( $value as $key => $val ) { |
162 | | - $entries[] = $is_list ? self::var_export( $val ) : var_export( $key, true ) . '=>' . self::var_export( $val ); |
163 | | - } |
| 164 | + if ( $pretty_print ) { |
| 165 | + $indent = str_repeat( "\t", $indent_level + 1 ); |
| 166 | + $closing_indent = str_repeat( "\t", $indent_level ); |
| 167 | + $separator = ',' . PHP_EOL; |
| 168 | + |
| 169 | + foreach ( $value as $key => $val ) { |
| 170 | + if ( $is_list ) { |
| 171 | + $entries[] = $indent . self::var_export( $val, $pretty_print, $indent_level + 1 ); |
| 172 | + } else { |
| 173 | + $entries[] = $indent . var_export( $key, true ) . ' => ' . self::var_export( $val, $pretty_print, $indent_level + 1 ); |
| 174 | + } |
| 175 | + } |
164 | 176 |
|
165 | | - return '[' . implode( ',', $entries ) . ']'; |
| 177 | + return '[' . PHP_EOL . implode( $separator, $entries ) . ',' . PHP_EOL . $closing_indent . ']'; |
| 178 | + } else { |
| 179 | + foreach ( $value as $key => $val ) { |
| 180 | + $entries[] = $is_list ? self::var_export( $val, $pretty_print, $indent_level ) : var_export( $key, true ) . '=>' . self::var_export( $val, $pretty_print, $indent_level ); |
| 181 | + } |
| 182 | + |
| 183 | + return '[' . implode( ',', $entries ) . ']'; |
| 184 | + } |
166 | 185 | } |
167 | 186 | } |
0 commit comments