From c82c754b8e094796993e69718275e574444c0fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kerner?= Date: Fri, 22 Feb 2013 23:27:38 +0100 Subject: [PATCH] RedisProfilerStorage wrong db-number/index-number selected --- .../HttpKernel/Profiler/RedisProfilerStorage.php | 10 ++++++++-- .../HttpKernel/Tests/Profiler/Mock/RedisMock.php | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Profiler/RedisProfilerStorage.php b/src/Symfony/Component/HttpKernel/Profiler/RedisProfilerStorage.php index e981d75f61..d601653bb8 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/RedisProfilerStorage.php +++ b/src/Symfony/Component/HttpKernel/Profiler/RedisProfilerStorage.php @@ -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); diff --git a/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/RedisMock.php b/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/RedisMock.php index 4b8cf07a66..122479b922 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/RedisMock.php +++ b/src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/RedisMock.php @@ -238,4 +238,17 @@ class RedisMock return true; } + + public function select($dbnum) + { + if (!$this->connected) { + return false; + } + + if (0 > $dbnum) { + return false; + } + + return true; + } }