-
Notifications
You must be signed in to change notification settings - Fork 59
Add support for pretty-printing PHP files in wp i18n make-php command #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
d966001
1edfaa0
e2872d2
9c89886
3899f6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| class PhpArrayGenerator extends PhpArray { | ||
| public static $options = [ | ||
| 'includeHeaders' => false, | ||
| 'prettyPrint' => false, | ||
| ]; | ||
|
|
||
| /** | ||
|
|
@@ -22,7 +23,15 @@ class PhpArrayGenerator extends PhpArray { | |
| public static function toString( Translations $translations, array $options = [] ) { | ||
| $array = static::generate( $translations, $options ); | ||
|
|
||
| return '<?php' . PHP_EOL . 'return ' . static::var_export( $array ) . ';'; | ||
| $pretty_print = isset( $options['prettyPrint'] ) ? $options['prettyPrint'] : false; | ||
|
|
||
| if ( true === $pretty_print ) { | ||
| $exported_array = static::pretty_export( $array ); | ||
| } else { | ||
| $exported_array = static::var_export( $array ); | ||
| } | ||
|
|
||
| return '<?php' . PHP_EOL . ' return ' . $exported_array . ';'; | ||
sovetski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -163,4 +172,36 @@ private static function var_export( $value ) { | |
|
|
||
| return '[' . implode( ',', $entries ) . ']'; | ||
| } | ||
|
|
||
| /** | ||
| * Outputs or returns a parsable string representation of a variable. | ||
| * @since 4.0.0 | ||
sovetski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @param mixed $value The variable you want to export. | ||
| * @param int $level The current indentation level. | ||
| * @param int $indentation The number of spaces for indentation. Default is 4. | ||
|
||
| * @return string The variable representation. | ||
| */ | ||
| private static function pretty_export( $values, $indentation = 2 ) { | ||
| $result = '[' . PHP_EOL; | ||
| $indent = str_repeat( ' ', $indentation ); | ||
|
|
||
| foreach ( $values as $key => $value ) { | ||
| $result .= $indent . str_repeat( ' ', $indentation ) . "'$key' => "; | ||
|
|
||
| if ( is_array( $value ) ) { | ||
| $result .= self::pretty_export( $value, $indentation + $indentation ); | ||
| } elseif ( strpos( $value, "\0" ) !== false ) { | ||
| $parts = explode( "\0", $value ); | ||
| $result .= "'" . implode( "' . \"\\0\" . '", array_map( 'addslashes', $parts ) ) . "'"; | ||
| } else { | ||
| $result .= "'$value'"; | ||
| } | ||
|
|
||
| $result .= ',' . PHP_EOL; | ||
| } | ||
|
|
||
| $result .= $indent . ']'; | ||
| return $result; | ||
| } | ||
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.