forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresConfig.php
More file actions
79 lines (68 loc) · 2.64 KB
/
PostgresConfig.php
File metadata and controls
79 lines (68 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Tempest\Database\Config;
use PDO;
use SensitiveParameter;
use Tempest\Database\Migrations\DatePrefixStrategy;
use Tempest\Database\Migrations\MigrationNamingStrategy;
use Tempest\Database\Tables\NamingStrategy;
use Tempest\Database\Tables\PluralizedSnakeCaseStrategy;
use UnitEnum;
final class PostgresConfig implements DatabaseConfig
{
public string $dsn {
get => sprintf(
'pgsql:host=%s;port=%s;dbname=%s;user=%s;password=%s',
$this->host,
$this->port,
$this->database,
$this->username,
$this->password,
);
}
public DatabaseDialect $dialect {
get => DatabaseDialect::POSTGRESQL;
}
public bool $usePersistentConnection {
get => $this->persistent;
}
public MigrationNamingStrategy $migrationNamingStrategy {
get => $this->migrationNaming;
}
public array $options {
get {
$options = [];
if ($this->persistent) {
$options[PDO::ATTR_PERSISTENT] = true;
}
return $options;
}
}
/**
* @param string $host The PostgreSQL server hostname or IP address.
* @param string $port The PostgreSQL server port number.
* @param string $username The PostgreSQL username for authentication.
* @param string $password The PostgreSQL password for authentication.
* @param string $database The database name to connect to.
* @param bool $persistent Whether to use persistent connections. Persistent connections are not closed at the end of the script and are cached for reuse when another script requests a connection using the same credentials.
* @param NamingStrategy $namingStrategy The naming strategy for database tables and columns.
* @param MigrationNamingStrategy $migrationNaming The naming strategy for migration file prefixes.
* @param string|UnitEnum|null $tag An optional tag to identify this database configuration.
*/
public function __construct(
#[SensitiveParameter]
public string $host = '127.0.0.1',
#[SensitiveParameter]
public string $port = '5432',
#[SensitiveParameter]
public string $username = 'postgres',
#[SensitiveParameter]
public string $password = '',
#[SensitiveParameter]
public string $database = 'app',
public bool $persistent = false,
public NamingStrategy $namingStrategy = new PluralizedSnakeCaseStrategy(),
public MigrationNamingStrategy $migrationNaming = new DatePrefixStrategy(),
public null|string|UnitEnum $tag = null,
) {}
}