1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Connection; |
6
|
|
|
|
7
|
|
|
use Stringable; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The Dsn class is typically used to parse a DSN string, which is a string that contains all the necessary information |
11
|
|
|
* to connect to a database, such as the database driver, hostname, database name, port and options. |
12
|
|
|
* |
13
|
|
|
* It also allows you to access individual components of the DSN, such as the driver or the database name. |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractDsn implements Stringable |
16
|
|
|
{ |
17
|
|
|
private bool $unixSocket = false; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @psalm-param string[] $options |
21
|
|
|
*/ |
22
|
|
|
public function __construct( |
23
|
|
|
private string $driver, |
24
|
|
|
private string $host, |
25
|
|
|
private string $databaseName, |
26
|
|
|
private string|null $port = null, |
27
|
|
|
private array $options = [] |
28
|
|
|
) { |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string The Data Source Name, or DSN, contains the information required to connect to the database. |
33
|
|
|
* |
34
|
|
|
* Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on the format of the DSN string. |
35
|
|
|
* |
36
|
|
|
* The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as |
37
|
|
|
* `key=value` and concatenated by `;`. For example: |
38
|
|
|
* |
39
|
|
|
* ```php |
40
|
|
|
* $dsn = new Dsn('mysql', '127.0.0.1', 'yiitest', '3306', ['charset' => 'utf8mb4']); |
41
|
|
|
* $pdoDriver = new PDODriver($dsn->asString(), 'username', 'password'); |
42
|
|
|
* $connection = new Connection($pdoDriver, $schemaCache); |
43
|
|
|
* ``` |
44
|
|
|
* |
45
|
|
|
* Will result in the DSN string `mysql:host=127.0.0.1;dbname=yiitest;port=3306;charset=utf8mb4`. |
46
|
|
|
*/ |
47
|
|
|
public function asString(): string |
48
|
|
|
{ |
49
|
|
|
$host = "host=$this->host"; |
50
|
|
|
|
51
|
|
|
if ($this->unixSocket) { |
52
|
|
|
$host = "unix_socket=$this->host"; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$dsn = "$this->driver:" . $host . ';' . "dbname=$this->databaseName"; |
56
|
|
|
|
57
|
|
|
if ($this->port !== null) { |
58
|
|
|
$dsn .= ';' . "port=$this->port"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$parts = []; |
62
|
|
|
|
63
|
|
|
foreach ($this->options as $key => $value) { |
64
|
|
|
$parts[] = "$key=$value"; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!empty($parts)) { |
68
|
|
|
$dsn .= ';' . implode(';', $parts); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $dsn; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string The Data Source Name, or DSN, contains the information required to connect to the database. |
76
|
|
|
*/ |
77
|
|
|
public function __toString(): string |
78
|
|
|
{ |
79
|
|
|
return $this->asString(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string The database name to connect to. |
84
|
|
|
*/ |
85
|
|
|
public function getDatabaseName(): string |
86
|
|
|
{ |
87
|
|
|
return $this->databaseName; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string The database driver name. |
92
|
|
|
*/ |
93
|
|
|
public function getDriver(): string |
94
|
|
|
{ |
95
|
|
|
return $this->driver; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string The database host name or IP address. |
100
|
|
|
*/ |
101
|
|
|
public function getHost(): string |
102
|
|
|
{ |
103
|
|
|
return $this->host; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array The database connection options. Default value to an empty array. |
108
|
|
|
*/ |
109
|
|
|
public function getOptions(): array |
110
|
|
|
{ |
111
|
|
|
return $this->options; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string|null The database port. Null if not set. |
116
|
|
|
*/ |
117
|
|
|
public function getPort(): string|null |
118
|
|
|
{ |
119
|
|
|
return $this->port; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function unixSocket(): static |
123
|
|
|
{ |
124
|
|
|
$this->unixSocket = true; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|