-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPostgresAdapter.php
More file actions
102 lines (86 loc) · 1.97 KB
/
Copy pathPostgresAdapter.php
File metadata and controls
102 lines (86 loc) · 1.97 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace App\Database;
final class PostgresAdapter implements DatabaseAdapterInterface
{
public function getServiceName(): string
{
return 'postgres';
}
public function supports(string $serviceName): bool
{
return in_array($serviceName, ['postgres', 'postgresql'], true);
}
public function getUsername(): string
{
return 'postgres';
}
public function getPassword(): string
{
return 'postgres';
}
public function getDefaultPort(): int
{
return 5432;
}
/**
* @return list<string>
*/
public function getUrlSchemes(): array
{
return ['postgres', 'postgresql', 'pgsql'];
}
/**
* @return list<string>
*/
public function getDumpCommand(string $database): array
{
return [
'pg_dump',
'-U',
$this->getUsername(),
$database,
];
}
/**
* @return list<string>
*/
public function getRestoreCommand(string $database): array
{
return [
'psql',
'-U',
$this->getUsername(),
$database,
];
}
/**
* @return list<string>
*/
public function getShellCommand(string $database = ''): array
{
$cmd = [
'psql',
'-U',
$this->getUsername(),
];
if ($database !== '') {
$cmd[] = $database;
}
return $cmd;
}
public function getDsn(string $host = '127.0.0.1', int $port = 0, ?string $database = null): string
{
$dsn = sprintf(
'postgresql://%s:%s@%s:%d',
rawurlencode($this->getUsername()),
rawurlencode($this->getPassword()),
$host,
$port > 0 ? $port : $this->getDefaultPort(),
);
if ($database !== null) {
$dsn .= '/'.$database;
}
return $dsn;
}
}