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

Skip to content

[Cache] Add support for using DSN with PDOAdapter #53448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Cache/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/PdoAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down