8000 feature #35295 [Messenger] Messenger redis local sock dsn (JJarrie) · symfony/symfony@af4035d · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit af4035d

Browse files
committed
feature #35295 [Messenger] Messenger redis local sock dsn (JJarrie)
This PR was squashed before being merged into the 5.1-dev branch (closes #35295). Discussion ---------- [Messenger] Messenger redis local sock dsn | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #34682 | License | MIT | Doc PR | symfony/symfony-docs#12924 Enable connection by unix socket to Redis. ``` messenger.yaml framework: messenger: transports: async: "redis:///var/run/redis/redis.sock" ``` Commits ------- 0421e01 [Messenger] Messenger redis local sock dsn
2 parents b350c80 + 0421e01 commit af4035d

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ public function testFromDsn()
5454
);
5555
}
5656

57+
public function testFromDsnOnUnixSocket()
58+
{
59+
$this->assertEquals(
60+
new Connection(['stream' => 'queue'], [
61+
'host' => '/var/run/redis/redis.sock',
62+
'port' => 0,
63+
], [], $redis = $this->createMock(\Redis::class)),
< 8000 code>64+
Connection::fromDsn('redis:///var/run/redis/redis.sock', ['stream' => 'queue'], $redis)
65+
);
66+
}
67+
5768
public function testFromDsnWithOptions()
5869
{
5970
$this->assertEquals(

src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,15 @@ public function __construct(array $configuration, array $connectionCredentials =
7373

7474
public static function fromDsn(string $dsn, array $redisOptions = [], \Redis $redis = null): self
7575
{
76-
if (false === $parsedUrl = parse_url($dsn)) {
77-
throw new InvalidArgumentException(sprintf('The given Redis DSN "%s" is invalid.', $dsn));
78-
}
79-
80-
$pathParts = explode('/', $parsedUrl['path'] ?? '');
81-
82-
$stream = $pathParts[1] ?? $redisOptions['stream'] ?? null;
83-
$group = $pathParts[2] ?? $redisOptions['group'] ?? null;
84-
$consumer = $pathParts[3] ?? $redisOptions['consumer'] ?? null;
76+
$url = $dsn;
8577

86-
$connectionCredentials = [
87-
'host' => $parsedUrl['host'] ?? '127.0.0.1',
88-
'port' => $parsedUrl['port'] ?? 6379,
89-
'auth' => $parsedUrl['pass'] ?? $parsedUrl['user'] ?? null,
90-
];
78+
if (preg_match('#^redis:///([^:@])+$#', $dsn)) {
79+
$url = str_replace('redis:', 'file:', $dsn);
80+
}
9181

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

114-
return new self([
115-
'stream' => $stream,
116-
'group' => $group,
117-
'consumer' => $consumer,
107+
$configuration = [
108+
'stream' => $redisOptions['stream'] ?? null,
109+
'group' => $redisOptions['group'] ?? null,
110+
'consumer' => $redisOptions['consumer'] ?? null,
118111
'auto_setup' => $autoSetup,
119112
'stream_max_entries' => $maxEntries,
120113
'dbindex' => $dbIndex,
121-
], $connectionCredentials, $redisOptions, $redis);
114+
];
115+
116+
if (isset($parsedUrl['host'])) {
117+
$connectionCredentials = [
118+
'host' => $parsedUrl['host'] ?? '127.0.0.1',
119+
'port' => $parsedUrl['port'] ?? 6379,
120+
'auth' => $parsedUrl['pass'] ?? $parsedUrl['user'] ?? null,
121+
];
122+
123+
$pathParts = explode('/', $parsedUrl['path'] ?? '');
124+
125+
$configuration['stream'] = $pathParts[1] ?? $configuration['stream'];
126+
$configuration['group'] = $pathParts[2] ?? $configuration['group'];
127+
$configuration['consumer'] = $pathParts[3] ?? $configuration['consumer'];
128+
} else {
129+
$connectionCredentials = [
130+
'host' => $parsedUrl['path'],
131+
'port' => 0,
132+
];
133+
}
134+
135+
return new self($configuration, $connectionCredentials, $redisOptions, $redis);
122136
}
123137

124138
public function get(): ?array

0 commit comments

Comments
 (0)
0