1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle Database package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Config\Postgres; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Config\ProvidesSourceString; |
15
|
|
|
|
16
|
|
|
class TcpConnectionConfig extends ConnectionConfig implements ProvidesSourceString |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param non-empty-string $database |
|
|
|
|
20
|
|
|
* @param non-empty-string $host |
21
|
|
|
* @param positive-int $port |
22
|
|
|
* @param non-empty-string|null $user |
23
|
|
|
* @param non-empty-string|null $password |
24
|
|
|
* @param array $options |
25
|
|
|
*/ |
26
|
34 |
|
public function __construct( |
27
|
|
|
public string $database, |
28
|
|
|
public string $host = 'localhost', |
29
|
|
|
public int $port = 5432, |
30
|
|
|
?string $user = null, |
31
|
|
|
?string $password = null, |
32
|
|
|
array $options = [] |
33
|
|
|
) { |
34
|
34 |
|
parent::__construct($user, $password, $options); |
35
|
34 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
2 |
|
public function getSourceString(): string |
41
|
|
|
{ |
42
|
2 |
|
return $this->database; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns the Postgres-specific PDO DataSourceName, that looks like: |
47
|
|
|
* <code> |
48
|
|
|
* pgsql:host=localhost;port=5432;dbname=dbname;user=login;password=pass |
49
|
|
|
* </code> |
50
|
|
|
* |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
40 |
|
public function getDsn(): string |
54
|
|
|
{ |
55
|
40 |
|
$config = [ |
56
|
40 |
|
'host' => $this->host, |
57
|
40 |
|
'port' => $this->port, |
58
|
40 |
|
'dbname' => $this->database, |
59
|
|
|
|
60
|
|
|
// |
61
|
|
|
// Username and Password may be is a part of DataSourceName |
62
|
|
|
// However, they can also be passed as separate |
63
|
|
|
// parameters, so we ignore the case with the DataSourceName: |
64
|
|
|
// |
65
|
|
|
// 'user' => $this->user, |
66
|
|
|
// 'password' => $this->password, |
67
|
|
|
]; |
68
|
|
|
|
69
|
40 |
|
return \sprintf('%s:%s', $this->getName(), $this->dsn($config)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function __set_state(array $state): self |
73
|
|
|
{ |
74
|
|
|
return new self(...$state); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|