Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public function testFromDsn()
);
}

public function testFromDsnOnUnixSocket()
{
$this->assertEquals(
new Connection(['stream' => 'queue'], [
'host' => '/var/run/redis/redis.sock',
'port' => 0,
], [], $redis = $this->createMock(\Redis::class)),
Connection::fromDsn('redis:///var/run/redis/redis.sock', ['stream' => 'queue'], $redis)
);
}

public function testFromDsnWithOptions()
{
$this->assertEquals(
Expand Down
52 changes: 33 additions & 19 deletions src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,15 @@ public function __construct(array $configuration, array $connectionCredentials =

public static function fromDsn(string $dsn, array $redisOptions = [], \Redis $redis = null): self
{
if (false === $parsedUrl = parse_url($dsn)) {
throw new InvalidArgumentException(sprintf('The given Redis DSN "%s" is invalid.', $dsn));
}

$pathParts = explode('/', $parsedUrl['path'] ?? '');

$stream = $pathParts[1] ?? $redisOptions['stream'] ?? null;
$group = $pathParts[2] ?? $redisOptions['group'] ?? null;
$consumer = $pathParts[3] ?? $redisOptions['consumer'] ?? null;
$url = $dsn;

$connectionCredentials = [
'host' => $parsedUrl['host'] ?? '127.0.0.1',
'port' => $parsedUrl['port'] ?? 6379,
'auth' => $parsedUrl['pass'] ?? $parsedUrl['user'] ?? null,
];
if (preg_match('#^redis:///([^:@])+$#', $dsn)) {
$url = str_replace('redis:', 'file:', $dsn);
}

if (false === $parsedUrl = parse_url($url)) {
throw new InvalidArgumentException(sprintf('The given Redis DSN "%s" is invalid.', $dsn));
}
if (isset($parsedUrl['query'])) {
parse_str($parsedUrl['query'], $redisOptions);
}
Expand All @@ -111,14 +104,35 @@ public static function fromDsn(string $dsn, array $redisOptions = [], \Redis $re
unset($redisOptions['dbindex']);
}

return new self([
'stream' => $stream,
'group' => $group,
'consumer' => $consumer,
$configuration = [
'stream' => $redisOptions['stream'] ?? null,
'group' => $redisOptions['group'] ?? null,
'consumer' => $redisOptions['consumer'] ?? null,
'auto_setup' => $autoSetup,
'stream_max_entries' => $maxEntries,
'dbindex' => $dbIndex,
], $connectionCredentials, $redisOptions, $redis);
];

if (isset($parsedUrl['host'])) {
$connectionCredentials = [
'host' => $parsedUrl['host'] ?? '127.0.0.1',
'port' => $parsedUrl['port'] ?? 6379,
'auth' => $parsedUrl['pass'] ?? $parsedUrl['user'] ?? null,
];

$pathParts = explode('/', $parsedUrl['path'] ?? '');

$configuration['stream'] = $pathParts[1] ?? $configuration['stream'];
$configuration['group'] = $pathParts[2] ?? $configuration['group'];
$configuration['consumer'] = $pathParts[3] ?? $configuration['consumer'];
} else {
$connectionCredentials = [
'host' => $parsedUrl['path'],
'port' => 0,
];
}

return new self($configuration, $connectionCredentials, $redisOptions, $redis);
}

public function get(): ?array
Expand Down