Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aurelien-riv committed May 16, 2018
1 parent b7e8348 commit 9b34b27
Show file tree
Hide file tree
Showing 34 changed files with 246 additions and 164 deletions.
4 changes: 2 additions & 2 deletions config.template.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
databases:
connections:
- dsn: "sqlite::memory:"
- db: ":memory:"
name: database1
engine: sqlite
- dsn: "sqlite::memory:"
- db: ":memory:"
name: database2
engine: sqlite

Expand Down
5 changes: 0 additions & 5 deletions src/BaseModuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,4 @@ public function getConfigTreeBuilder();
* Load the settings of the module
*/
public function loadConfig(array $config);

/**
* @return array
*/
public function getConfig();
}
8 changes: 6 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace DBChecker;

use DBChecker\DBAL\AbstractDBAL;
use DBChecker\InputModules\AbstractDBAL;
use DBChecker\InputModules\InputModuleManager;
use DBChecker\modules\DataBase\DatabasesModule;
use DBChecker\modules\ModuleManager;
use Symfony\Component\Yaml\Yaml;

Expand All @@ -15,6 +17,8 @@ public function __construct($yamlPath)
$settings = Yaml::parseFile($yamlPath);

$this->moduleManager = new ModuleManager();

$this->moduleManager->loadModule(new InputModuleManager(), $settings);
foreach (ModuleManager::ENABLED_MODULES as $module)
{
$this->moduleManager->loadModule(new $module(), $settings);
Expand All @@ -29,7 +33,7 @@ public function getModuleWorkers()
yield from $this->moduleManager->getWorkers();
}

public function getDBALs() : array
public function getDBALs() : \Generator
{
return $this->moduleManager->getDatabaseModule()->getDBALs();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

namespace DBChecker\DBAL;
namespace DBChecker\InputModules;

use BadMethodCallException;
use DBChecker\DBQueries\AbstractDbQueries;

abstract class AbstractDBAL implements
\DBChecker\modules\MissingCompressionDetect\DBQueriesInterface,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DBChecker\DBQueries;
namespace DBChecker\InputModules;

abstract class AbstractDbQueries
{
Expand Down
8 changes: 8 additions & 0 deletions src/InputModules/InputModuleInterface.php
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;
}
72 changes: 72 additions & 0 deletions src/InputModules/InputModuleManager.php
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace DBChecker\DBAL;
namespace DBChecker\InputModules\MySQL;

use DBChecker\DBQueries\MySQLQueries;
use DBChecker\InputModules\AbstractDBAL;

/**
* @property MySQLQueries $queries
Expand Down
50 changes: 50 additions & 0 deletions src/InputModules/MySQL/MySQLModule.php
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

namespace DBChecker\DBQueries;
namespace DBChecker\InputModules\MySQL;

use DBChecker\DBAL\AbstractDBAL;
use DBChecker\DBAL\MySQLDBAL;
use DBChecker\InputModules\AbstractDbQueries;

class MySQLQueries extends AbstractDbQueries
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace DBChecker\DBAL;
namespace DBChecker\InputModules\SQLite;

use DBChecker\DBQueries\SQLiteQueries;
use DBChecker\InputModules\AbstractDBAL;

/**
* @property SQLiteQueries $queries
Expand Down
44 changes: 44 additions & 0 deletions src/InputModules/SQLite/SQLiteModule.php
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace DBChecker\DBQueries;
namespace DBChecker\InputModules\SQLite;

use DBChecker\InputModules\AbstractDbQueries;

class SQLiteQueries extends AbstractDbQueries
{
Expand Down
2 changes: 1 addition & 1 deletion src/ModuleWorkerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DBChecker;

use DBChecker\DBAL\AbstractDBAL;
use DBChecker\InputModules\AbstractDBAL;

interface ModuleWorkerInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/modules/AnalyzeTableCheck/AnalyzeTableCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DBChecker\modules\AnalyzeTableCheck;

use DBChecker\DBAL\AbstractDBAL;
use DBChecker\InputModules\AbstractDBAL;
use DBChecker\ModuleWorkerInterface;

class AnalyzeTableCheck implements ModuleWorkerInterface
Expand Down
6 changes: 0 additions & 6 deletions src/modules/AnalyzeTableCheck/AnalyzeTableCheckModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace DBChecker\modules\AnalyzeTableCheck;

use DBChecker\Config;
use DBChecker\ModuleInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand All @@ -24,11 +23,6 @@ public function loadConfig(array $config)
{
}

public function getConfig()
{
return [];
}

public function getWorker()
{
return new AnalyzeTableCheck();
Expand Down
Loading

0 comments on commit 9b34b27

Please sign in to comment.