function _potx_parse_php_file in Translation template extractor 8
Parse a PHP file for translatables.
2 calls to _potx_parse_php_file()
- PotxTest::parsePhpContent in tests/
src/ Kernel/ PotxTest.php - Parse the given file with the given API version.
- _potx_process_file in ./
potx.inc - Process a file and put extracted information to the given parameters.
File
- ./
potx.inc, line 216 - Extraction API used by the web and command line interface.
Code
function _potx_parse_php_file($code, $file_name, $save_callback, $name_parts, $basename, $api_version) {
global $_potx_tokens, $_potx_lookup;
$constraint_extract = FALSE;
if (substr($name_parts['filename'], -10) == 'Constraint' && $api_version > POTX_API_7) {
$constraint_extract = TRUE;
}
// Extract raw PHP language tokens.
$raw_tokens = token_get_all($code);
unset($code);
// Remove whitespace and possible HTML (the later in templates for example),
// count line numbers so we can include them in the output.
$_potx_tokens = [];
$_potx_lookup = [];
$token_number = 0;
$line_number = 1;
foreach ($raw_tokens as $token) {
if (!is_array($token) || $token[0] != T_WHITESPACE && $token[0] != T_INLINE_HTML) {
if (is_array($token)) {
$token[] = $line_number;
$constraint_match = $constraint_extract && $token[0] == T_VARIABLE && strlen($token[1]) >= 7 && substr_compare($token[1], 'message', -7, 7, TRUE) === 0;
// Fill array for finding token offsets quickly.
if (in_array($token[0], [
T_STRING,
T_DOC_COMMENT,
]) || $token[0] == T_VARIABLE && $token[1] == '$t' || $constraint_match || $token[0] == T_CONSTANT_ENCAPSED_STRING && ($token[1] == "'#template'" || $token[1] == '"#template"')) {
// Give doc comments a specific key because their content varies.
$key = $token[0] == T_DOC_COMMENT ? 'T_DOC_COMMENT' : ($constraint_match ? 'T_POTX_CONSTRAINT' : $token[1]);
// Normalise "#template" token to support both single-quoted and
// double-quoted #template keys.
if ($key == '"#template"') {
$key = "'#template'";
}
if (!isset($_potx_lookup[$key])) {
$_potx_lookup[$key] = [];
}
$_potx_lookup[$key][] = $token_number;
}
}
$_potx_tokens[] = $token;
$token_number++;
}
// Collect line numbers.
if (is_array($token)) {
$line_number += count(explode("\n", $token[1])) - 1;
}
else {
$line_number += count(explode("\n", $token)) - 1;
}
}
unset($raw_tokens);
// Regular t() calls with different usages.
if ($api_version > POTX_API_6) {
// Drupal 7 onwards supports context on t().
_potx_find_t_calls_with_context($file_name, $save_callback);
if ($api_version < POTX_API_8) {
// st() and $t() are supported up to Drupal 7.
_potx_find_t_calls_with_context($file_name, $save_callback, '$t', POTX_STRING_BOTH);
_potx_find_t_calls_with_context($file_name, $save_callback, 'st', POTX_STRING_INSTALLER);
}
else {
// TranslatableMarkup (and deprecated TranslationWrapper) added in
// Drupal 8.
_potx_find_t_calls_with_context($file_name, $save_callback, 'TranslatableMarkup');
_potx_find_t_calls_with_context($file_name, $save_callback, 'TranslationWrapper');
}
}
else {
// Context-less API up to Drupal 6.
_potx_find_t_calls($file_name, $save_callback);
_potx_find_t_calls($file_name, $save_callback, '$t', POTX_STRING_BOTH);
_potx_find_t_calls($file_name, $save_callback, 'st', POTX_STRING_INSTALLER);
}
if ($api_version < POTX_API_8) {
// This does not support context even in Drupal 7.
_potx_find_t_calls($file_name, $save_callback, '_locale_import_message', POTX_STRING_BOTH);
}
// dt() in drush does not support context either.
_potx_find_t_calls($file_name, $save_callback, 'dt');
if ($api_version > POTX_API_5) {
// Watchdog calls have both of their arguments translated from Drupal 6.x.
if ($api_version < POTX_API_8) {
_potx_find_watchdog_calls($file_name, $save_callback);
}
if ($api_version > POTX_API_7) {
// Logging calls may use a colorful set of methods now.
_potx_find_t_calls($file_name, $save_callback, 'debug');
_potx_find_t_calls($file_name, $save_callback, 'info');
_potx_find_t_calls($file_name, $save_callback, 'notice');
_potx_find_t_calls($file_name, $save_callback, 'warning');
_potx_find_t_calls($file_name, $save_callback, 'error');
_potx_find_t_calls($file_name, $save_callback, 'critical');
_potx_find_t_calls($file_name, $save_callback, 'alert');
_potx_find_t_calls($file_name, $save_callback, 'emergency');
_potx_find_log_calls($file_name, $save_callback);
}
}
else {
// Watchdog calls only have their first argument translated in Drupal 5.x
// and before.
_potx_find_t_calls($file_name, $save_callback, 'watchdog');
}
// Plurals need unique parsing.
if ($api_version < POTX_API_8) {
// format_plural() is removed in Drupal 8.
_potx_find_format_plural_calls($file_name, $save_callback, 'format_plural', $api_version);
}
// Support for formatPlural() calls in Drupal 8+.
if ($api_version > POTX_API_7) {
_potx_find_format_plural_calls($file_name, $save_callback, 'formatPlural', $api_version);
_potx_find_format_plural_calls($file_name, $save_callback, 'PluralTranslatableMarkup', $api_version);
}
if ($name_parts['extension'] == 'module') {
if ($api_version < POTX_API_7) {
_potx_find_perm_hook($file_name, $name_parts['filename'], $save_callback);
}
if ($api_version > POTX_API_5) {
// @todo: if tabs are not defined on the menu hook anymore, exclude this for 8.
_potx_find_menu_hooks($file_name, $name_parts['filename'], $save_callback);
}
}
// Support @Translation annotation (in doc comments) on Drupal 8+.
if ($api_version > POTX_API_7) {
_potx_find_translation_annotations($file_name, $save_callback);
}
if ($constraint_extract) {
_potx_find_constraint_messages($file_name, $save_callback);
}
if ($api_version > POTX_API_7) {
_potx_process_inline_templates($file_name, $save_callback);
}
// Special handling of some Drupal core files.
if ($api_version < POTX_API_8 && ($basename == 'locale.inc' && $api_version < POTX_API_7 || $basename == 'iso.inc')) {
_potx_find_language_names($file_name, $save_callback, $api_version);
}
elseif ($api_version > POTX_API_7 && $basename == 'LanguageManager.php') {
_potx_find_language_names($file_name, $save_callback, $api_version);
}
elseif ($basename == 'locale.module') {
// Applies to all Drupal versions, yay!
_potx_add_date_strings($file_name, $save_callback, $api_version);
}
elseif ($basename == 'common.inc') {
// Applies to all Drupal versions, yay!
_potx_add_format_interval_strings($file_name, $save_callback, $api_version);
}
elseif ($basename == 'system.module') {
// Applies to all Drupal versions, yay!
_potx_add_default_region_names($file_name, $save_callback, $api_version);
}
elseif ($basename == 'user.module' && $api_version < POTX_API_8) {
// Save default user role names (up to Drupal 7).
$save_callback('anonymous user', POTX_CONTEXT_NONE, $file_name);
$save_callback('authenticated user', POTX_CONTEXT_NONE, $file_name);
if ($api_version > POTX_API_6) {
// Administator role is included by default from Drupal 7.
$save_callback('administrator', POTX_CONTEXT_NONE, $file_name);
}
}
}