[PROFILER][REDIS] Support database, auth on redis connection

This commit is contained in:
Stéphane PY 2013-01-10 17:46:24 +01:00
parent 7ab8e48373
commit d027f45833

View File

@ -17,6 +17,7 @@ use Redis;
* RedisProfilerStorage stores profiling information in Redis. * RedisProfilerStorage stores profiling information in Redis.
* *
* @author Andrej Hudec <pulzarraider@gmail.com> * @author Andrej Hudec <pulzarraider@gmail.com>
* @author Stephane PY <py.stephane1@gmail.com>
*/ */
class RedisProfilerStorage implements ProfilerStorageInterface class RedisProfilerStorage implements ProfilerStorageInterface
{ {
@ -213,19 +214,26 @@ class RedisProfilerStorage implements ProfilerStorageInterface
protected function getRedis() protected function getRedis()
{ {
if (null === $this->redis) { if (null === $this->redis) {
if (!preg_match('#^redis://(?(?=\[.*\])\[(.*)\]|(.*)):(.*)$#', $this->dsn, $matches)) { $data = parse_url($this->dsn);
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));
}
$host = $matches[1] ?: $matches[2]; if (false === $data || !isset($data['scheme']) || $data['scheme'] !== 'redis' || !isset($data['host']) || !isset($data['port'])) {
$port = $matches[3]; throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Redis with an invalid dsn "%s". The minimal expected format is "redis://[host]:port".', $this->dsn));
}
if (!extension_loaded('redis')) { if (!extension_loaded('redis')) {
throw new \RuntimeException('RedisProfilerStorage requires that the redis extension is loaded.'); throw new \RuntimeException('RedisProfilerStorage requires that the redis extension is loaded.');
} }
$redis = new Redis; $redis = new Redis;
$redis->connect($host, $port); $redis->connect($data['host'], $data['port']);
if (isset($data['path'])) {
$redis->select(substr($data['path'], 1));
}
if (isset($data['pass'])) {
$redis->auth($data['pass']);
}
$redis->setOption(self::REDIS_OPT_PREFIX, self::TOKEN_PREFIX); $redis->setOption(self::REDIS_OPT_PREFIX, self::TOKEN_PREFIX);