Add configuration dir, add datbase connection interface

This commit is contained in:
Dave Smith-Hayes 2024-05-14 21:10:36 -04:00
parent f5d5afa3c2
commit 54473bdf79
3 changed files with 46 additions and 19 deletions

View File

@ -0,0 +1,25 @@
<?php
namespace Slovocast\Infrastructure\Configuration;
use League\Config\ConfigurationInterface;
use League\Config\Configuration;
use Nette\Schema\Expect;
class DatabaseConfiguration
{
public static function getConfig(): ConfigurationInterface
{
return new Configuration([
'database' => Expect::structure([
'driver' => Expect::anyOf('mysql', 'sqlite')->required(),
'host' => Expect::string()->default('localhost'),
'post' => Expect::int()->min(1)->max(65535),
'database' => Expect::string()->required(),
'username' => Expect::string()->required(),
'password' => Expect::string()->nullable()
])
]);
}
}

View File

@ -2,38 +2,25 @@
namespace Slovocast\Infrastructure; namespace Slovocast\Infrastructure;
use League\Config\Configuration;
use Nette\Schema\Expect;
use PDO; use PDO;
use Slovocast\Infrastructure\DatabaseConnectionInterface;
/** /**
* Represents an active connection to a database * Represents an active connection to a database
*/ */
class DatabaseConnection class DatabaseConnection implements DatabaseConnectionInterface
{ {
private string $name;
private PDO $pdo; private PDO $pdo;
public function __construct(Configuration $config) public function __construct(string $name, PDO $pdo)
{ {
$this->pdo = new PDO(); $this->name = $name;
$this->pdo = $pdo;
} }
public function getConnection(): PDO public function getConnection(): PDO
{ {
return $this->pdo; return $this->pdo;
} }
public static function getConfigSchema(): Configuration
{
return new Configuration([
'database' => Expect::structure([
'driver' => Expect::anyOf('mysql', 'sqlite')->required(),
'host' => Expect::string()->default('localhost'),
'post' => Expect::int()->min(1)->max(65535),
'database' => Expect::string()->required(),
'username' => Expect::string()->required(),
'password' => Expect::string()->nullable()
])
]);
}
} }

View File

@ -0,0 +1,15 @@
<?php
namespace Slovocast\Infrastructure;
use PDO;
/**
* Represents an active connection to the Database through the PDO. Unlike a
* pool, these connections don't need to be returned.
*/
interface DatabaseConnectionInterface
{
public function getConnection(): PDO;
public function getName(): string;
}