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

Skip to content

[Cache] Using DSN with PDOAdapter #52460

Closed
@HypeMC

Description

@HypeMC

Symfony version(s) affected

>=5.4

Description

According to the docs it should be possible to use DSN to configure the PDOAdapter, how an exception is thrown instead:

  Unsupported DSN: it does not start with "redis[s]:", "memcached:" nor "couchbase:".

This happens because the AbstractAdapter::createConnection() supports only certain adapters:

public static function createConnection(string $dsn, array $options = [])
{
if (str_starts_with($dsn, 'redis:') || str_starts_with($dsn, 'rediss:')) {
return RedisAdapter::createConnection($dsn, $options);
}
if (str_starts_with($dsn, 'memcached:')) {
return MemcachedAdapter::createConnection($dsn, $options);
}
if (0 === strpos($dsn, 'couchbase:')) {
if (CouchbaseBucketAdapter::isSupported()) {
return CouchbaseBucketAdapter::createConnection($dsn, $options);
}
return CouchbaseCollectionAdapter::createConnection($dsn, $options);
}
throw new InvalidArgumentException('Unsupported DSN: it does not start with "redis[s]:", "memcached:" nor "couchbase:".');
}

I'm not sure if this is a bug in the documentation (and possible new feature) or a bug in the code.

How to reproduce

framework:
  cache:
    pools:
      my.cache:
        adapter: cache.adapter.pdo
        provider: "pgsql:host=localhost;port=5432;dbname=postgres;user=postgres;password=pass"
#[AsCommand('app:test')]
class TestCommand extends Command
{
    public function __construct(
        #[Autowire(service: 'my.cache')] private CacheInterface $cache,
    ) {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        return Command::SUCCESS;
    }
}

Possible Solution

Add the necessary logic to AbstractAdapter::createConnection().

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions