diff --git a/services/scanners/ScannerDatabase.php b/services/scanners/ScannerDatabase.php index bf2a8bf..419a121 100644 --- a/services/scanners/ScannerDatabase.php +++ b/services/scanners/ScannerDatabase.php @@ -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 + * * @since 1.0 */ -class ScannerDatabase { - +class ScannerDatabase +{ /** * @var array array containing the table ids to process. */ @@ -46,9 +46,10 @@ 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; @@ -56,9 +57,9 @@ public function __construct(Scanner $scanner) { 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); + } }