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
|
956 |
|
final class Dsn implements Stringable |
16
|
|
|
{ |
17
|
956 |
|
public function __construct( |
18
|
956 |
|
private string $driver, |
19
|
956 |
|
private string $host, |
20
|
956 |
|
private string $databaseName, |
21
|
956 |
|
private string|null $port = null, |
22
|
956 |
|
private array $options = [] |
23
|
|
|
) { |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return string The Data Source Name, or DSN, contains the information required to connect to the database. |
28
|
|
|
* |
29
|
|
|
* Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on the format of the DSN string. |
30
|
|
|
* |
31
|
|
|
* The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as |
32
|
|
|
* `key=value` and concatenated by `;`. For example: |
33
|
|
|
* |
34
|
|
|
* ```php |
35
|
|
|
* $dsn = new Dsn('mysql', '127.0.0.1', 'yiitest', '3306'); |
36
|
|
|
* $connection = new Connection($this->cache, $this->logger, $this->profiler, $dsn->getDsn()); |
37
|
|
|
* ``` |
38
|
956 |
|
* |
39
|
|
|
* Will result in the DSN string `mysql:host=127.0.0.1;dbname=yiitest;port=3306`. |
40
|
956 |
|
*/ |
41
|
|
|
public function asString(): string |
42
|
956 |
|
{ |
43
|
956 |
|
$dsn = "$this->driver:" . "host=$this->host" . ';' . "dbname=$this->databaseName"; |
44
|
|
|
|
45
|
|
|
if ($this->port !== null) { |
46
|
956 |
|
$dsn .= ';' . "port=$this->port"; |
47
|
|
|
} |
48
|
956 |
|
|
49
|
|
|
$parts = []; |
50
|
|
|
|
51
|
|
|
/** @psalm-var string[] */ |
52
|
956 |
|
$options = $this->options; |
53
|
|
|
|
54
|
|
|
foreach ($options as $key => $value) { |
55
|
|
|
$parts[] = "$key=$value"; |
56
|
956 |
|
} |
57
|
|
|
|
58
|
|
|
if (!empty($parts)) { |
59
|
|
|
$dsn .= ';' . implode(';', $parts); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $dsn; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string The Data Source Name, or DSN, contains the information required to connect to the database. |
67
|
|
|
*/ |
68
|
|
|
public function __toString(): string |
69
|
|
|
{ |
70
|
|
|
return $this->asString(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string The database name to connect to. |
75
|
|
|
*/ |
76
|
|
|
public function getDatabaseName(): string |
77
|
|
|
{ |
78
|
|
|
return $this->databaseName; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string The database driver name. |
83
|
|
|
*/ |
84
|
|
|
public function getDriver(): string |
85
|
|
|
{ |
86
|
|
|
return $this->driver; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array The database host name or IP address. |
91
|
|
|
*/ |
92
|
|
|
public function getHost(): string |
93
|
|
|
{ |
94
|
|
|
return $this->host; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array The database connection options. Default value to an empty array. |
99
|
|
|
*/ |
100
|
|
|
public function getOptions(): array |
101
|
|
|
{ |
102
|
|
|
return $this->options; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array The database port. Null if not set. |
107
|
|
|
*/ |
108
|
|
|
public function getPort(): string|null |
109
|
|
|
{ |
110
|
|
|
return $this->port; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|