Skip to content

Commit 3d3a094

Browse files
blackshadevbrendt
andauthored
feat(database): refactor DatabaseConfig interface (#902)
Co-authored-by: Brent Roose <[email protected]>
1 parent 269bfcb commit 3d3a094

File tree

72 files changed

+296
-397
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+296
-397
lines changed

src/Tempest/Console/src/Stubs/database.config.stub.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
declare(strict_types=1);
44

5-
use Tempest\Database\Connections\MySqlConnection;
6-
use Tempest\Database\DatabaseConfig;
5+
use Tempest\Database\Config\MysqlConfig;
76
use function Tempest\env;
87

9-
return new DatabaseConfig(
10-
connection: new MySqlConnection(
11-
host: env('DB_HOST'),
12-
port: env('DB_PORT'),
13-
username: env('DB_USERNAME'),
14-
password: env('DB_PASSWORD'),
15-
database: env('DB_DATABASE'),
16-
),
8+
return new MysqlConfig(
9+
host: env('DB_HOST'),
10+
port: env('DB_PORT'),
11+
username: env('DB_USERNAME'),
12+
password: env('DB_PASSWORD'),
13+
database: env('DB_DATABASE'),
1714
);

src/Tempest/Container/src/GenericContainer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public function config(object $config): self
107107
{
108108
$this->singleton($config::class, $config);
109109

110+
foreach (new ClassReflector($config)->getInterfaces() as $interface) {
111+
$this->singleton($interface->getName(), $config);
112+
}
113+
110114
return $this;
111115
}
112116

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Database\Config;
6+
7+
use Tempest\Database\Tables\NamingStrategy;
8+
9+
interface DatabaseConfig
10+
{
11+
public string $dsn { get; }
12+
13+
public NamingStrategy $namingStrategy { get; }
14+
15+
public DatabaseDialect $dialect { get; }
16+
17+
public ?string $username { get; }
18+
19+
public ?string $password { get; }
20+
}

src/Tempest/Database/src/DatabaseDialect.php renamed to src/Tempest/Database/src/Config/DatabaseDialect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Tempest\Database;
5+
namespace Tempest\Database\Config;
66

77
enum DatabaseDialect: string
88
{

src/Tempest/Database/src/Connections/MySqlConnection.php renamed to src/Tempest/Database/src/Config/MysqlConfig.php

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@
22

33
declare(strict_types=1);
44

5-
namespace Tempest\Database\Connections;
5+
namespace Tempest\Database\Config;
66

77
use SensitiveParameter;
8-
use Tempest\Database\DatabaseDialect;
98
use Tempest\Database\Tables\NamingStrategy;
109
use Tempest\Database\Tables\PluralizedSnakeCaseStrategy;
1110

12-
final readonly class MySqlConnection implements DatabaseConnection
11+
final class MysqlConfig implements DatabaseConfig
1312
{
13+
public string $dsn {
14+
get => sprintf(
15+
'mysql:host=%s:%s;dbname=%s',
16+
$this->host,
17+
$this->port,
18+
$this->database,
19+
);
20+
}
21+
22+
public DatabaseDialect $dialect {
23+
get => DatabaseDialect::MYSQL;
24+
}
25+
1426
public function __construct(
1527
#[SensitiveParameter]
1628
public string $host = 'localhost',
@@ -25,29 +37,4 @@ public function __construct(
2537
public NamingStrategy $namingStrategy = new PluralizedSnakeCaseStrategy(),
2638
) {
2739
}
28-
29-
public function getDsn(): string
30-
{
31-
return "mysql:host={$this->host}:{$this->port};dbname={$this->database}";
32-
}
33-
34-
public function getUsername(): string
35-
{
36-
return $this->username;
37-
}
38-
39-
public function getPassword(): string
40-
{
41-
return $this->password;
42-
}
43-
44-
public function dialect(): DatabaseDialect
45-
{
46-
return DatabaseDialect::MYSQL;
47-
}
48-
49-
public function tableNamingStrategy(): NamingStrategy
50-
{
51-
return $this->namingStrategy;
52-
}
5340
}

src/Tempest/Database/src/Connections/PostgresConnection.php renamed to src/Tempest/Database/src/Config/PostgresConfig.php

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,29 @@
22

33
declare(strict_types=1);
44

5-
namespace Tempest\Database\Connections;
5+
namespace Tempest\Database\Config;
66

77
use SensitiveParameter;
8-
use Tempest\Database\DatabaseDialect;
98
use Tempest\Database\Tables\NamingStrategy;
109
use Tempest\Database\Tables\PluralizedSnakeCaseStrategy;
1110

12-
final class PostgresConnection implements DatabaseConnection
11+
final class PostgresConfig implements DatabaseConfig
1312
{
13+
public string $dsn {
14+
get => sprintf(
15+
'pgsql:host=%s;port=%s;dbname=%s;user=%s;password=%s',
16+
$this->host,
17+
$this->port,
18+
$this->database,
19+
$this->username,
20+
$this->password,
21+
);
22+
}
23+
24+
public DatabaseDialect $dialect {
25+
get => DatabaseDialect::POSTGRESQL;
26+
}
27+
1428
public function __construct(
1529
#[SensitiveParameter]
1630
public string $host = '127.0.0.1',
@@ -21,42 +35,8 @@ public function __construct(
2135
#[SensitiveParameter]
2236
public string $password = '',
2337
#[SensitiveParameter]
24-
public string $database = 'postgres',
38+
public string $database = 'app',
2539
public NamingStrategy $namingStrategy = new PluralizedSnakeCaseStrategy(),
2640
) {
2741
}
28-
29-
public function getDsn(): string
30-
{
31-
//DATABASE_URL="postgresql://postgres:postgres@postgres:5432/mydb?schema=public"
32-
33-
return sprintf(
34-
'pgsql:host=%s;port=%s;dbname=%s;user=%s;password=%s',
35-
$this->host,
36-
$this->port,
37-
$this->database,
38-
$this->username,
39-
$this->password,
40-
);
41-
}
42-
43-
public function getUsername(): string
44-
{
45-
return $this->username;
46-
}
47-
48-
public function getPassword(): string
49-
{
50-
return $this->password;
51-
}
52-
53-
public function dialect(): DatabaseDialect
54-
{
55-
return DatabaseDialect::POSTGRESQL;
56-
}
57-
58-
public function tableNamingStrategy(): NamingStrategy
59-
{
60-
return $this->namingStrategy;
61-
}
6242
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Database\Config;
6+
7+
use SensitiveParameter;
8+
use Tempest\Database\Tables\NamingStrategy;
9+
use Tempest\Database\Tables\PluralizedSnakeCaseStrategy;
10+
11+
final class SQLiteConfig implements DatabaseConfig
12+
{
13+
public string $dsn {
14+
get => sprintf(
15+
'sqlite:%s',
16+
$this->path,
17+
);
18+
}
19+
20+
public ?string $username {
21+
get => null;
22+
}
23+
24+
public ?string $password {
25+
get => null;
26+
}
27+
28+
public DatabaseDialect $dialect {
29+
get => DatabaseDialect::SQLITE;
30+
}
31+
32+
public function __construct(
33+
#[SensitiveParameter]
34+
public string $path = 'localhost',
35+
public NamingStrategy $namingStrategy = new PluralizedSnakeCaseStrategy(),
36+
) {
37+
}
38+
}

src/Tempest/Database/src/Config/database.config.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use Tempest\Database\Connections\SQLiteConnection;
6-
use Tempest\Database\DatabaseConfig;
5+
use Tempest\Database\Config\SQLiteConfig;
76

8-
return new DatabaseConfig(
9-
connection: new SQLiteConnection(
10-
path: __DIR__ . '/../database.sqlite',
11-
),
7+
return new SQLiteConfig(
8+
path: __DIR__ . '/../database.sqlite',
129
);

src/Tempest/Database/src/CachedConnectionInitializer.php renamed to src/Tempest/Database/src/Connection/CachedConnectionInitializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Tempest\Database;
5+
namespace Tempest\Database\Connection;
66

77
use Tempest\Container\Container;
88
use Tempest\Container\Initializer;

src/Tempest/Database/src/Connection.php renamed to src/Tempest/Database/src/Connection/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Tempest\Database;
5+
namespace Tempest\Database\Connection;
66

77
use PDOStatement;
88

0 commit comments

Comments
 (0)