Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2c625e

Browse files
feature #34057 [Lock][Cache] Allows URL DSN in PDO adapters (jderusse)
This PR was merged into the 4.4 branch. Discussion ---------- [Lock][Cache] Allows URL DSN in PDO adapters | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | TODO This PR duplicate a feature from PdoSessionHandler that convert URL DSN ( ie. mysql://localhost/test) into PDO DSN (ie. mysql:host=localhost;dbname=test) that would ease configuration by using the same well-known variable ``` framework: lock: '%env(DATABASE_URL)%' ``` note: I applied the same change on Cache component for consistency. Commits ------- 474daf9 Allows URL DSN in Lock and Cache
2 parents 3b11a76 + 474daf9 commit e2c625e

File tree

5 files changed

+83
-5
lines changed

5 files changed

+83
-5
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,33 @@ public function testCleanupExpiredItems()
7171
$this->assertFalse($newItem->isHit());
7272
$this->assertSame(0, $getCacheItemCount(), 'PDOAdapter must clean up expired items');
7373
}
74+
75+
/**
76+
* @dataProvider provideDsn
77+
*/
78+
public function testDsn(string $dsn, string $file = null)
79+
{
80+
try {
81+
$pool = new PdoAdapter($dsn);
82+
$pool->createTable();
83+
84+
$item = $pool->getItem('key');
85+
$item->set('value');
86+
$this->assertTrue($pool->save($item));
87+
} finally {
88+
if (null !== $file) {
89+
@unlink($file);
90+
}
91+
}
92+
}
93+
94+
public function provideDsn()
95+
{
96+
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
97+
yield ['sqlite://localhost/'.$dbFile, ''.$dbFile];
98+
yield ['sqlite:'.$dbFile, ''.$dbFile];
99+
yield ['sqlite3:///'.$dbFile, ''.$dbFile];
100+
yield ['sqlite://localhost/:memory:'];
101+
yield ['sqlite::memory:'];
102+
}
74103
}

src/Symfony/Component/Cache/Traits/PdoTrait.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\DBAL\Connection;
1515
use Doctrine\DBAL\DBALException;
1616
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
17+
use Doctrine\DBAL\DriverManager;
1718
use Doctrine\DBAL\Exception\TableNotFoundException;
1819
use Doctrine\DBAL\Schema\Schema;
1920
use Symfony\Component\Cache\Exception\InvalidArgumentException;
@@ -370,8 +371,15 @@ protected function doSave(array $values, $lifetime)
370371
private function getConnection()
371372
{
372373
if (null === $this->conn) {
373-
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
374-
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
374+
if (strpos($this->dsn, '://')) {
375+
if (!class_exists(DriverManager::class)) {
376+
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
377+
}
378+
$this->conn = DriverManager::getConnection(['url' => $this->dsn]);
379+
} else {
380+
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
381+
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
382+
}
375383
}
376384
if (null === $this->driver) {
377385
if ($this->conn instanceof \PDO) {

src/Symfony/Component/Lock/Store/PdoStore.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\DBAL\Connection;
1515
use Doctrine\DBAL\DBALException;
16+
use Doctrine\DBAL\DriverManager;
1617
use Doctrine\DBAL\Schema\Schema;
1718
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1819
use Symfony\Component\Lock\Exception\InvalidTtlException;
@@ -229,8 +230,15 @@ private function getUniqueToken(Key $key): string
229230
private function getConnection()
230231
{
231232
if (null === $this->conn) {
232-
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
233-
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
233+
if (strpos($this->dsn, '://')) {
234+
if (!class_exists(DriverManager::class)) {
235+
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
236+
}
237+
$this->conn = DriverManager::getConnection(['url' => $this->dsn]);
238+
} else {
239+
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
240+
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
241+
}
234242
}
235243

236244
return $this->conn;

src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,34 @@ public function testInvalidTtlConstruct()
7373

7474
return new PdoStore('sqlite:'.self::$dbFile, [], 0.1, 0.1);
7575
}
76+
77+
/**
78+
* @dataProvider provideDsn
79+
*/
80+
public function testDsn(string $dsn, string $file = null)
81+
{
82+
$key = new Key(uniqid(__METHOD__, true));
83+
84+
try {
85+
$store = new PdoStore($dsn);
86+
$store->createTable();
87+
88+
$store->save($key);
89+
$this->assertTrue($store->exists($key));
90+
} finally {
91+
if (null !== $file) {
92+
@unlink($file);
93+
}
94+
}
95+
}
96+
97+
public function provideDsn()
98+
{
99+
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
100+
yield ['sqlite://localhost/'.$dbFile, ''.$dbFile];
101+
yield ['sqlite:'.$dbFile, ''.$dbFile];
102+
yield ['sqlite3:///'.$dbFile, ''.$dbFile];
103+
yield ['sqlite://localhost/:memory:'];
104+
yield ['sqlite::memory:'];
105+
}
76106
}

src/Symfony/Component/Lock/composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
"psr/log": "~1.0"
2121
},
2222
"require-dev": {
23-
"doctrine/dbal": "~2.4",
23+
"doctrine/dbal": "~2.5",
2424
"mongodb/mongodb": "~1.1",
2525
"predis/predis": "~1.0"
2626
},
27+
"conflict": {
28+
"doctrine/dbal": "<2.5"
29+
},
2730
"autoload": {
2831
"psr-4": { "Symfony\\Component\\Lock\\": "" },
2932
"exclude-from-classmap": [

0 commit comments

Comments
 (0)