From d18468b90db4dc17926f5c208a81c1de57293057 Mon Sep 17 00:00:00 2001 From: Benoit Galati Date: Tue, 5 Feb 2019 10:22:19 +0100 Subject: [PATCH] Fix #449 - No master server available for replication Connection alias was not set anymore by the parameters factory --- Factory/PredisParametersFactory.php | 9 ++++++++- Tests/Factory/PredisParametersFactoryTest.php | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Factory/PredisParametersFactory.php b/Factory/PredisParametersFactory.php index 38b5890d..a048b163 100644 --- a/Factory/PredisParametersFactory.php +++ b/Factory/PredisParametersFactory.php @@ -20,8 +20,9 @@ public static function create($options, $class, $dsn) throw new \InvalidArgumentException(sprintf('%s::%s requires $class argument to implement %s', __CLASS__, __METHOD__, '\Predis\Connection\ParametersInterface')); } + $defaultOptions = array('timeout' => null); // Allow to be consistent with old version of Predis where default timeout was 5 $dsnOptions = static::parseDsn(new RedisDsn($dsn)); - $dsnOptions = array_merge($options, $dsnOptions); + $dsnOptions = array_merge($defaultOptions, $options, $dsnOptions); if (isset($dsnOptions['persistent'], $dsnOptions['database']) && true === $dsnOptions['persistent']) { $dsnOptions['persistent'] = (int)$dsnOptions['database']; @@ -48,12 +49,18 @@ private static function parseDsn(RedisDsn $dsn) $options['path'] = $dsn->getDatabase(); } } + if (null !== $dsn->getDatabase()) { $options['database'] = $dsn->getDatabase(); } + $options['password'] = $dsn->getPassword(); $options['weight'] = $dsn->getWeight(); + if (null !== $dsn->getAlias()) { + $options['alias'] = $dsn->getAlias(); + } + return $options; } } diff --git a/Tests/Factory/PredisParametersFactoryTest.php b/Tests/Factory/PredisParametersFactoryTest.php index 1440f1de..24ccf21a 100644 --- a/Tests/Factory/PredisParametersFactoryTest.php +++ b/Tests/Factory/PredisParametersFactoryTest.php @@ -72,6 +72,21 @@ public function createDp() 'port' => 6380, 'password' => 'pw' ) + ), + array( + 'redis://localhost?alias=master', + 'Predis\Connection\Parameters', + array('replication' => true), + array( + 'scheme' => 'tcp', + 'host' => 'localhost', + 'port' => 6379, + 'replication' => true, + 'password' => null, + 'weight' => null, + 'alias' => 'master', + 'timeout' => null, + ) ) ); }