Transition from environment variables to a configuration file format.

This commit is contained in:
Austen McDonald
2016-07-22 04:19:10 +00:00
parent 8973dc943b
commit 2370528392
5 changed files with 39 additions and 35 deletions
+7
View File
@@ -0,0 +1,7 @@
database:
dsn: sqlite::memory:
name: daenerys
user: root
password:
logs:
path: ../logs
+1 -5
View File
@@ -1,10 +1,6 @@
<phpunit bootstrap="bootstrap/bootstrap.php">
<php>
<env name="DB_DSN" value="sqlite::memory:" />
<env name="DB_USER" value="root" />
<env name="DB_PASSWORD" value="" />
<env name="DB_NAME" value="daenerys" />
<env name="LOG_PATH" value="./logs" />
<env name="LOTGD_CONFIG" value="./config/test.yml" />
</php>
<testsuites>
<testsuite name="All">
+9 -28
View File
@@ -32,39 +32,20 @@ class Bootstrap
*/
public static function createGame(): Game
{
$logPath = getenv('LOG_PATH');
if ($logPath === false || strlen($logPath) == 0 || is_dir($logPath) === false) {
throw new InvalidConfigurationException("Invalid or missing log directory: '{$logPath}'");
$configFilePath = getenv('LOTGD_CONFIG');
if ($configFilePath === false || strlen($configFilePath) == 0 || is_file($configFilePath) === false) {
throw new InvalidConfigurationException("Invalid or missing configuration file: '{$configFilePath}'.");
}
$cleanedLogPath = realpath($logPath);
$config = new Configuration($configFilePath);
$logger = new \Monolog\Logger('lotgd');
// Add lotgd as the prefix for the log filenames.
$logger->pushHandler(new \Monolog\Handler\RotatingFileHandler($cleanedLogPath . DIRECTORY_SEPARATOR . 'lotgd', 14));
$logger->pushHandler(new \Monolog\Handler\RotatingFileHandler($config->getLogPath() . DIRECTORY_SEPARATOR . 'lotgd', 14));
$v = Game::getVersion();
$logger->info("Bootstrap constructing game (Daenerys 🐲 {$v}).");
$logger->info("Bootstrap constructing game (Daenerys 🐲{$v}).");
$dsn = getenv('DB_DSN');
$user = getenv('DB_USER');
$passwd = getenv('DB_PASSWORD');
if ($dsn === false || strlen($dsn) == 0) {
$m = "Invalid or missing data source name: '{$dsn}'";
$logger->critical($m);
throw new InvalidConfigurationException($m);
}
if ($user === false || strlen($user) == 0) {
$m = "Invalid or missing database user: '{$user}'";
$logger->critical($m);
throw new InvalidConfigurationException("Invalid or missing database user: '{$user}'");
}
if ($passwd === false) {
$m = "Invalid or missing database password: '{$passwd}'";
$logger->critical($m);
throw new InvalidConfigurationException("Invalid or missing database password: '{$passwd}'");
}
$pdo = new \PDO($dsn, $user, $passwd);
$pdo = new \PDO($config->getDatabaseDSN(), $config->getDatabaseUser(), $config->getDatabasePassword());
// Read db annotations from model files
$annotationMetaDataDirectories = array_merge(
@@ -85,6 +66,6 @@ class Bootstrap
$eventManager = new EventManager($entityManager);
return new Game($entityManager, $eventManager, $logger);
return new Game($config, $entityManager, $eventManager, $logger);
}
}
+12
View File
@@ -14,12 +14,15 @@ class Game
private $composerManager;
private $moduleManager;
private $logger;
private $configuration;
public function __construct(
Configuration $configuration,
EntityManagerInterface $entityManager,
EventManager $eventManager,
\Monolog\Logger $logger)
{
$this->configuration = $configuration;
$this->entityManager = $entityManager;
$this->eventManager = $eventManager;
$this->logger = $logger;
@@ -34,6 +37,15 @@ class Game
return '0.1.0';
}
/**
* Returns the game's configuration.
* @return Configuration The game's configuration.
*/
public function getConfiguration(): Configuration
{
return $this->configuration;
}
/**
* Returns the game's module manager.
* @return ModuleManager The game's module manager.
+10 -2
View File
@@ -9,6 +9,8 @@ use Doctrine\ORM\Mapping\AnsiQuoteStrategy;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\Tools\SchemaTool;
use LotGD\Core\Configuration;
/**
* Description of ModelTestCase
*/
@@ -28,8 +30,14 @@ abstract class ModelTestCase extends \PHPUnit_Extensions_Database_TestCase
final public function getConnection(): \PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
{
if ($this->connection === null) {
$configFilePath = getenv('LOTGD_CONFIG');
if ($configFilePath === false || strlen($configFilePath) == 0 || is_file($configFilePath) === false) {
throw new InvalidConfigurationException("Invalid or missing configuration file: '{$configFilePath}'.");
}
$config = new Configuration($configFilePath);
if (self::$pdo === null) {
self::$pdo = new \PDO(getenv('DB_DSN'), getenv('DB_USER'), getenv('DB_PASSWORD'));
self::$pdo = new \PDO($config->getDatabaseDSN(), $config->getDatabaseUser(), $config->getDatabasePassword());
// Read db annotations from model files
$configuration = Setup::createAnnotationMetadataConfiguration(["src/Models"], true);
@@ -43,7 +51,7 @@ abstract class ModelTestCase extends \PHPUnit_Extensions_Database_TestCase
$schemaTool->updateSchema($metaData);
}
$this->connection = $this->createDefaultDBConnection(self::$pdo, getenv('DB_NAME'));
$this->connection = $this->createDefaultDBConnection(self::$pdo, $config->getDatabaseName());
}
return $this->connection;