From 75984c23c4160a2b54a9650fcfab04896f315761 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sun, 7 Jan 2024 15:12:52 +0100 Subject: [PATCH] [Cache] Add support for using DSN with PDOAdapter --- .../Component/Cache/Adapter/AbstractAdapter.php | 5 ++++- src/Symfony/Component/Cache/Adapter/PdoAdapter.php | 12 ++++++++++++ src/Symfony/Component/Cache/CHANGELOG.md | 1 + .../Component/Cache/Tests/Adapter/PdoAdapterTest.php | 11 +++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php index ed90f4716c7f2..68db8c16959eb 100644 --- a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php @@ -124,8 +124,11 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra return CouchbaseCollectionAdapter::createConnection($dsn, $options); } + if (preg_match('/^(mysql|oci|pgsql|sqlsrv|sqlite):/', $dsn)) { + return PdoAdapter::createConnection($dsn, $options); + } - throw new InvalidArgumentException('Unsupported DSN: it does not start with "redis[s]:", "memcached:" nor "couchbase:".'); + throw new InvalidArgumentException('Unsupported DSN: it does not start with "redis[s]:", "memcached:", "couchbase:", "mysql:", "oci:", "pgsql:", "sqlsrv:" nor "sqlite:".'); } public function commit(): bool diff --git a/src/Symfony/Component/Cache/Adapter/PdoAdapter.php b/src/Symfony/Component/Cache/Adapter/PdoAdapter.php index 94a80fe52a06d..0dd1b168bbb1f 100644 --- a/src/Symfony/Component/Cache/Adapter/PdoAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/PdoAdapter.php @@ -89,6 +89,18 @@ public function __construct(#[\SensitiveParameter] \PDO|string $connOrDsn, strin parent::__construct($namespace, $defaultLifetime); } + public static function createConnection(#[\SensitiveParameter] string $dsn, array $options = []): \PDO|string + { + if ($options['lazy'] ?? true) { + return $dsn; + } + + $pdo = new \PDO($dsn); + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + + return $pdo; + } + /** * Creates the table to store cache items which can be called once for setup. * diff --git a/src/Symfony/Component/Cache/CHANGELOG.md b/src/Symfony/Component/Cache/CHANGELOG.md index a60d58aaa3603..a6b391de1f25a 100644 --- a/src/Symfony/Component/Cache/CHANGELOG.md +++ b/src/Symfony/Component/Cache/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * Add option `sentinel_master` as an alias for `redis_sentinel` * Deprecate `CouchbaseBucketAdapter`, use `CouchbaseCollectionAdapter` * Add support for URL encoded characters in Couchbase DSN + * Add support for using DSN with PDOAdapter 7.0 --- diff --git a/src/Symfony/Component/Cache/Tests/Adapter/PdoAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/PdoAdapterTest.php index f5e1da81cae67..48ec520debd16 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/PdoAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/PdoAdapterTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Cache\Tests\Adapter; use Psr\Cache\CacheItemPoolInterface; +use Symfony\Component\Cache\Adapter\AbstractAdapter; use Symfony\Component\Cache\Adapter\PdoAdapter; /** @@ -41,6 +42,16 @@ public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterfac return new PdoAdapter('sqlite:'.self::$dbFile, 'ns', $defaultLifetime); } + public function testCreateConnectionReturnsStringWithLazyTrue() + { + self::assertSame('sqlite:'.self::$dbFile, AbstractAdapter::createConnection('sqlite:'.self::$dbFile)); + } + + public function testCreateConnectionReturnsPDOWithLazyFalse() + { + self::assertInstanceOf(\PDO::class, AbstractAdapter::createConnection('sqlite:'.self::$dbFile, ['lazy' => false])); + } + public function testCleanupExpiredItems() { $pdo = new \PDO('sqlite:'.self::$dbFile);