Skip to content

Commit 5becb64

Browse files
authored
Merge pull request #458 from wp-cli/copilot/add-pretty-print-arg
2 parents 4055e85 + 87c3201 commit 5becb64

File tree

3 files changed

+80
-10
lines changed

3 files changed

+80
-10
lines changed

features/makephp.feature

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,44 @@ Feature: Generate PHP files from PO files
259259
"""
260260
new message
261261
"""
262+
263+
Scenario: Should create pretty-printed PHP files
264+
Given an empty foo-plugin directory
265+
And a foo-plugin/foo-plugin-de_DE.po file:
266+
"""
267+
# Copyright (C) 2018 Foo Plugin
268+
# This file is distributed under the same license as the Foo Plugin package.
269+
msgid ""
270+
msgstr ""
271+
"Project-Id-Version: Foo Plugin\n"
272+
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
273+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
274+
"Language-Team: LANGUAGE <[email protected]>\n"
275+
"Language: de_DE\n"
276+
"MIME-Version: 1.0\n"
277+
"Content-Type: text/plain; charset=UTF-8\n"
278+
"Content-Transfer-Encoding: 8bit\n"
279+
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
280+
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
281+
"X-Domain: foo-plugin\n"
282+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
283+
284+
#: foo-plugin.php:15
285+
msgid "Foo Plugin"
286+
msgstr "Bar Plugin"
287+
"""
288+
289+
When I run `wp i18n make-php foo-plugin --pretty-print`
290+
Then STDOUT should contain:
291+
"""
292+
Success: Created 1 file.
293+
"""
294+
And the return code should be 0
295+
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
296+
"""
297+
'domain' => 'foo-plugin',
298+
"""
299+
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
300+
"""
301+
'messages' => [
302+
"""

src/MakePhpCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class MakePhpCommand extends WP_CLI_Command {
2222
* [<destination>]
2323
* : Path to the destination directory for the resulting PHP files. Defaults to the source directory.
2424
*
25+
* [--pretty-print]
26+
* : Pretty-print resulting PHP files.
27+
*
2528
* ## EXAMPLES
2629
*
2730
* # Create PHP files for all PO files in the current directory.
@@ -32,6 +35,10 @@ class MakePhpCommand extends WP_CLI_Command {
3235
* $ wp i18n make-php example-plugin-de_DE.po languages
3336
* Success: Created 1 file.
3437
*
38+
* # Create a pretty-printed PHP file.
39+
* $ wp i18n make-php example-plugin-de_DE.po languages --pretty-print
40+
* Success: Created 1 file.
41+
*
3542
* @when before_wp_load
3643
*
3744
* @throws WP_CLI\ExitException
@@ -57,6 +64,8 @@ public function __invoke( $args, $assoc_args ) {
5764
$files = new IteratorIterator( new DirectoryIterator( $source ) );
5865
}
5966

67+
$pretty_print = Utils\get_flag_value( $assoc_args, 'pretty-print', false );
68+
6069
$result_count = 0;
6170
/** @var DirectoryIterator $file */
6271
foreach ( $files as $file ) {
@@ -73,7 +82,8 @@ public function __invoke( $args, $assoc_args ) {
7382
$destination_file = "{$destination}/{$file_basename}.l10n.php";
7483

7584
$translations = Translations::fromPoFile( $file->getPathname() );
76-
if ( ! PhpArrayGenerator::toFile( $translations, $destination_file ) ) {
85+
$options = [ 'prettyPrint' => $pretty_print ];
86+
if ( ! PhpArrayGenerator::toFile( $translations, $destination_file, $options ) ) {
7787
WP_CLI::warning( sprintf( 'Could not create file %s', $destination_file ) );
7888
continue;
7989
}

src/PhpArrayGenerator.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
class PhpArrayGenerator extends PhpArray {
1515
public static $options = [
1616
'includeHeaders' => false,
17+
'prettyPrint' => false,
1718
];
1819

1920
/**
2021
* {@inheritdoc}
2122
*/
2223
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 );
2426

25-
return '<?php' . PHP_EOL . 'return ' . static::var_export( $array ) . ';';
27+
return '<?php' . PHP_EOL . 'return ' . static::var_export( $array, $options['prettyPrint'] ) . ';';
2628
}
2729

2830
/**
@@ -146,22 +148,39 @@ private static function array_is_list( array $arr ) {
146148
*
147149
* @since 4.0.0
148150
*
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).
150154
* @return string The variable representation.
151155
*/
152-
private static function var_export( $value ) {
156+
private static function var_export( $value, $pretty_print = false, $indent_level = 0 ) {
153157
if ( ! is_array( $value ) ) {
154158
return var_export( $value, true );
155159
}
156160

157161
$entries = array();
158-
159162
$is_list = self::array_is_list( $value );
160163

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+
}
164176

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+
}
166185
}
167186
}

0 commit comments

Comments
 (0)