-
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
1 parent
b7e8348
commit 9b34b27
Showing
34 changed files
with
246 additions
and
164 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
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
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
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
2 changes: 1 addition & 1 deletion
2
src/DBQueries/AbstractDbQueries.php → src/InputModules/AbstractDbQueries.php
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
namespace DBChecker\DBQueries; | ||
namespace DBChecker\InputModules; | ||
|
||
abstract class AbstractDbQueries | ||
{ | ||
|
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace DBChecker\InputModules; | ||
|
||
interface InputModuleInterface extends \DBChecker\BaseModuleInterface | ||
{ | ||
public function getDbal() : AbstractDBAL; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace DBChecker\InputModules; | ||
|
||
use DBChecker\BaseModuleInterface; | ||
use DBChecker\InputModules\MySQL\MySQLModule; | ||
use DBChecker\InputModules\SQLite\SQLiteModule; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
|
||
class InputModuleManager implements BaseModuleInterface | ||
{ | ||
private $modules = []; | ||
|
||
public function getName() | ||
{ | ||
return 'databases'; | ||
} | ||
|
||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$treeBuilder->root($this->getName()) | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->arrayNode('connections') | ||
->requiresAtLeastOneElement() | ||
->arrayPrototype() | ||
->ignoreExtraKeys(false) | ||
->children() | ||
->enumNode('engine') | ||
->values(['mysql', 'sqlite']) | ||
->isRequired() | ||
->end() | ||
->end() | ||
->end() | ||
->end(); | ||
return $treeBuilder; | ||
} | ||
|
||
private function findEngineModule($engineName) : BaseModuleInterface | ||
{ | ||
foreach ([MySQLModule::class, SQLiteModule::class] as $engineModule) | ||
{ | ||
/** @var BaseModuleInterface $module */ | ||
$module = new $engineModule(); | ||
if ($module->getName() === $engineName) | ||
{ | ||
return $module; | ||
} | ||
} | ||
throw new \InvalidArgumentException("Unknown module $engineName"); | ||
} | ||
|
||
public function loadConfig(array $config) | ||
{ | ||
foreach ($config['connections'] as $cnx) | ||
{ | ||
$module = $this->findEngineModule($cnx['engine']); | ||
unset($cnx['engine']); | ||
$module->loadConfig($cnx); | ||
$this->modules[] = $module; | ||
} | ||
} | ||
|
||
public function getDBALs() | ||
{ | ||
foreach ($this->modules as $module) | ||
{ | ||
yield $module->getDbal(); | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
src/DBAL/MySQLDBAL.php → src/InputModules/MySQL/MySQLDBAL.php
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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace DBChecker\InputModules\MySQL; | ||
|
||
use DBChecker\InputModules\AbstractDBAL; | ||
use DBChecker\InputModules\InputModuleInterface; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
|
||
class MySQLModule implements InputModuleInterface | ||
{ | ||
private $dbal; | ||
|
||
public function getName() | ||
{ | ||
return "mysql"; | ||
} | ||
|
||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$treeBuilder->root($this->getName()) | ||
->children() | ||
->scalarNode('name') | ||
->info('A name to use instead of the db name in the output') | ||
->defaultNull() | ||
->end() | ||
->scalarNode('db')->info('The database name')->end() | ||
->scalarNode('login')->defaultNull()->end() | ||
->scalarNode('password')->defaultNull()->end() | ||
->scalarNode('host')->defaultValue('localhost')->end() | ||
->integerNode('port')->end() | ||
->end(); | ||
return $treeBuilder; | ||
} | ||
|
||
public function loadConfig(array $config) | ||
{ | ||
$dsn = "mysql:dbname={$config['db']};host={$config['host']};port={$config['port']}"; | ||
$pdo = new \PDO($dsn, $config['login'], $config['password']); | ||
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); | ||
|
||
$queries = new MySQLQueries($pdo, $config['name'] ?? $config['db']); | ||
$this->dbal = new MySQLDBAL($queries); | ||
} | ||
|
||
public function getDbal() : AbstractDBAL | ||
{ | ||
return $this->dbal; | ||
} | ||
} |
5 changes: 2 additions & 3 deletions
5
src/DBQueries/MySQLQueries.php → src/InputModules/MySQL/MySQLQueries.php
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
4 changes: 2 additions & 2 deletions
4
src/DBAL/SQLiteDBAL.php → src/InputModules/SQLite/SQLiteDBAL.php
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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace DBChecker\InputModules\SQLite; | ||
|
||
use DBChecker\InputModules\AbstractDBAL; | ||
use DBChecker\InputModules\InputModuleInterface; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
|
||
class SQLiteModule implements InputModuleInterface | ||
{ | ||
private $dbal; | ||
|
||
public function getName() | ||
{ | ||
return "sqlite"; | ||
} | ||
|
||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$treeBuilder->root($this->getName()) | ||
->children() | ||
->scalarNode('name') | ||
->info('A name to use instead of the db name in the output') | ||
->defaultNull() | ||
->end() | ||
->end(); | ||
return $treeBuilder; | ||
} | ||
|
||
public function loadConfig(array $config) | ||
{ | ||
$pdo = new \PDO("sqlite:{$config['db']}"); | ||
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); | ||
|
||
$queries = new SQLiteQueries($pdo, $config['name'] ?? $config['db']); | ||
$this->dbal = new SQLiteDBAL($queries); | ||
} | ||
|
||
public function getDbal() : AbstractDBAL | ||
{ | ||
return $this->dbal; | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
src/DBQueries/SQLiteQueries.php → src/InputModules/SQLite/SQLiteQueries.php
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
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
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
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
Oops, something went wrong.