-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathMakePhpCommand.php
More file actions
109 lines (92 loc) · 3.06 KB
/
MakePhpCommand.php
File metadata and controls
109 lines (92 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace WP_CLI\I18n;
use DirectoryIterator;
use Gettext\Translations;
use IteratorIterator;
use SplFileInfo;
use WP_CLI;
use WP_CLI\Utils;
use WP_CLI_Command;
class MakePhpCommand extends WP_CLI_Command {
use JsStringFilterTrait;
/**
* Create PHP files from PO files.
*
* ## OPTIONS
*
* <source>
* : Path to an existing PO file or a directory containing multiple PO files.
*
* [<destination>]
* : Path to the destination directory for the resulting PHP files. Defaults to the source directory.
*
* [--pretty-print]
* : Pretty-print resulting PHP files.
*
* ## EXAMPLES
*
* # Create PHP files for all PO files in the current directory.
* $ wp i18n make-php .
* Success: Created 3 files.
*
* # Create a PHP file from a single PO file in a specific directory.
* $ wp i18n make-php example-plugin-de_DE.po languages
* Success: Created 1 file.
*
* # Create a pretty-printed PHP file.
* $ wp i18n make-php example-plugin-de_DE.po languages --pretty-print
* Success: Created 1 file.
*
* @param array<string> $args Command arguments.
* @param array<mixed> $assoc_args Associative arguments.
* @return void
*
* @when before_wp_load
*
* @throws WP_CLI\ExitException
*/
public function __invoke( $args, $assoc_args ) {
$source = realpath( $args[0] );
if ( ! $source || ( ! is_file( $source ) && ! is_dir( $source ) ) ) {
WP_CLI::error( 'Source file or directory does not exist.' );
}
$destination = is_file( $source ) ? dirname( $source ) : $source;
if ( isset( $args[1] ) ) {
$destination = $args[1];
}
if ( ! is_dir( $destination ) && ! mkdir( $destination, 0777, true ) ) {
WP_CLI::error( 'Could not create destination directory.' );
}
if ( is_file( $source ) ) {
$files = [ new SplFileInfo( $source ) ];
} else {
$files = new IteratorIterator( new DirectoryIterator( $source ) );
}
/** @var array<string, bool|string> $assoc_args_simple */
$assoc_args_simple = $assoc_args;
$pretty_print = Utils\get_flag_value( $assoc_args_simple, 'pretty-print', false );
$result_count = 0;
/** @var DirectoryIterator $file */
foreach ( $files as $file ) {
if ( 'po' !== $file->getExtension() ) {
continue;
}
if ( ! $file->isFile() || ! $file->isReadable() ) {
WP_CLI::warning( sprintf( 'Could not read file %s', $file->getFilename() ) );
continue;
}
$file_basename = basename( $file->getFilename(), '.po' );
$destination_file = "{$destination}/{$file_basename}.l10n.php";
$translations = Translations::fromPoFile( $file->getPathname() );
// Remove JS-only strings from PHP files to keep them small.
$this->remove_js_only_strings( $translations );
$options = [ 'prettyPrint' => $pretty_print ];
if ( ! PhpArrayGenerator::toFile( $translations, $destination_file, $options ) ) {
WP_CLI::warning( sprintf( 'Could not create file %s', $destination_file ) );
continue;
}
++$result_count;
}
WP_CLI::success( sprintf( 'Created %d %s.', $result_count, Utils\pluralize( 'file', $result_count ) ) );
}
}