View source
<?php
namespace Drupal\potx\Commands;
use Drush\Commands\DrushCommands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
class PotxCommands extends DrushCommands {
public function potx($mode = NULL, array $options = [
'modules' => NULL,
'files' => NULL,
'folder' => NULL,
'api' => NULL,
'language' => NULL,
'translations' => FALSE,
]) {
include_once __DIR__ . '/../../potx.inc';
include_once __DIR__ . '/../../potx.local.inc';
$files = [];
$build_mode = POTX_BUILD_SINGLE;
if (!is_null($mode) && in_array($mode, [
'core',
'multiple',
'single',
])) {
$build_mode = constant('POTX_BUILD_' . strtoupper($mode));
}
potx_status('set', POTX_STATUS_SILENT);
$modules_option = $options['modules'];
$files_option = $options['files'];
$folder_option = $options['folder'];
$api_option = $options['api'];
if (empty($api_option) || !in_array($api_option, [
5,
6,
7,
8,
])) {
$api_option = POTX_API_CURRENT;
}
$language_option = $options['language'];
if (!empty($language_option) && !in_array($language_option, array_keys(\Drupal::languageManager()
->getLanguages()))) {
$language_option = NULL;
}
$translations_option = (bool) $options['translations'];
potx_local_init($folder_option);
if (!empty($modules_option)) {
$modules = explode(',', $modules_option);
foreach ($modules as $module) {
$files = array_merge($files, _potx_explore_dir(drupal_get_path('module', $module) . '/', '*', $api_option, TRUE));
}
}
elseif (!empty($files_option)) {
$files = explode(',', $files_option);
}
elseif (!empty($folder_option)) {
$files = _potx_explore_dir($folder_option, '*', $api_option, TRUE);
}
else {
$files = _potx_explore_dir(getcwd() . '/', '*', $api_option, TRUE);
}
foreach ($files as $file) {
$this
->output()
->writeln(dt("Processing {$file}..."));
_potx_process_file($file, 0, '_potx_save_string', '_potx_save_version', $api_option);
}
potx_finish_processing('_potx_save_string', $api_option);
$template_export_langcode = $language_option;
$translation_export_langcode = $translations_option && $language_option ? $language_option : NULL;
_potx_build_files(POTX_STRING_RUNTIME, $build_mode, 'general', '_potx_save_string', '_potx_save_version', '_potx_get_header', $template_export_langcode, $translation_export_langcode, $api_option);
_potx_build_files(POTX_STRING_INSTALLER, POTX_BUILD_SINGLE, 'installer', '_potx_save_string', '_potx_save_version', '_potx_get_header', $template_export_langcode, $translation_export_langcode, $api_option);
_potx_write_files();
$this
->output()
->writeln("");
$errors = potx_status('get');
$strings = _potx_save_string(NULL, NULL, NULL, 0, POTX_STRING_RUNTIME);
$rows[] = [
'files' => count($files),
'strings' => count($strings),
'errors' => count($errors),
];
if (!empty($errors)) {
$this
->output()
->writeln(dt("Errors"));
foreach ($errors as $error) {
$this
->logger()
->error($error);
}
}
return new RowsOfFields($rows);
}
}