8000 [Cache] Add support for URL encoded characters in Couchbase DSN · symfony/symfony@ea1ccc4 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit ea1ccc4

Browse files
[Cache] Add support for URL encoded characters in Couchbase DSN
1 parent fe482a4 commit ea1ccc4

File tree

6 files changed

+37
-18
lines changed

6 files changed

+37
-18
lines changed

.github/workflows/integration-tests.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ jobs:
132132
133133
- name: Configure Couchbase
134134
run: |
135-
curl -s -u 'username=Administrator&password=111111' -X POST http://localhost:8091/node/controller/setupServices -d 'services=kv%2Cn1ql%2Cindex%2Cfts'
136-
curl -s -X POST http://localhost:8091/settings/web -d 'username=Administrator&password=111111&port=SAME'
137-
curl -s -u Administrator:111111 -X POST http://localhost:8091/pools/default/buckets -d 'ramQuotaMB=100&bucketType=ephemeral&name=cache'
138-
curl -s -u Administrator:111111 -X POST http://localhost:8091/pools/default -d 'memoryQuota=256'
135+
curl -s -u 'username=Administrator&password=111111@' -X POST http://localhost:8091/node/controller/setupServices -d 'services=kv%2Cn1ql%2Cindex%2Cfts'
136+
curl -s -X POST http://localhost:8091/settings/web -d 'username=Administrator&password=111111%40&port=SAME'
137+
curl -s -u Administrator:111111@ -X POST http://localhost:8091/pools/default/buckets -d 'ramQuotaMB=100&bucketType=ephemeral&name=cache'
138+
curl -s -u Administrator:111111@ -X POST http://localhost:8091/pools/default -d 'memoryQuota=256'
139139
140140
- name: Setup PHP
141141
uses: shivammathur/setup-php@v2

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<env name="ZOOKEEPER_HOST" value="localhost" />
2626
<env name="COUCHBASE_HOST" value="localhost" />
2727
<env name="COUCHBASE_USER" value="Administrator" />
28-
<env name="COUCHBASE_PASS" value="111111" />
28+
<env name="COUCHBASE_PASS" value="111111%40" />
2929
</php>
3030

3131
<testsuites>

src/Symfony/Component/Cache/Adapter/CouchbaseCollectionAdapter.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $dsn
5959

6060
set_error_handler(static fn ($type, $msg, $file, $line) => throw new \ErrorException($msg, 0, $type, $file, $line));
6161

62-
$dsnPattern = '/^(?<protocol>couchbase(?:s)?)\:\/\/(?:(?<username>[^\:]+)\:(?<password>[^\@]{6,})@)?'
63-
.'(?<host>[^\:]+(?:\:\d+)?)(?:\/(?<bucketName>[^\/\?]+))(?:(?:\/(?<scopeName>[^\/]+))'
64-
.'(?:\/(?<collectionName>[^\/\?]+)))?(?:\/)?(?:\?(?<options>.*))?$/i';
65-
62+
$pathPattern = '/^(?:\/(?<bucketName>[^\/\?]+))(?:(?:\/(?<scopeName>[^\/]+))(?:\/(?<collectionName>[^\/\?]+)))?(?:\/)?$/';
6663
$newServers = [];
6764
$protocol = 'couchbase';
6865
try {
@@ -74,31 +71,32 @@ public static function createConnection(#[\SensitiveParameter] array|string $dsn
7471
throw new InvalidArgumentException('Invalid Couchbase DSN: it does not start with "couchbase:".');
7572
}
7673

77-
preg_match($dsnPattern, $server, $matches);
74+
$params = parse_url($server);
7875

79-
$username = $matches['username'] ?: $username;
80-
$password = $matches['password'] ?: $password;
81-
$protocol = $matches['protocol'] ?: $protocol;
76+
$username = $params['user'] ?? $username;
77+
$password = rawurldecode($params['pass'] ?? $password);
78+
$protocol = $params['scheme'] ?? $protocol;
8279

83-
if (isset($matches['options'])) {
84-
$optionsInDsn = self::getOptions($matches['options']);
80+
if (isset($params['query'])) {
81+
$optionsInDsn = self::getOptions($params['query']);
8582

8683
foreach ($optionsInDsn as $parameter => $value) {
8784
$options[$parameter] = $value;
8885
}
8986
}
9087

91-
$newServers[] = $matches['host'];
88+
$newServers[] = $params['host'];
9289
}
9390

94-
$option = isset($matches['options']) ? '?'.$matches['options'] : '';
91+
$option = isset($params['query']) ? '?'.$params['query'] : '';
9592
$connectionString = $protocol.'://'.implode(',', $newServers).$option;
9693

9794
$clusterOptions = new ClusterOptions();
9895
$clusterOptions->credentials($username, $password);
9996

10097
$client = new Cluster($connectionString, $clusterOptions);
10198

99+
preg_match($pathPattern, $params['path'] ?? '', $matches);
102100
$bucket = $client->bucket($matches['bucketName']);
103101
$collection = $bucket->defaultCollecti 6D40 on();
104102
if (!empty($matches['scopeName'])) {

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add option `sentinel_master` as an alias for `redis_sentinel`
88
* Deprecate `CouchbaseBucketAdapter`, use `CouchbaseCollectionAdapter`
9+
* Add support for URL encoded characters in Couchbase DSN
910

1011
7.0
1112
---

src/Symfony/Component/Cache/Tests/Adapter/CouchbaseCollectionAdapterTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,24 @@ public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
4242

4343
return new CouchbaseCollectionAdapter($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
4444
}
45+
46+
/**
47+
* Couchbase consider expiration time greater than 30 days as an absolute timestamp.
48+
* This test case overrides parent to avoid this behavior for the "k2" item.
49+
*/
50+
public function testExpiration()
51+
{
52+
$cache = $this->createCachePool();
53+
$cache->save($cache->getItem('k1')->set('v1')->expiresAfter(2));
54+
$cache->save($cache->getItem('k2')->set('v2')->expiresAfter(86400));
55+
56+
sleep(3);
57+
$item = $cache->getItem('k1');
58+
$this->assertFalse($item->isHit());
59+
$this->assertNull($item->get(), "Item's value must be null when isHit() is false.");
60+
61+
$item = $cache->getItem('k2');
62+
$this->assertTrue($item->isHit());
63+
$this->assertSame('v2', $item->get());
64+
}
4565
}

src/Symfony/Component/Cache/phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<env name="MEMCACHED_HOST" value="localhost" />
1616
<env name="COUCHBASE_HOST" value="localhost" />
1717
<env name="COUCHBASE_USER" value="Administrator" />
18-
<env name="COUCHBASE_PASS" value="111111" />
18+
<env name="COUCHBASE_PASS" value="111111%40" />
1919
</php>
2020

2121
<testsuites>

0 commit comments

Comments
 (0)
0