Skip to content

Commit

Permalink
Update ScannerDatabase.php
Browse files Browse the repository at this point in the history
  • Loading branch information
guimarca authored Jul 5, 2017
1 parent 36fea0d commit ea3bac9
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions services/scanners/ScannerDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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.
*/
Expand All @@ -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 ');
}
}
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
}
}

0 comments on commit ea3bac9

Please sign in to comment.