diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index a27b7d5c..37bf068a 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -265,4 +265,38 @@ static function ( $file, $key, $iterator ) use ( $include, $exclude, $extensions private static function trim_leading_slash( $path ) { return ltrim( $path, '/' ); } + + /** + * Formerly part php-gettext 4.x. + */ + protected static function getFiles($file) + { + if (empty($file)) { + throw new InvalidArgumentException('There is not any file defined'); + } + + if (is_string($file)) { + if (!is_file($file)) { + throw new InvalidArgumentException("'$file' is not a valid file"); + } + + if (!is_readable($file)) { + throw new InvalidArgumentException("'$file' is not a readable file"); + } + + return [$file]; + } + + if (is_array($file)) { + $files = []; + + foreach ($file as $f) { + $files = array_merge($files, self::getFiles($f)); + } + + return $files; + } + + throw new InvalidArgumentException('The first argument must be string or array'); + } } diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 37794185..4191c272 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -6,6 +6,7 @@ use Gettext\Merge; use Gettext\Translation; use Gettext\Translations; +use Gettext\Headers; use Gettext\Utils\ParsedComment; use WP_CLI; use WP_CLI_Command; @@ -402,6 +403,8 @@ function ( $file ) { } } + $this->exceptions = Translations::create(); + if ( isset( $assoc_args['subtract'] ) ) { $this->subtract_and_merge = Utils\get_flag_value( $assoc_args, 'subtract-and-merge', false ); @@ -415,7 +418,7 @@ function ( $file ) { WP_CLI::debug( sprintf( 'Ignoring any string already existing in: %s', $file ), 'make-pot' ); - $this->exceptions[ $file ] = new Translations(); + $this->exceptions[ $file ] = Translations::create(); Po::fromFile( $file, $this->exceptions[ $file ] ); } } @@ -554,11 +557,11 @@ protected function get_file_headers( $type ) { * @return Translations A Translation set. */ protected function extract_strings() { - $translations = new Translations(); + $translations = Translations::create(); // Add existing strings first but don't keep headers. if ( ! empty( $this->merge ) ) { - $existing_translations = new Translations(); + $existing_translations = Translations::create(); Po::fromFile( $this->merge, $existing_translations ); $translations->mergeWith( $existing_translations, Merge::ADD | Merge::REMOVE ); } @@ -568,10 +571,10 @@ protected function extract_strings() { $this->set_default_headers( $translations ); // POT files have no Language header. - $translations->deleteHeader( Translations::HEADER_LANGUAGE ); + $translations->getHeaders()->delete( Headers::HEADER_LANGUAGE ); // Only relevant for PO files, not POT files. - $translations->setHeader( 'PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE' ); + $translations->getHeaders()->set('PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE'); if ( $this->domain ) { $translations->setDomain( $this->domain ); @@ -585,15 +588,15 @@ protected function extract_strings() { continue; } - $translation = new Translation( '', $data ); + $translation = Translation::create( '', $data ); if ( isset( $this->main_file_data['Theme Name'] ) ) { - $translation->addExtractedComment( sprintf( '%s of the theme', $header ) ); + $translation->getComments()->add( sprintf( '%s of the theme', $header ) ); } else { - $translation->addExtractedComment( sprintf( '%s of the plugin', $header ) ); + $translation->getComments()->add( sprintf( '%s of the plugin', $header ) ); } - $translations[] = $translation; + $translations->add($translation); } try { @@ -908,16 +911,16 @@ protected function set_default_headers( $translations ) { } if ( null !== $name ) { - $translations->setHeader( 'Project-Id-Version', $name . ( $version ? ' ' . $version : '' ) ); + $translations->getHeaders()->set('Project-Id-Version', $name . ( $version ? ' ' . $version : '' )); } if ( null !== $bugs_address ) { - $translations->setHeader( 'Report-Msgid-Bugs-To', $bugs_address ); + $translations->getHeaders()->set('Report-Msgid-Bugs-To', $bugs_address); } - $translations->setHeader( 'Last-Translator', 'FULL NAME ' ); - $translations->setHeader( 'Language-Team', 'LANGUAGE ' ); - $translations->setHeader( 'X-Generator', 'WP-CLI ' . WP_CLI_VERSION ); + $translations->getHeaders()->set('Last-Translator', 'FULL NAME '); + $translations->getHeaders()->set('Language-Team', 'LANGUAGE '); + $translations->getHeaders()->set('X-Generator', 'WP-CLI ' . WP_CLI_VERSION); foreach ( $this->headers as $key => $value ) { $translations->setHeader( $key, $value ); diff --git a/src/PhpCodeExtractor.php b/src/PhpCodeExtractor.php index 364afcaf..f8588a81 100644 --- a/src/PhpCodeExtractor.php +++ b/src/PhpCodeExtractor.php @@ -3,11 +3,11 @@ namespace WP_CLI\I18n; use Exception; -use Gettext\Extractors\PhpCode; +use Gettext\Scanner\PhpScanner; use Gettext\Translations; use WP_CLI; -final class PhpCodeExtractor extends PhpCode { +class PhpCodeExtractor { use IterableCodeExtractor; public static $options = [ @@ -43,8 +43,6 @@ final class PhpCodeExtractor extends PhpCode { ], ]; - protected static $functionsScannerClass = 'WP_CLI\I18n\PhpFunctionsScanner'; - /** * {@inheritdoc} */ @@ -52,7 +50,10 @@ public static function fromString( $string, Translations $translations, array $o WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' ); try { - static::fromStringMultiple( $string, [ $translations ], $options ); + $scanner = new PhpScanner( $translations ); + $scanner->setFunctions( self::$options['functions'] ); + $scanner->extractCommentsStartingWith($options['extractComments']); + $scanner->scanString( $string, $options[ 'file' ] ); } catch ( Exception $exception ) { WP_CLI::debug( sprintf( diff --git a/src/PhpFunctionsScanner.php b/src/PhpFunctionsScanner.php deleted file mode 100644 index 9ec67878..00000000 --- a/src/PhpFunctionsScanner.php +++ /dev/null @@ -1,86 +0,0 @@ -getFunctions( $options['constants'] ) as $function ) { - list( $name, $line, $args ) = $function; - - if ( ! isset( $functions[ $name ] ) ) { - continue; - } - - $original = null; - $domain = null; - $context = null; - $plural = null; - - switch ( $functions[ $name ] ) { - case 'text_domain': - case 'gettext': - list( $original, $domain ) = array_pad( $args, 2, null ); - break; - - case 'text_context_domain': - list( $original, $context, $domain ) = array_pad( $args, 3, null ); - break; - - case 'single_plural_number_domain': - list( $original, $plural, $number, $domain ) = array_pad( $args, 4, null ); - break; - - case 'single_plural_number_context_domain': - list( $original, $plural, $number, $context, $domain ) = array_pad( $args, 5, null ); - break; - - case 'single_plural_domain': - list( $original, $plural, $domain ) = array_pad( $args, 3, null ); - break; - - case 'single_plural_context_domain': - list( $original, $plural, $context, $domain ) = array_pad( $args, 4, null ); - break; - - default: - // Should never happen. - \WP_CLI::error( sprintf( "Internal error: unknown function map '%s' for '%s'.", $functions[ $name ], $name ) ); - } - - if ( '' === (string) $original ) { - continue; - } - - if ( $domain !== $translations->getDomain() && null !== $translations->getDomain() ) { - continue; - } - - $translation = $translations->insert( $context, $original, $plural ); - if ( $add_reference ) { - $translation = $translation->addReference( $file, $line ); - } - - if ( isset( $function[3] ) ) { - foreach ( $function[3] as $extracted_comment ) { - $translation = $translation->addExtractedComment( $extracted_comment ); - } - } - } - } -}