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\SQLServer; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Config\ProvidesSourceString; |
15
|
|
|
use Cycle\Database\Config\Support\DataSourceName; |
16
|
|
|
|
17
|
|
|
class DsnConnectionConfig extends ConnectionConfig implements ProvidesSourceString |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string|null |
21
|
|
|
*/ |
22
|
|
|
private ?string $database = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var non-empty-string |
|
|
|
|
26
|
|
|
* @psalm-allow-private-mutation |
27
|
|
|
*/ |
28
|
|
|
public string $dsn; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param non-empty-string|\Stringable $dsn |
|
|
|
|
32
|
|
|
* @param non-empty-string|null $user |
33
|
|
|
* @param non-empty-string|null $password |
34
|
|
|
* @param array $options |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
string|\Stringable $dsn, |
38
|
|
|
?string $user = null, |
39
|
|
|
?string $password = null, |
40
|
|
|
array $options = [] |
41
|
|
|
) { |
42
|
|
|
parent::__construct($user, $password, $options); |
43
|
|
|
|
44
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */ |
45
|
|
|
$this->dsn = DataSourceName::normalize((string)$dsn, $this->getName()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public function getSourceString(): string |
52
|
|
|
{ |
53
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */ |
54
|
|
|
return $this->database ??= DataSourceName::read($this->getDsn(), 'database') ?? '*'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function getDsn(): string |
61
|
|
|
{ |
62
|
|
|
return $this->dsn; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function __set_state(array $an_array): object |
66
|
|
|
{ |
67
|
|
|
return new self( |
68
|
|
|
dsn: $an_array['dsn'], |
69
|
|
|
user: $an_array['user'], |
70
|
|
|
password: $an_array['password'], |
71
|
|
|
options: $an_array['options'], |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|