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\MySQL; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Config\ProvidesSourceString; |
15
|
|
|
|
16
|
|
|
class TcpConnectionConfig extends ConnectionConfig implements ProvidesSourceString |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param non-empty-string $host |
|
|
|
|
20
|
|
|
* @param positive-int $port |
21
|
|
|
* @param non-empty-string $database |
22
|
|
|
* @param non-empty-string|null $charset |
23
|
|
|
* @param non-empty-string|null $user |
24
|
|
|
* @param non-empty-string|null $password |
25
|
|
|
* @param array<int, non-empty-string|non-empty-string> $options |
26
|
|
|
*/ |
27
|
4 |
|
public function __construct( |
28
|
|
|
public string $database, |
29
|
|
|
public string $host = 'localhost', |
30
|
|
|
public int $port = 3307, |
31
|
|
|
public ?string $charset = null, |
32
|
|
|
?string $user = null, |
33
|
|
|
?string $password = null, |
34
|
|
|
array $options = [], |
35
|
|
|
) { |
36
|
4 |
|
parent::__construct($user, $password, $options); |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
484 |
|
public function getSourceString(): string |
43
|
|
|
{ |
44
|
484 |
|
return $this->database; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the MySQL-specific PDO DataSourceName with connection URI, |
49
|
|
|
* that looks like: |
50
|
|
|
* <code> |
51
|
|
|
* mysql:host=localhost;port=3307;dbname=dbname |
52
|
|
|
* </code> |
53
|
|
|
* |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
8 |
|
public function getDsn(): string |
57
|
|
|
{ |
58
|
8 |
|
$config = [ |
59
|
8 |
|
'host' => $this->host, |
60
|
8 |
|
'port' => $this->port, |
61
|
8 |
|
'dbname' => $this->database, |
62
|
8 |
|
'charset' => $this->charset, |
63
|
|
|
]; |
64
|
|
|
|
65
|
8 |
|
return \sprintf('%s:%s', $this->getName(), $this->dsn($config)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function __set_state(array $state): self |
69
|
|
|
{ |
70
|
|
|
return new self(...$state); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|