8000 [HttpFoundation] Allow RedisCluster class for RedisSessionHandler · symfony/symfony@d2ecea0 · GitHub
[go: up one dir, main page]

Skip to content

Commit d2ecea0

Browse files
michaelperrinnicolas-grekas
authored andcommitted
[HttpFoundation] Allow RedisCluster class for RedisSessionHandler
1 parent 3ac90c1 commit d2ecea0

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,20 @@ class RedisSessionHandler extends AbstractSessionHandler
3333
* List of available options:
3434
* * prefix: The prefix to use for the keys in order to avoid collision on the Redis server.
3535
*
36-
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redis
37-
* @param array $options An associative array of options
36+
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|RedisProxy $redis
37+
* @param array $options An associative array of options
3838
*
3939
* @throws \InvalidArgumentException When unsupported client or options are passed
4040
*/
4141
public function __construct($redis, array $options = array())
4242
{
43-
if (!$redis instanceof \Redis && !$redis instanceof \RedisArray && !$redis instanceof \Predis\Client && !$redis instanceof RedisProxy) {
43+
if (
44+
!$redis instanceof \Redis &&
45+
!$redis instanceof \RedisArray &&
46+
!$redis instanceof \RedisCluster &&
47+
!$redis instanceof \Predis\Client &&
48+
!$redis instanceof RedisProxy
49+
) {
4450
throw new \InvalidArgumentException(sprintf('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given', __METHOD__, \is_object($redis) ? \get_class($redis) : \gettype($redis)));
4551
}
4652

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase
3232
*/
3333
protected $redisClient;
3434

35-
/**
36-
* @var \Redis
37-
*/
38-
protected $validator;
39-
4035
/**
4136
* @return \Redis|\RedisArray|\RedisCluster|\Predis\Client
4237
*/
@@ -52,9 +47,6 @@ protected function setUp()
5247

5348
$host = getenv('REDIS_HOST') ?: 'localhost';
5449

55-
$this->validator = new \Redis();
56-
$this->validator->connect($host);
57-
5850
$this->redisClient = $this->createRedisClient($host);
5951
$this->storage = new RedisSessionHandler(
6052
$this->redisClient,
@@ -154,24 +146,24 @@ public function getOptionFixtures(): array
154146
protected function setFixture($key, $value, $ttl = null)
155147
{
156148
if (null !== $ttl) {
157-
$this->validator->setex($key, $ttl, $value);
149+
$this->redisClient->setex($key, $ttl, $value);
158150
} else {
159-
$this->validator->set($key, $value);
151+
$this->redisClient->set($key, $value);
160152
}
161153
}
162154

163155
protected function getFixture($key)
164156
{
165-
return $this->validator->get($key);
157+
return $this->redisClient->get($key);
166158
}
167159

168160
protected function hasFixture($key): bool
169161
{
170-
return $this->validator->exists($key);
162+
return $this->redisClient->exists($key);
171163
}
172164

173165
protected function fixtureTtl($key): int
174166
{
175-
return $this->validator->ttl($key);
167+
return $this->redisClient->ttl($key);
176168
}
177169
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class PredisClusterSessionHandlerTest extends AbstractRedisSessionHandlerTestCas
1717
{
1818
protected function createRedisClient(string $host): Client
1919
{
20-
return new Client(array(array('host' => $host)));
20+
return new Client(array(array('host' => $host)));
2121
}
2222
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
13+
14+
class RedisClusterSessionHandlerTest extends AbstractRedisSessionHandlerTestCase
15+
{
16+
public static function setupBeforeClass()
17+
{
18+
if (!class_exists('RedisCluster')) {
19+
self::markTestSkipped('The RedisCluster class is required.');
20+
}
21+
22+
if (!$hosts = getenv('REDIS_CLUSTER_HOSTS')) {
23+
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
24+
}
25+
}
26+
27+
protected function createRedisClient(string $host): \RedisCluster
28+
{
29+
return new \RedisCluster(null, explode(' ', getenv('REDIS_CLUSTER_HOSTS')));
30+
}
31+
}

0 commit comments

Comments
 (0)
0