@@ -67,10 +67,7 @@ public static function createConnection($dsn, array $options = [])
67
67
68
68
set_error_handler (function ($ type , $ msg , $ file , $ line ): bool { throw new \ErrorException ($ msg , 0 , $ type , $ file , $ line ); });
69
69
70
- $ dsnPattern = '/^(?<protocol>couchbase(?:s)?)\:\/\/(?:(?<username>[^\:]+)\:(?<password>[^\@]{6,})@)? '
71
- .'(?<host>[^\:]+(?:\:\d+)?)(?:\/(?<bucketName>[^\/\?]+))(?:(?:\/(?<scopeName>[^\/]+)) '
72
- .'(?:\/(?<collectionName>[^\/\?]+)))?(?:\/)?(?:\?(?<options>.*))?$/i ' ;
73
-
70
+ $ pathPattern = '/^(?:\/(?<bucketName>[^\/\?]+))(?:(?:\/(?<scopeName>[^\/]+))(?:\/(?<collectionName>[^\/\?]+)))?(?:\/)?$/ ' ;
74
71
$ newServers = [];
75
72
$ protocol = 'couchbase ' ;
76
73
try {
@@ -82,31 +79,32 @@ public static function createConnection($dsn, array $options = [])
82
79
throw new InvalidArgumentException ('Invalid Couchbase DSN: it does not start with "couchbase:". ' );
83
80
}
84
81
85
- preg_match ( $ dsnPattern , $ server , $ matches );
82
+ $ parsed = parse_url ( $ server );
86
83
87
- $ username = $ matches [ ' username ' ] ?: $ username ;
88
- $ password = $ matches [ ' password ' ] ?: $ password ;
89
- $ protocol = $ matches [ ' protocol ' ] ?: $ protocol ;
84
+ $ username = $ parsed [ ' user ' ] ?? $ username ;
85
+ $ password = rawurldecode ( $ parsed [ ' pass ' ] ?? $ password) ;
86
+ $ protocol = $ parsed [ ' scheme ' ] ?? $ protocol ;
90
87
91
- if (isset ($ matches [ ' options ' ])) {
92
- $ optionsInDsn = self ::getOptions ($ matches [ ' options ' ]);
88
+ if (isset ($ parsed [ ' query ' ])) {
89
+ $ optionsInDsn = self ::getOptions ($ parsed [ ' query ' ]);
93
90
94
91
foreach ($ optionsInDsn as $ parameter => $ value ) {
95
92
$ options [$ parameter ] = $ value ;
96
93
}
97
94
}
98
95
99
- $ newServers [] = $ matches ['host ' ];
96
+ $ newServers [] = $ parsed ['host ' ];
100
97
}
101
98
102
- $ option = isset ($ matches [ ' options ' ]) ? '? ' .$ matches [ ' options ' ] : '' ;
99
+ $ option = isset ($ parsed [ ' query ' ]) ? '? ' .$ parsed [ ' query ' ] : '' ;
103
100
$ connectionString = $ protocol .':// ' .implode (', ' , $ newServers ).$ option ;
104
101
105
102
$ clusterOptions = new ClusterOptions ();
106
103
$ clusterOptions ->credentials ($ username , $ password );
107
104
108
105
$ client = new Cluster ($ connectionString , $ clusterOptions );
109
106
107
+ preg_match ($ pathPattern , $ parsed ['path ' ] ?? '' , $ matches );
110
108
$ bucket = $ client ->bucket ($ matches ['bucketName ' ]);
111
109
$ collection = $ bucket ->defaultCollection ();
112
110
if (!empty ($ matches ['scopeName ' ])) {
@@ -122,7 +120,7 @@ public static function createConnection($dsn, array $options = [])
122
120
123
121
public static function isSupported (): bool
124
122
{
125
- return \extension_loaded ('couchbase ' ) && version_compare (phpversion ('couchbase ' ), '3.0.5 ' , '>= ' ) && version_compare (phpversion ('couchbase ' ), '4.0 ' , '< ' );
123
+ return \extension_loaded ('couchbase ' ) && version_compare (phpversion ('couchbase ' ), '3.0.4 ' , '>= ' ) && version_compare (phpversion ('couchbase ' ), '4.0 ' , '< ' );
126
124
}
127
125
128
126
private static function getOptions (string $ options ): array
0 commit comments