From 506b769b905ede790f77e74b15e877cc3c03b289 Mon Sep 17 00:00:00 2001 From: Iskandar Najmuddin Date: Mon, 5 May 2014 15:08:00 +0000 Subject: [PATCH] Make parseDsn() method static, remove unused member vars --- lib/Resque.php | 7 +--- lib/Resque/Redis.php | 66 ++++++++++++++++------------------- test/Resque/Tests/DsnTest.php | 6 ++-- 3 files changed, 33 insertions(+), 46 deletions(-) diff --git a/lib/Resque.php b/lib/Resque.php index 13c841e..0f4b94e 100755 --- a/lib/Resque.php +++ b/lib/Resque.php @@ -54,12 +54,7 @@ class Resque return self::$redis; } - $server = self::$redisServer; - if (empty($server)) { - $server = 'localhost:6379'; - } - - self::$redis = new Resque_Redis($server, self::$redisDatabase); + self::$redis = new Resque_Redis(self::$redisServer, self::$redisDatabase); return self::$redis; } diff --git a/lib/Resque/Redis.php b/lib/Resque/Redis.php index bddd013..a946b14 100755 --- a/lib/Resque/Redis.php +++ b/lib/Resque/Redis.php @@ -29,9 +29,6 @@ class Resque_Redis */ const DEFAULT_DATABASE = 0; - private $server; - private $database; - /** * @var array List of all commands in Redis that supply a key as their * first argument. Used to prefix keys with the Resque namespace. @@ -114,16 +111,12 @@ class Resque_Redis */ public function __construct($server, $database = null) { - $this->server = $server; - $this->database = $database; - - if (is_array($this->server)) { + if (is_array($server)) { $this->driver = new Credis_Cluster($server); - } else { - list($host, $port, $dsnDatabase, $user, $password, $options) = $this->parseDsn($server); + list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server); // $user is not used, only $password // Look for known Credis_Client options @@ -139,11 +132,10 @@ class Resque_Redis // value passed into the constructor. if ($dsnDatabase !== false) { $database = $dsnDatabase; - $this->database = $database; } } - if ($this->database !== null) { + if ($database !== null) { $this->driver->select($database); } } @@ -158,9 +150,10 @@ class Resque_Redis * Note: the 'user' part of the DSN is not used. * * @param string $dsn A DSN string - * @return array [host, port, db, user, pass, options] + * @return array An array of DSN compotnents, with 'false' values for any unknown components. e.g. + * [host, port, db, user, pass, options] */ - public function parseDsn($dsn) + public static function parseDsn($dsn) { if ($dsn == '') { // Use a sensible default for an empty DNS string @@ -219,37 +212,38 @@ class Resque_Redis * @param array $args Array of supplied arguments to the method. * @return mixed Return value from Resident::call() based on the command. */ - public function __call($name, $args) { - if(in_array($name, $this->keyCommands)) { - if(is_array($args[0])) { - foreach($args[0] AS $i => $v) { - $args[0][$i] = self::$defaultNamespace . $v; - } - } else { - $args[0] = self::$defaultNamespace . $args[0]; - } + public function __call($name, $args) + { + if (in_array($name, $this->keyCommands)) { + if (is_array($args[0])) { + foreach ($args[0] AS $i => $v) { + $args[0][$i] = self::$defaultNamespace . $v; + } + } + else { + $args[0] = self::$defaultNamespace . $args[0]; + } } try { return $this->driver->__call($name, $args); } - catch(CredisException $e) { + catch (CredisException $e) { return false; } } - public static function getPrefix() - { - return self::$defaultNamespace; - } + public static function getPrefix() + { + return self::$defaultNamespace; + } - public static function removePrefix($string) - { - $prefix=self::getPrefix(); + public static function removePrefix($string) + { + $prefix=self::getPrefix(); - if (substr($string, 0, strlen($prefix)) == $prefix) { - $string = substr($string, strlen($prefix), strlen($string) ); - } - return $string; - } + if (substr($string, 0, strlen($prefix)) == $prefix) { + $string = substr($string, strlen($prefix), strlen($string) ); + } + return $string; + } } -?> \ No newline at end of file diff --git a/test/Resque/Tests/DsnTest.php b/test/Resque/Tests/DsnTest.php index 7e483a3..282a088 100755 --- a/test/Resque/Tests/DsnTest.php +++ b/test/Resque/Tests/DsnTest.php @@ -159,8 +159,7 @@ class Resque_Tests_DsnTest extends Resque_Tests_TestCase */ public function testParsingValidDsnString($dsn, $expected) { - $resqueRedis = new Resque_Redis('localhost'); - $result = $resqueRedis->parseDsn($dsn); + $result = Resque_Redis::parseDsn($dsn); $this->assertEquals($expected, $result); } @@ -170,9 +169,8 @@ class Resque_Tests_DsnTest extends Resque_Tests_TestCase */ public function testParsingBogusDsnStringThrowsException($dsn) { - $resqueRedis = new Resque_Redis('localhost'); // The next line should throw an InvalidArgumentException - $result = $resqueRedis->parseDsn($dsn); + $result = Resque_Redis::parseDsn($dsn); } }