Skip to content

Commit b004c3c

Browse files
committed
feature #53448 [Cache] Add support for using DSN with PDOAdapter (HypeMC)
This PR was merged into the 7.1 branch. Discussion ---------- [Cache] Add support for using DSN with `PDOAdapter` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #52460 | License | MIT Adds support for using DSN with the `PDOAdapter`: ```yaml framework: cache: pools: my.cache: adapter: cache.adapter.pdo provider: "pgsql:host=localhost;port=5432;dbname=postgres;user=postgres;password=pass" ``` Commits ------- 75984c2 [Cache] Add support for using DSN with PDOAdapter
2 parents 82426c4 + 75984c2 commit b004c3c

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
124124

125125
return CouchbaseCollectionAdapter::createConnection($dsn, $options);
126126
}
127+
if (preg_match('/^(mysql|oci|pgsql|sqlsrv|sqlite):/', $dsn)) {
128+
return PdoAdapter::createConnection($dsn, $options);
129+
}
127130

128-
throw new InvalidArgumentException('Unsupported DSN: it does not start with "redis[s]:", "memcached:" nor "couchbase:".');
131+
throw new InvalidArgumentException('Unsupported DSN: it does not start with "redis[s]:", "memcached:", "couchbase:", "mysql:", "oci:", "pgsql:", "sqlsrv:" nor "sqlite:".');
129132
}
130133

131134
public function commit(): bool

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

+12
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ public function __construct(#[\SensitiveParameter] \PDO|string $connOrDsn, strin
8989
parent::__construct($namespace, $defaultLifetime);
9090
}
9191

92+
public static function createConnection(#[\SensitiveParameter] string $dsn, array $options = []): \PDO|string
93+
{
94+
if ($options['lazy'] ?? true) {
95+
return $dsn;
96+
}
97+
98+
$pdo = new \PDO($dsn);
99+
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
100+
101+
return $pdo;
102+
}
103+
92104
/**
93105
* Creates the table to store cache items which can be called once for setup.
94106
*

src/Symfony/Component/Cache/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add option `sentinel_master` as an alias for `redis_sentinel`
88
* Deprecate `CouchbaseBucketAdapter`, use `CouchbaseCollectionAdapter`
99
* Add support for URL encoded characters in Couchbase DSN
10+
* Add support for using DSN with PDOAdapter
1011

1112
7.0
1213
---

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

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

1414
use Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
1516
use Symfony\Component\Cache\Adapter\PdoAdapter;
1617

1718
/**
@@ -41,6 +42,16 @@ public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterfac
4142
return new PdoAdapter('sqlite:'.self::$dbFile, 'ns', $defaultLifetime);
4243
}
4344

45+
public function testCreateConnectionReturnsStringWithLazyTrue()
46+
{
47+
self::assertSame('sqlite:'.self::$dbFile, AbstractAdapter::createConnection('sqlite:'.self::$dbFile));
48+
}
49+
50+
public function testCreateConnectionReturnsPDOWithLazyFalse()
51+
{
52+
self::assertInstanceOf(\PDO::class, AbstractAdapter::createConnection('sqlite:'.self::$dbFile, ['lazy' => false]));
53+
}
54+
4455
public function testCleanupExpiredItems()
4556
{
4657
$pdo = new \PDO('sqlite:'.self::$dbFile);

0 commit comments

Comments
 (0)