Skip to content

Commit

Permalink
Allows URL DSN in Lock and Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse committed Oct 27, 2019
1 parent 3f759d7 commit fe58b03
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/PdoAdapterTest.php
Expand Up @@ -71,4 +71,33 @@ public function testCleanupExpiredItems()
$this->assertFalse($newItem->isHit());
$this->assertSame(0, $getCacheItemCount(), 'PDOAdapter must clean up expired items');
}

/**
* @dataProvider provideDsn
*/
public function testDsn(string $dsn, string $file = null)
{
try {
$pool = new PdoAdapter($dsn);
$pool->createTable();

$item = $pool->getItem('key');
$item->set('value');
$this->assertTrue($pool->save($item));
} finally {
if (null !== $file) {
@unlink($file);
}
}
}

public function provideDsn()
{
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
yield ['sqlite://localhost/'.$dbFile, ''.$dbFile];
yield ['sqlite:'.$dbFile, ''.$dbFile];
yield ['sqlite3:///'.$dbFile, ''.$dbFile];
yield ['sqlite://localhost/:memory:'];
yield ['sqlite::memory:'];
}
}
12 changes: 10 additions & 2 deletions src/Symfony/Component/Cache/Traits/PdoTrait.php
Expand Up @@ -14,6 +14,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -370,8 +371,15 @@ protected function doSave(array $values, $lifetime)
private function getConnection()
{
if (null === $this->conn) {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
if (strpos($this->dsn, '://')) {
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
}
$this->conn = DriverManager::getConnection(['url' => $this->dsn]);
} else {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
}
if (null === $this->driver) {
if ($this->conn instanceof \PDO) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/composer.json
Expand Up @@ -39,7 +39,7 @@
"symfony/var-dumper": "^4.1.1|^5.0"
},
"conflict": {
"doctrine/dbal": "<2.5",
"doctrine/dbal": "<2.5.0",
"symfony/dependency-injection": "<3.4",
"symfony/var-dumper": "<3.4"
},
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Component/Lock/Store/PdoStore.php
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\InvalidTtlException;
Expand Down Expand Up @@ -229,8 +230,15 @@ private function getUniqueToken(Key $key): string
private function getConnection()
{
if (null === $this->conn) {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
if (strpos($this->dsn, '://')) {
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
}
$this->conn = DriverManager::getConnection(['url' => $this->dsn]);
} else {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
}

return $this->conn;
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php
Expand Up @@ -73,4 +73,34 @@ public function testInvalidTtlConstruct()

return new PdoStore('sqlite:'.self::$dbFile, [], 0.1, 0.1);
}

/**
* @dataProvider provideDsn
*/
public function testDsn(string $dsn, string $file = null)
{
$key = new Key(uniqid(__METHOD__, true));

try {
$store = new PdoStore($dsn);
$store->createTable();

$store->save($key);
$this->assertTrue($store->exists($key));
} finally {
if (null !== $file) {
@unlink($file);
}
}
}

public function provideDsn()
{
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
yield ['sqlite://localhost/'.$dbFile, ''.$dbFile];
yield ['sqlite:'.$dbFile, ''.$dbFile];
yield ['sqlite3:///'.$dbFile, ''.$dbFile];
yield ['sqlite://localhost/:memory:'];
yield ['sqlite::memory:'];
}
}
5 changes: 4 additions & 1 deletion src/Symfony/Component/Lock/composer.json
Expand Up @@ -20,10 +20,13 @@
"psr/log": "~1.0"
},
"require-dev": {
"doctrine/dbal": "~2.4",
"doctrine/dbal": "~2.5",
"mongodb/mongodb": "~1.1",
"predis/predis": "~1.0"
},
"conflict": {
"doctrine/dbal": "<2.5.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Lock\\": "" },
"exclude-from-classmap": [
Expand Down

0 comments on commit fe58b03

Please sign in to comment.