Skip to content

Commit 36749d4

Browse files
authored
Merge pull request #812 from Geolim4/master
Improved Couchdb config
2 parents 649fc6b + a1700cb commit 36749d4

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

lib/Phpfastcache/Drivers/Couchdb/Config.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace Phpfastcache\Drivers\Couchdb;
1818

1919
use Phpfastcache\Config\ConfigurationOption;
20+
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
2021

2122
class Config extends ConfigurationOption
2223
{
@@ -66,8 +67,16 @@ public function getDatabase(): string
6667
*/
6768
public function setDatabase(string $database): Config
6869
{
69-
$this->database = $database;
70-
return $this;
70+
/** @see https://docs.couchdb.org/en/latest/api/database/common.html#put--db */
71+
if(\preg_match('#^[a-z][a-z0-9_\-+\$()/]+$#', $database)){
72+
$this->database = $database;
73+
return $this;
74+
}
75+
76+
throw new PhpfastcacheInvalidArgumentException(sprintf(
77+
"Error: illegal_database_name Name: '%s'. Only lowercase characters (a-z), digits (0-9), and any of the characters _, $, (, ), +, -, and / are allowed. Must begin with a letter.",
78+
$database
79+
));
7180
}
7281

7382
/**

lib/Phpfastcache/Drivers/Couchdb/Driver.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
namespace Phpfastcache\Drivers\Couchdb;
1818

19-
use Doctrine\CouchDB\{CouchDBClient, CouchDBException};
19+
use Doctrine\CouchDB\{CouchDBClient, CouchDBException, HTTP\HTTPException};
2020
use Phpfastcache\Cluster\AggregatablePoolInterface;
2121
use Phpfastcache\Core\Pool\{DriverBaseTrait, ExtendedCacheItemPoolInterface};
2222
use Phpfastcache\Entities\DriverStatistic;
@@ -94,7 +94,7 @@ protected function driverConnect(): bool
9494
}
9595
$url .= $clientConfig->getHost();
9696
$url .= ":{$clientConfig->getPort()}";
97-
$url .= '/' . $this->getDatabaseName();
97+
$url .= '/' . \urlencode($this->getDatabaseName());
9898

9999
$this->instance = CouchDBClient::create(
100100
[
@@ -122,7 +122,9 @@ protected function getDatabaseName(): string
122122
*/
123123
protected function createDatabase()
124124
{
125-
if (!in_array($this->getDatabaseName(), $this->instance->getAllDatabases(), true)) {
125+
try{
126+
$this->instance->getDatabaseInfo($this->getDatabaseName());
127+
} catch(HTTPException $e){
126128
$this->instance->createDatabase($this->getDatabaseName());
127129
}
128130
}
@@ -188,7 +190,7 @@ protected function driverWrite(CacheItemInterface $item): bool
188190
*/
189191
protected function getLatestDocumentRevision($docId)
190192
{
191-
$path = '/' . $this->getDatabaseName() . '/' . urlencode($docId);
193+
$path = '/' . \urlencode($this->getDatabaseName()) . '/' . urlencode($docId);
192194

193195
$response = $this->instance->getHttpClient()->request(
194196
'HEAD',

tests/Couchdb.test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
chdir(__DIR__);
1414
require_once __DIR__ . '/../vendor/autoload.php';
1515
$testHelper = new TestHelper('Couchdb driver');
16+
1617
$config = new CouchdbConfig();
18+
$config->setDatabase('phpfastcache($test/-)+1337');
1719
$config->setItemDetailedDate(true);
1820
try{
1921
$cacheInstance = CacheManager::getInstance('Couchdb', $config);

0 commit comments

Comments
 (0)