Added configuration for headers in PHP files generated by message/extract command (#14417)

This commit is contained in:
Robert Korulczyk
2017-07-10 19:14:54 +02:00
committed by Alexander Makarov
parent a50d9d8e3e
commit d2781cd232
65 changed files with 519 additions and 171 deletions

View File

@ -131,6 +131,25 @@ class MessageController extends \yii\console\Controller
* @see isCategoryIgnored
*/
public $ignoreCategories = [];
/**
* @var string File header in generated PHP file with messages. This property is used only if [[$format]] is "php".
* @since 2.0.13
*/
public $phpFileHeader = '';
/**
* @var string|null DocBlock used for messages array in generated PHP file. If `null`, default DocBlock will be used.
* This property is used only if [[$format]] is "php".
* @since 2.0.13
*/
public $phpDocBlock;
/**
* @var array Config for messages extraction.
* @see actionExtract()
* @see initConfig()
* @since 2.0.13
*/
protected $config;
/**
@ -155,6 +174,8 @@ class MessageController extends \yii\console\Controller
'messageTable',
'catalog',
'ignoreCategories',
'phpFileHeader',
'phpDocBlock',
]);
}
@ -271,81 +292,45 @@ EOD;
*/
public function actionExtract($configFile = null)
{
$configFileContent = [];
if ($configFile !== null) {
$configFile = Yii::getAlias($configFile);
if (!is_file($configFile)) {
throw new Exception("The configuration file does not exist: $configFile");
}
$configFileContent = require $configFile;
}
$this->initConfig($configFile);
$config = array_merge(
$this->getOptionValues($this->action->id),
$configFileContent,
$this->getPassedOptionValues()
);
$config['sourcePath'] = Yii::getAlias($config['sourcePath']);
$config['messagePath'] = Yii::getAlias($config['messagePath']);
if (!isset($config['sourcePath'], $config['languages'])) {
throw new Exception('The configuration file must specify "sourcePath" and "languages".');
}
if (!is_dir($config['sourcePath'])) {
throw new Exception("The source path {$config['sourcePath']} is not a valid directory.");
}
if (empty($config['format']) || !in_array($config['format'], ['php', 'po', 'pot', 'db'])) {
throw new Exception('Format should be either "php", "po", "pot" or "db".');
}
if (in_array($config['format'], ['php', 'po', 'pot'])) {
if (!isset($config['messagePath'])) {
throw new Exception('The configuration file must specify "messagePath".');
}
if (!is_dir($config['messagePath'])) {
throw new Exception("The message path {$config['messagePath']} is not a valid directory.");
}
}
if (empty($config['languages'])) {
throw new Exception('Languages cannot be empty.');
}
$files = FileHelper::findFiles(realpath($config['sourcePath']), $config);
$files = FileHelper::findFiles(realpath($this->config['sourcePath']), $this->config);
$messages = [];
foreach ($files as $file) {
$messages = array_merge_recursive($messages, $this->extractMessages($file, $config['translator'], $config['ignoreCategories']));
$messages = array_merge_recursive($messages, $this->extractMessages($file, $this->config['translator'], $this->config['ignoreCategories']));
}
$catalog = isset($config['catalog']) ? $config['catalog'] : 'messages';
$catalog = isset($this->config['catalog']) ? $this->config['catalog'] : 'messages';
if (in_array($config['format'], ['php', 'po'])) {
foreach ($config['languages'] as $language) {
$dir = $config['messagePath'] . DIRECTORY_SEPARATOR . $language;
if (in_array($this->config['format'], ['php', 'po'])) {
foreach ($this->config['languages'] as $language) {
$dir = $this->config['messagePath'] . DIRECTORY_SEPARATOR . $language;
if (!is_dir($dir) && !@mkdir($dir)) {
throw new Exception("Directory '{$dir}' can not be created.");
}
if ($config['format'] === 'po') {
$this->saveMessagesToPO($messages, $dir, $config['overwrite'], $config['removeUnused'], $config['sort'], $catalog, $config['markUnused']);
if ($this->config['format'] === 'po') {
$this->saveMessagesToPO($messages, $dir, $this->config['overwrite'], $this->config['removeUnused'], $this->config['sort'], $catalog, $this->config['markUnused']);
} else {
$this->saveMessagesToPHP($messages, $dir, $config['overwrite'], $config['removeUnused'], $config['sort'], $config['markUnused']);
$this->saveMessagesToPHP($messages, $dir, $this->config['overwrite'], $this->config['removeUnused'], $this->config['sort'], $this->config['markUnused']);
}
}
} elseif ($config['format'] === 'db') {
} elseif ($this->config['format'] === 'db') {
/** @var Connection $db */
$db = Instance::ensure($config['db'], Connection::className());
$sourceMessageTable = isset($config['sourceMessageTable']) ? $config['sourceMessageTable'] : '{{%source_message}}';
$messageTable = isset($config['messageTable']) ? $config['messageTable'] : '{{%message}}';
$db = Instance::ensure($this->config['db'], Connection::className());
$sourceMessageTable = isset($this->config['sourceMessageTable']) ? $this->config['sourceMessageTable'] : '{{%source_message}}';
$messageTable = isset($this->config['messageTable']) ? $this->config['messageTable'] : '{{%message}}';
$this->saveMessagesToDb(
$messages,
$db,
$sourceMessageTable,
$messageTable,
$config['removeUnused'],
$config['languages'],
$config['markUnused']
$this->config['removeUnused'],
$this->config['languages'],
$this->config['markUnused']
);
} elseif ($config['format'] === 'pot') {
$this->saveMessagesToPOT($messages, $config['messagePath'], $catalog);
} elseif ($this->config['format'] === 'pot') {
$this->saveMessagesToPOT($messages, $this->config['messagePath'], $catalog);
}
}
@ -739,23 +724,7 @@ EOD;
$array = VarDumper::export($merged);
$content = <<<EOD
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii {$this->id}/{$this->action->id}' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
{$this->config['phpFileHeader']}{$this->config['phpDocBlock']}
return $array;
EOD;
@ -902,4 +871,72 @@ EOD;
$this->stdout("Nothing to save.\n", Console::FG_GREEN);
}
}
/**
* @param string $configFile
* @throws Exception If configuration file does not exists.
* @since 2.0.13
*/
protected function initConfig($configFile)
{
$configFileContent = [];
if ($configFile !== null) {
$configFile = Yii::getAlias($configFile);
if (!is_file($configFile)) {
throw new Exception("The configuration file does not exist: $configFile");
}
$configFileContent = require $configFile;
}
$this->config = array_merge(
$this->getOptionValues($this->action->id),
$configFileContent,
$this->getPassedOptionValues()
);
$this->config['sourcePath'] = Yii::getAlias($this->config['sourcePath']);
$this->config['messagePath'] = Yii::getAlias($this->config['messagePath']);
if (!isset($this->config['sourcePath'], $this->config['languages'])) {
throw new Exception('The configuration file must specify "sourcePath" and "languages".');
}
if (!is_dir($this->config['sourcePath'])) {
throw new Exception("The source path {$this->config['sourcePath']} is not a valid directory.");
}
if (empty($this->config['format']) || !in_array($this->config['format'], ['php', 'po', 'pot', 'db'])) {
throw new Exception('Format should be either "php", "po", "pot" or "db".');
}
if (in_array($this->config['format'], ['php', 'po', 'pot'])) {
if (!isset($this->config['messagePath'])) {
throw new Exception('The configuration file must specify "messagePath".');
}
if (!is_dir($this->config['messagePath'])) {
throw new Exception("The message path {$this->config['messagePath']} is not a valid directory.");
}
}
if (empty($this->config['languages'])) {
throw new Exception('Languages cannot be empty.');
}
if ($this->config['format'] === 'php' && $this->config['phpDocBlock'] === null) {
$this->config['phpDocBlock'] = <<<DOCBLOCK
/**
* Message translations.
*
* This file is automatically generated by 'yii {$this->id}/{$this->action->id}' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
DOCBLOCK;
}
}
}