优化CRUD生成的lang文件——格式美化

pull/98/MERGE
PPPSCN 2019-02-16 19:51:46 +08:00 committed by Karson
parent f9b43bdbe4
commit 2bfd19267a
1 changed files with 18 additions and 2 deletions

View File

@ -116,6 +116,11 @@ class Crud extends Command
*/ */
protected $editorClass = 'editor'; protected $editorClass = 'editor';
/**
* langList的key最长字节数
*/
protected $fieldMaxLen = 0;
protected function configure() protected function configure()
{ {
$this $this
@ -730,6 +735,15 @@ class Crud extends Command
$editList = implode("\n", array_filter($editList)); $editList = implode("\n", array_filter($editList));
$javascriptList = implode(",\n", array_filter($javascriptList)); $javascriptList = implode(",\n", array_filter($javascriptList));
$langList = implode(",\n", array_filter($langList)); $langList = implode(",\n", array_filter($langList));
//数组等号对齐
$langList = array_filter(explode(",\n", $langList . ",\n"));
foreach ($langList as &$line) {
if (preg_match("/^\s+'([^']+)'\s*=>\s*'([^']+)'\s*/is", $line, $matches)) {
$line = " '{$matches[1]}'" . str_pad('=>', ($this->fieldMaxLen - strlen($matches[1]) + 3), ' ', STR_PAD_LEFT) . " '{$matches[2]}'";
}
}
unset($line);
$langList = implode(",\n", array_filter($langList)). ",";
//表注释 //表注释
$tableComment = $modelTableInfo['Comment']; $tableComment = $modelTableInfo['Comment'];
@ -1072,6 +1086,7 @@ EOD;
{ {
if ($content || !Lang::has($field)) { if ($content || !Lang::has($field)) {
$itemArr = []; $itemArr = [];
$this->fieldMaxLen = strlen($field) > $this->fieldMaxLen ? strlen($field) : $this->fieldMaxLen;
$content = str_replace('', ',', $content); $content = str_replace('', ',', $content);
if (stripos($content, ':') !== false && stripos($content, ',') && stripos($content, '=') !== false) { if (stripos($content, ':') !== false && stripos($content, ',') && stripos($content, '=') !== false) {
list($fieldLang, $item) = explode(':', $content); list($fieldLang, $item) = explode(':', $content);
@ -1081,6 +1096,7 @@ EOD;
if (count($valArr) == 2) { if (count($valArr) == 2) {
list($key, $value) = $valArr; list($key, $value) = $valArr;
$itemArr[$field . ' ' . $key] = $value; $itemArr[$field . ' ' . $key] = $value;
$this->fieldMaxLen = strlen($field . ' ' . $key) > $this->fieldMaxLen ? strlen($field . ' ' . $key) : $this->fieldMaxLen;
} }
} }
} else { } else {