Make parseDsn() method static, remove unused member vars

This commit is contained in:
Iskandar Najmuddin 2014-05-05 15:08:00 +00:00
parent 62ed620083
commit 506b769b90
3 changed files with 33 additions and 46 deletions

View File

@ -54,12 +54,7 @@ class Resque
return self::$redis; return self::$redis;
} }
$server = self::$redisServer; self::$redis = new Resque_Redis(self::$redisServer, self::$redisDatabase);
if (empty($server)) {
$server = 'localhost:6379';
}
self::$redis = new Resque_Redis($server, self::$redisDatabase);
return self::$redis; return self::$redis;
} }

View File

@ -29,9 +29,6 @@ class Resque_Redis
*/ */
const DEFAULT_DATABASE = 0; const DEFAULT_DATABASE = 0;
private $server;
private $database;
/** /**
* @var array List of all commands in Redis that supply a key as their * @var array List of all commands in Redis that supply a key as their
* first argument. Used to prefix keys with the Resque namespace. * first argument. Used to prefix keys with the Resque namespace.
@ -114,16 +111,12 @@ class Resque_Redis
*/ */
public function __construct($server, $database = null) public function __construct($server, $database = null)
{ {
$this->server = $server; if (is_array($server)) {
$this->database = $database;
if (is_array($this->server)) {
$this->driver = new Credis_Cluster($server); $this->driver = new Credis_Cluster($server);
} }
else { 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 // $user is not used, only $password
// Look for known Credis_Client options // Look for known Credis_Client options
@ -139,11 +132,10 @@ class Resque_Redis
// value passed into the constructor. // value passed into the constructor.
if ($dsnDatabase !== false) { if ($dsnDatabase !== false) {
$database = $dsnDatabase; $database = $dsnDatabase;
$this->database = $database;
} }
} }
if ($this->database !== null) { if ($database !== null) {
$this->driver->select($database); $this->driver->select($database);
} }
} }
@ -158,9 +150,10 @@ class Resque_Redis
* Note: the 'user' part of the DSN is not used. * Note: the 'user' part of the DSN is not used.
* *
* @param string $dsn A DSN string * @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 == '') { if ($dsn == '') {
// Use a sensible default for an empty DNS string // Use a sensible default for an empty DNS string
@ -219,20 +212,22 @@ class Resque_Redis
* @param array $args Array of supplied arguments to the method. * @param array $args Array of supplied arguments to the method.
* @return mixed Return value from Resident::call() based on the command. * @return mixed Return value from Resident::call() based on the command.
*/ */
public function __call($name, $args) { public function __call($name, $args)
if(in_array($name, $this->keyCommands)) { {
if(is_array($args[0])) { if (in_array($name, $this->keyCommands)) {
foreach($args[0] AS $i => $v) { if (is_array($args[0])) {
foreach ($args[0] AS $i => $v) {
$args[0][$i] = self::$defaultNamespace . $v; $args[0][$i] = self::$defaultNamespace . $v;
} }
} else { }
else {
$args[0] = self::$defaultNamespace . $args[0]; $args[0] = self::$defaultNamespace . $args[0];
} }
} }
try { try {
return $this->driver->__call($name, $args); return $this->driver->__call($name, $args);
} }
catch(CredisException $e) { catch (CredisException $e) {
return false; return false;
} }
} }
@ -252,4 +247,3 @@ class Resque_Redis
return $string; return $string;
} }
} }
?>

View File

@ -159,8 +159,7 @@ class Resque_Tests_DsnTest extends Resque_Tests_TestCase
*/ */
public function testParsingValidDsnString($dsn, $expected) public function testParsingValidDsnString($dsn, $expected)
{ {
$resqueRedis = new Resque_Redis('localhost'); $result = Resque_Redis::parseDsn($dsn);
$result = $resqueRedis->parseDsn($dsn);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
@ -170,9 +169,8 @@ class Resque_Tests_DsnTest extends Resque_Tests_TestCase
*/ */
public function testParsingBogusDsnStringThrowsException($dsn) public function testParsingBogusDsnStringThrowsException($dsn)
{ {
$resqueRedis = new Resque_Redis('localhost');
// The next line should throw an InvalidArgumentException // The next line should throw an InvalidArgumentException
$result = $resqueRedis->parseDsn($dsn); $result = Resque_Redis::parseDsn($dsn);
} }
} }