1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver\AbstractOracleDriver; |
6
|
|
|
|
7
|
|
|
use function implode; |
8
|
|
|
use function is_array; |
9
|
|
|
use function sprintf; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Represents an Oracle Easy Connect string |
13
|
|
|
* |
14
|
|
|
* @link https://docs.oracle.com/database/121/NETAG/naming.htm |
15
|
|
|
*/ |
16
|
|
|
final class EasyConnectString |
17
|
|
|
{ |
18
|
|
|
/** @var string */ |
19
|
|
|
private $string; |
20
|
|
|
|
21
|
|
|
private function __construct(string $string) |
22
|
|
|
{ |
23
|
|
|
$this->string = $string; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function __toString() : string |
27
|
|
|
{ |
28
|
|
|
return $this->string; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Creates the object from an array representation |
33
|
|
|
* |
34
|
|
|
* @param mixed[] $params |
35
|
|
|
*/ |
36
|
|
|
public static function fromArray(array $params) : self |
37
|
|
|
{ |
38
|
|
|
return new self(self::renderParams($params)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Creates the object from the given DBAL connection parameters. |
43
|
|
|
* |
44
|
|
|
* @param mixed[] $params |
45
|
|
|
*/ |
46
|
|
|
public static function fromConnectionParameters(array $params) : self |
47
|
|
|
{ |
48
|
|
|
if (! empty($params['connectstring'])) { |
49
|
|
|
return new self($params['connectstring']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (empty($params['host'])) { |
53
|
|
|
return new self($params['dbname'] ?? ''); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$connectData = []; |
57
|
|
|
|
58
|
|
|
if (isset($params['servicename']) || isset($params['dbname'])) { |
59
|
|
|
$serviceKey = 'SID'; |
60
|
|
|
|
61
|
|
|
if (! empty($params['service'])) { |
62
|
|
|
$serviceKey = 'SERVICE_NAME'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$serviceName = $params['servicename'] ?? $params['dbname']; |
66
|
|
|
|
67
|
|
|
$connectData[$serviceKey] = $serviceName; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (! empty($params['instancename'])) { |
71
|
|
|
$connectData['INSTANCE_NAME'] = $params['instancename']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (! empty($params['pooled'])) { |
75
|
|
|
$connectData['SERVER'] = 'POOLED'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return self::fromArray([ |
79
|
|
|
'DESCRIPTION' => [ |
80
|
|
|
'ADDRESS' => [ |
81
|
|
|
'PROTOCOL' => 'TCP', |
82
|
|
|
'HOST' => $params['host'], |
83
|
|
|
'PORT' => $params['port'] ?? 1521, |
84
|
|
|
], |
85
|
|
|
'CONNECT_DATA' => $connectData, |
86
|
|
|
], |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed[] $params |
92
|
|
|
*/ |
93
|
|
|
private static function renderParams(array $params) : string |
94
|
|
|
{ |
95
|
|
|
$chunks = []; |
96
|
|
|
|
97
|
|
|
foreach ($params as $key => $value) { |
98
|
|
|
$string = self::renderValue($value); |
99
|
|
|
|
100
|
|
|
if ($string === '') { |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$chunks[] = sprintf('(%s=%s)', $key, $string); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return implode('', $chunks); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param mixed $value |
112
|
|
|
*/ |
113
|
|
|
private static function renderValue($value) : string |
114
|
|
|
{ |
115
|
|
|
if (is_array($value)) { |
116
|
|
|
return self::renderParams($value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return (string) $value; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|