forked from lajax/yii2-translate-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,14 @@ | |
* Detecting existing language elements in database. | ||
* The connection ids of the scanned databases and the table/field names can be defined in the configuration file of translateManager | ||
* examples: | ||
* | ||
* | ||
* ~~~ | ||
* 'tables' => [ | ||
* [ | ||
* 'connection' => 'db', | ||
* 'table' => 'language', | ||
* 'table' => '{{%language}}', | ||
* 'columns' => ['name', 'name_ascii'], | ||
* 'category' => 'database-table-name, | ||
* 'categoryPrefix' => 'lx-' | ||
* 'category' => 'database-table-name', | ||
* ], | ||
* [ | ||
* 'connection' => 'db', | ||
|
@@ -28,13 +27,14 @@ | |
* ] | ||
* ] | ||
* ~~~ | ||
* | ||
* | ||
* | ||
* | ||
* @author Lajos Molnár <[email protected]> | ||
* | ||
* @since 1.0 | ||
*/ | ||
class ScannerDatabase { | ||
|
||
class ScannerDatabase | ||
{ | ||
/** | ||
* @var array array containing the table ids to process. | ||
*/ | ||
|
@@ -46,19 +46,20 @@ class ScannerDatabase { | |
private $_scanner; | ||
|
||
/** | ||
* @param Scaner $scanner | ||
* @param Scanner $scanner | ||
*/ | ||
public function __construct(Scanner $scanner) { | ||
public function __construct(Scanner $scanner) | ||
{ | ||
$this->_scanner = $scanner; | ||
$this->_tables = Yii::$app->getModule('translatemanager')->tables; | ||
|
||
if (!empty($this->_tables) && is_array($this->_tables)) { | ||
foreach ($this->_tables as $tables) { | ||
if (empty($tables['connection'])) { | ||
throw new InvalidConfigException('Incomplete database configuration: connection '); | ||
} else if (empty($tables['table'])) { | ||
} elseif (empty($tables['table'])) { | ||
throw new InvalidConfigException('Incomplete database configuration: table '); | ||
} else if (empty($tables['columns'])) { | ||
} elseif (empty($tables['columns'])) { | ||
throw new InvalidConfigException('Incomplete database configuration: columns '); | ||
} | ||
} | ||
|
@@ -68,7 +69,8 @@ public function __construct(Scanner $scanner) { | |
/** | ||
* Scanning database tables defined in configuration file. Searching for language elements yet to be translated. | ||
*/ | ||
public function run() { | ||
public function run() | ||
{ | ||
$this->_scanner->stdout('Detect DatabaseTable - BEGIN', Console::FG_GREY); | ||
if (is_array($this->_tables)) { | ||
foreach ($this->_tables as $tables) { | ||
|
@@ -81,15 +83,17 @@ public function run() { | |
|
||
/** | ||
* Scanning database table | ||
* | ||
* @param array $tables | ||
*/ | ||
private function _scanningTable($tables) { | ||
private function _scanningTable($tables) | ||
{ | ||
$this->_scanner->stdout('Extracting mesages from ' . $tables['table'] . '.' . implode(',', $tables['columns']), Console::FG_GREEN); | ||
$query = new \yii\db\Query(); | ||
$data = $query->select($tables['columns']) | ||
->from($tables['table']) | ||
->createCommand(Yii::$app->$tables['connection']) | ||
->queryAll(); | ||
->from($tables['table']) | ||
->createCommand(Yii::$app->{$tables['connection']}) | ||
->queryAll(); | ||
$category = $this->_getCategory($tables); | ||
foreach ($data as $columns) { | ||
$columns = array_map('trim', $columns); | ||
|
@@ -98,20 +102,34 @@ private function _scanningTable($tables) { | |
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the language category. | ||
* | ||
* @param array $tables | ||
* | ||
* @return string | ||
*/ | ||
private function _getCategory($tables) { | ||
private function _getCategory($tables) | ||
{ | ||
if (isset($tables['category']) && $tables['category'] == 'database-table-name') { | ||
$category = (isset($tables['categoryPrefix'])) ? $tables['categoryPrefix'] . $tables['table'] : $tables['table']; | ||
$category = $this->_normalizeTablename($tables['table']); | ||
} else { | ||
$category = Scanner::CATEGORY_DATABASE; | ||
} | ||
|
||
return $category; | ||
} | ||
|
||
/** | ||
* Returns the normalized database table name. | ||
* | ||
* @param string $tableName database table name. | ||
* | ||
* @return string | ||
*/ | ||
private function _normalizeTablename($tableName) | ||
{ | ||
return str_replace(['{', '%', '}'], '', $tableName); | ||
} | ||
} |