File tree Expand file tree Collapse file tree 2 files changed +49
-3
lines changed Expand file tree Collapse file tree 2 files changed +49
-3
lines changed Original file line number Diff line number Diff line change @@ -234,9 +234,17 @@ protected function getHostDsn(array $config): string
234
234
$ hosts = is_array ($ config ['host ' ]) ? $ config ['host ' ] : [$ config ['host ' ]];
235
235
236
236
foreach ($ hosts as &$ host ) {
237
- // Check if we need to add a port to the host
238
- if (strpos ($ host , ': ' ) === false && ! empty ($ config ['port ' ])) {
239
- $ host = $ host .': ' .$ config ['port ' ];
237
+ // ipv6
238
+ if (filter_var ($ host , \FILTER_VALIDATE_IP , \FILTER_FLAG_IPV6 )) {
239
+ $ host = '[ ' .$ host .'] ' ;
240
+ if (! empty ($ config ['port ' ])) {
241
+ $ host = $ host .': ' .$ config ['port ' ];
242
+ }
243
+ } else {
244
+ // Check if we need to add a port to the host
245
+ if (! str_contains ($ host , ': ' ) && ! empty ($ config ['port ' ])) {
246
+ $ host = $ host .': ' .$ config ['port ' ];
247
+ }
240
248
}
241
249
}
242
250
Original file line number Diff line number Diff line change @@ -62,6 +62,44 @@ public function dataConnectionConfig(): Generator
62
62
],
63
63
];
64
64
65
+ yield 'IPv4 ' => [
66
+ 'expectedUri ' => 'mongodb://1.2.3.4 ' ,
67
+ 'expectedDatabaseName ' => 'tests ' ,
68
+ 'config ' => [
69
+ 'host ' => '1.2.3.4 ' ,
70
+ 'database ' => 'tests ' ,
71
+ ],
72
+ ];
73
+
74
+ yield 'IPv4 and port ' => [
75
+ 'expectedUri ' => 'mongodb://1.2.3.4:1234 ' ,
76
+ 'expectedDatabaseName ' => 'tests ' ,
77
+ 'config ' => [
78
+ 'host ' => '1.2.3.4 ' ,
79
+ 'port ' => 1234 ,
80
+ 'database ' => 'tests ' ,
81
+ ],
82
+ ];
83
+
84
+ yield 'IPv6 and port ' => [
85
+ 'expectedUri ' => 'mongodb://[2001:db8:3333:4444:5555:6666:7777:8888]:1234 ' ,
86
+ 'expectedDatabaseName ' => 'tests ' ,
87
+ 'config ' => [
88
+ 'host ' => '2001:db8:3333:4444:5555:6666:7777:8888 ' ,
89
+ 'port ' => 1234 ,
90
+ 'database ' => 'tests ' ,
91
+ ],
92
+ ];
93
+
94
+ yield 'IPv6 ' => [
95
+ 'expectedUri ' => 'mongodb://[2001:db8:3333:4444:5555:6666:7777:8888] ' ,
96
+ 'expectedDatabaseName ' => 'tests ' ,
97
+ 'config ' => [
98
+ 'host ' => '2001:db8:3333:4444:5555:6666:7777:8888 ' ,
99
+ 'database ' => 'tests ' ,
100
+ ],
101
+ ];
102
+
65
103
yield 'Port in host name takes precedence ' => [
66
104
'expectedUri ' => 'mongodb://some-host:12345 ' ,
67
105
'expectedDatabaseName ' => 'tests ' ,
You can’t perform that action at this time.
0 commit comments