Add configuration dir, add datbase connection interface
This commit is contained in:
parent
f5d5afa3c2
commit
54473bdf79
@ -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()
|
||||
])
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
@ -2,38 +2,25 @@
|
||||
|
||||
namespace Slovocast\Infrastructure;
|
||||
|
||||
use League\Config\Configuration;
|
||||
use Nette\Schema\Expect;
|
||||
use PDO;
|
||||
use Slovocast\Infrastructure\DatabaseConnectionInterface;
|
||||
|
||||
/**
|
||||
* Represents an active connection to a database
|
||||
*/
|
||||
class DatabaseConnection
|
||||
class DatabaseConnection implements DatabaseConnectionInterface
|
||||
{
|
||||
private string $name;
|
||||
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
|
||||
{
|
||||
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()
|
||||
])
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
15
code/src/Infrastructure/DatabaseConnectionInterface.php
Normal file
15
code/src/Infrastructure/DatabaseConnectionInterface.php
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user