Skip to content

Commit 37c8fe3

Browse files
committed
[predis] Add support for prefix option
1 parent 3d9061f commit 37c8fe3

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

src/DependencyInjection/Configuration/RedisDsn.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class RedisDsn
5252

5353
protected ?string $role = null;
5454

55+
protected ?string $prefix = null;
56+
5557
public function __construct(string $dsn)
5658
{
5759
$this->dsn = $dsn;
@@ -119,6 +121,11 @@ public function getPersistentId(): string
119121
return md5($this->dsn);
120122
}
121123

124+
public function getPrefix(): ?string
125+
{
126+
return $this->prefix;
127+
}
128+
122129
public function isValid(): bool
123130
{
124131
if (strpos($this->dsn, 'redis://') !== 0 && strpos($this->dsn, 'rediss://') !== 0) {
@@ -202,6 +209,10 @@ protected function parseParameters(array $matches): string
202209
$this->role = $params['role'];
203210
}
204211

212+
if (!empty($params['prefix'])) {
213+
$this->prefix = $params['prefix'];
214+
}
215+
205216
return '';
206217
}
207218

src/Factory/PredisParametersFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
/** @internal */
1717
class PredisParametersFactory
1818
{
19-
/** @param array<string, mixed> $options */
19+
/**
20+
* @param class-string<ParametersInterface> $class
21+
* @param array<string, mixed> $options
22+
*/
2023
public static function create(array $options, string $class, string $dsn): ParametersInterface
2124
{
2225
if (!is_a($class, ParametersInterface::class, true)) {
@@ -41,6 +44,7 @@ private static function parseDsn(RedisDsn $dsn): array
4144
$options = [
4245
'password' => $dsn->getPassword(),
4346
'weight' => $dsn->getWeight(),
47+
'prefix' => $dsn->getPrefix(),
4448
];
4549

4650
if ($socket !== null) {

tests/Factory/PredisParametersFactoryTest.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414

1515
class PredisParametersFactoryTest extends TestCase
1616
{
17-
/** @return array<array{0: string, 1: class-string, 2: array<string, mixed>, 3: array<string, mixed>}> */
17+
/** @return array<array{0: string, 1: array<string, mixed>, 2: array<string, mixed>}> */
1818
public static function createDp(): array
1919
{
2020
return [
2121
[
2222
'redis://z:df577d779b4f724c8c29b5eff5bcc534b732722b9df308a661f1b79014175063d5@ec2-34-321-123-45.us-east-1.compute.amazonaws.com:3210',
23-
Parameters::class,
2423
[
2524
'test' => 123,
2625
'some' => 'string',
@@ -48,7 +47,6 @@ public static function createDp(): array
4847
],
4948
[
5049
'redis://pw@/var/run/redis/redis-1.sock/10',
51-
Parameters::class,
5250
[
5351
'test' => 124,
5452
'password' => 'toto',
@@ -72,7 +70,6 @@ public static function createDp(): array
7270
],
7371
[
7472
'rediss://pw@localhost:6380',
75-
Parameters::class,
7673
[],
7774
[
7875
'scheme' => 'tls',
@@ -83,7 +80,6 @@ public static function createDp(): array
8380
],
8481
[
8582
'redis://localhost?alias=master',
86-
Parameters::class,
8783
['replication' => 'predis'],
8884
[
8985
'scheme' => 'tcp',
@@ -98,7 +94,6 @@ public static function createDp(): array
9894
],
9995
[
10096
'redis://localhost?alias=connection_alias',
101-
Parameters::class,
10297
[
10398
'replication' => 'predis',
10499
'alias' => 'client_alias',
@@ -116,23 +111,35 @@ public static function createDp(): array
116111
],
117112
[
118113
'redis://localhost/0',
119-
Parameters::class,
120114
['persistent' => true],
121115
[
122116
'persistent' => true,
123117
'database' => 0,
124118
],
125119
],
126-
127120
[
128121
'redis://localhost',
129-
Parameters::class,
130122
['database' => 11, 'password' => 'pass'],
131123
[
132124
'database' => 11,
133125
'password' => 'pass',
134126
],
135127
],
128+
'everything in DSN' => [
129+
'rediss://pw@localhost:6380/0?prefix=foo&alias=connection_alias',
130+
[],
131+
$allOptions =
132+
[
133+
'scheme' => 'tls',
134+
'host' => 'localhost',
135+
'port' => 6380,
136+
'password' => 'pw',
137+
'database' => 0,
138+
'prefix' => 'foo',
139+
'alias' => 'connection_alias',
140+
],
141+
],
142+
'everything in options' => ['rediss://localhost:6380', $allOptions, $allOptions],
136143
];
137144
}
138145

@@ -142,11 +149,9 @@ public static function createDp(): array
142149
*
143150
* @dataProvider createDp
144151
*/
145-
public function testCreate(string $dsn, string $class, array $options, array $expectedParameters): void
152+
public function testCreate(string $dsn, array $options, array $expectedParameters): void
146153
{
147-
$parameters = PredisParametersFactory::create($options, $class, $dsn);
148-
149-
$this->assertInstanceOf($class, $parameters);
154+
$parameters = PredisParametersFactory::create($options, Parameters::class, $dsn);
150155

151156
foreach ($expectedParameters as $name => $value) {
152157
$this->assertSame($value, $parameters->{$name}, sprintf("Wrong '%s' value", $name));
@@ -157,6 +162,7 @@ public function testCreateException(): void
157162
{
158163
$this->expectException(InvalidArgumentException::class);
159164

165+
/** @psalm-suppress InvalidArgument */
160166
PredisParametersFactory::create([], stdClass::class, 'redis://localhost');
161167
}
162168
}

0 commit comments

Comments
 (0)