RedisProfilerStorage wrong db-number/index-number selected

This commit is contained in:
René Kerner 2013-02-22 23:27:38 +01:00 committed by Fabien Potencier
parent 52bd4ffa56
commit c82c754b8e
2 changed files with 21 additions and 2 deletions

View File

@ -193,12 +193,13 @@ class RedisProfilerStorage implements ProfilerStorageInterface
protected function getRedis()
{
if (null === $this->redis) {
if (!preg_match('#^redis://(?(?=\[.*\])\[(.*)\]|(.*)):(.*)$#', $this->dsn, $matches)) {
throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Redis with an invalid dsn "%s". The expected format is "redis://[host]:port".', $this->dsn));
if (!preg_match('#^redis://(?(?=\[.*\])\[(.*)\]|(.*)):(\d+)(/(\d+))?$#', $this->dsn, $matches)) {
throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Redis with an invalid dsn "%s". The expected format is "redis://[host]:port[/db-number]".', $this->dsn));
}
$host = $matches[1] ?: $matches[2];
$port = $matches[3];
$dbnum = !empty($matches[5]) ? intval($matches[5]) : -1;
if (!extension_loaded('redis')) {
throw new \RuntimeException('RedisProfilerStorage requires that the redis extension is loaded.');
@ -206,6 +207,11 @@ class RedisProfilerStorage implements ProfilerStorageInterface
$redis = new Redis;
$redis->connect($host, $port);
// if a valid dbnumber is given select the redis index
if (-1 < $dbnum) {
$redis->select($dbnum);
}
$redis->setOption(self::REDIS_OPT_PREFIX, self::TOKEN_PREFIX);

View File

@ -238,4 +238,17 @@ class RedisMock
return true;
}
public function select($dbnum)
{
if (!$this->connected) {
return false;
}
if (0 > $dbnum) {
return false;
}
return true;
}
}