|
| 1 | +<?php |
| 2 | +namespace MySQLExtractor\Driver; |
| 3 | +use MySQLExtractor\Common\Collection; |
| 4 | +use MySQLExtractor\Common\System; |
| 5 | +use MySQLExtractor\Extractor\Databases; |
| 6 | +use MySQLExtractor\Exceptions\InvalidSourceException; |
| 7 | + |
| 8 | +class Server extends Driver |
| 9 | +{ |
| 10 | + protected $PDO; |
| 11 | + |
| 12 | + public function __construct($mysqlCredentials) |
| 13 | + { |
| 14 | + $this->entries = new Collection(); |
| 15 | + $this->databaseExtractor = new Databases(); |
| 16 | + $this->PDO = System::getPDO($mysqlCredentials); |
| 17 | + } |
| 18 | + |
| 19 | + public function execute() |
| 20 | + { |
| 21 | + $this->extractRemoteDatabases(); |
| 22 | + |
| 23 | + if ($this->entries->count() > 0) { |
| 24 | + $this->databases = $this->databaseExtractor->from($this->entries)->get(); |
| 25 | + } else { |
| 26 | + throw new InvalidSourceException("There were no entries found.", 1); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + protected function extractRemoteDatabases() |
| 31 | + { |
| 32 | + $databases = $this->PDO->query('SHOW DATABASES'); |
| 33 | + foreach ($databases as $database) { |
| 34 | + System::flush(); |
| 35 | + $this->extractDatabaseDefinition($database[0]); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + protected function extractDatabaseDefinition($databaseName) |
| 40 | + { |
| 41 | + $this->PDO->query("use " . $databaseName); |
| 42 | + $tables = $this->PDO->query('SHOW TABLES'); |
| 43 | + |
| 44 | + foreach ($tables as $table) { |
| 45 | + System::flush(); |
| 46 | + $this->extractTableDefinition($table[0], $databaseName); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + protected function extractTableDefinition($tableName, $databaseName) |
| 51 | + { |
| 52 | + $tableContents = $this->PDO->query("SHOW CREATE TABLE `" . $tableName . "`")->fetch(); |
| 53 | + |
| 54 | + if (is_array($tableContents) && isset($tableContents['Create Table'])) { |
| 55 | + $filename = $databaseName . '.' . $tableName . '.sql'; |
| 56 | + $this->entries->set($filename, $tableContents['Create Table']); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments