Improve comments and readability

This commit is contained in:
Iskandar Najmuddin 2014-05-05 14:47:43 +00:00
parent ad33efbc67
commit 1abbad3f5e
5 changed files with 53 additions and 13 deletions

3
demo/check_status.php Normal file → Executable file
View File

@ -7,6 +7,9 @@ require __DIR__ . '/init.php';
date_default_timezone_set('GMT'); date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379'); Resque::setBackend('127.0.0.1:6379');
// You can also use a DSN-style format:
//Resque::setBackend('redis://user:pass@127.0.0.1:6379');
//Resque::setBackend('redis://user:pass@a.host.name:3432/2');
$status = new Resque_Job_Status($argv[1]); $status = new Resque_Job_Status($argv[1]);
if(!$status->isTracking()) { if(!$status->isTracking()) {

4
demo/queue.php Normal file → Executable file
View File

@ -7,6 +7,10 @@ require __DIR__ . '/init.php';
date_default_timezone_set('GMT'); date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379'); Resque::setBackend('127.0.0.1:6379');
// You can also use a DSN-style format:
//Resque::setBackend('redis://user:pass@127.0.0.1:6379');
//Resque::setBackend('redis://user:pass@a.host.name:3432/2');
$args = array( $args = array(
'time' => time(), 'time' => time(),
'array' => array( 'array' => array(

2
lib/Resque.php Normal file → Executable file
View File

@ -32,7 +32,7 @@ class Resque
* Given a host/port combination separated by a colon, set it as * Given a host/port combination separated by a colon, set it as
* the redis server that Resque will talk to. * the redis server that Resque will talk to.
* *
* @param mixed $server Host/port combination separated by a colon, or * @param mixed $server Host/port combination separated by a colon, DSN-formatted URI, or
* a nested array of servers with host/port pairs. * a nested array of servers with host/port pairs.
* @param int $database * @param int $database
*/ */

View File

@ -122,7 +122,7 @@ class Resque_Redis
} else { } else {
list($host, $port, $dsnDatabase, $user, $password, $options) = $this->parseDsn($server); list($host, $port, $dsnDatabase, $user, $password, $options) = $this->parseDsn($server);
// $user is are unused here // $user is not used, only $password
// Look for known Credis_Client options // Look for known Credis_Client options
$timeout = isset($options['timeout']) ? intval($options['timeout']) : null; $timeout = isset($options['timeout']) ? intval($options['timeout']) : null;
@ -133,9 +133,11 @@ class Resque_Redis
$this->driver->auth($password); $this->driver->auth($password);
} }
// If the `$database` constructor argument is not set, use the value from the DSN. // If we have found a database in our DSN, use it instead of the `$database`
if (is_null($database)) { // value passed into the constructor
if ($dsnDatabase !== false) {
$database = $dsnDatabase; $database = $dsnDatabase;
$this->database = $database;
} }
} }
@ -145,35 +147,52 @@ class Resque_Redis
} }
/** /**
* Parse a DSN string * Parse a DSN string, which can have one of the following formats:
* @param string $dsn *
* - host:port
* - redis://user:pass@host:port/db?option1=val1&option2=val2
* - tcp://user:pass@host:port/db?option1=val1&option2=val2
*
* 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 [host, port, db, user, pass, options]
*/ */
public function parseDsn($dsn) public function parseDsn($dsn)
{ {
$validSchemes = array('redis', 'tcp');
if ($dsn == '') { if ($dsn == '') {
// Use a sensible default for an empty DNS string // Use a sensible default for an empty DNS string
$dsn = 'redis://' . self::DEFAULT_HOST; $dsn = 'redis://' . self::DEFAULT_HOST;
} }
$parts = parse_url($dsn); $parts = parse_url($dsn);
// Check the URI scheme
$validSchemes = array('redis', 'tcp');
if (isset($parts['scheme']) && ! in_array($parts['scheme'], $validSchemes)) { if (isset($parts['scheme']) && ! in_array($parts['scheme'], $validSchemes)) {
throw new \InvalidArgumentException("Invalid DSN. Supported schemes are " . implode(', ', $validSchemes)); throw new \InvalidArgumentException("Invalid DSN. Supported schemes are " . implode(', ', $validSchemes));
} }
// Allow simple 'hostname' format, which parse_url treats as a path, not host. // Allow simple 'hostname' format, which `parse_url` treats as a path, not host.
if ( ! isset($parts['host'])) { if ( ! isset($parts['host']) && isset($parts['path'])) {
$parts = array('host' => $parts['path']); $parts['host'] = $parts['path'];
unset($parts['path']);
} }
// Extract the port number as an integer
$port = isset($parts['port']) ? intval($parts['port']) : self::DEFAULT_PORT; $port = isset($parts['port']) ? intval($parts['port']) : self::DEFAULT_PORT;
$database = self::DEFAULT_DATABASE; // Get the database from the 'path' part of the URI
$database = false;
if (isset($parts['path'])) { if (isset($parts['path'])) {
// Strip non-digit chars from path // Strip non-digit chars from path
$database = intval(preg_replace('/[^0-9]/', '', $parts['path'])); $database = intval(preg_replace('/[^0-9]/', '', $parts['path']));
} }
// Extract any 'user' and 'pass' values
$user = isset($parts['user']) ? $parts['user'] : false;
$pass = isset($parts['pass']) ? $parts['pass'] : false;
// Convert the query string into an associative array
$options = array(); $options = array();
if (isset($parts['query'])) { if (isset($parts['query'])) {
// Parse the query string into an array // Parse the query string into an array
@ -184,8 +203,8 @@ class Resque_Redis
$parts['host'], $parts['host'],
$port, $port,
$database, $database,
isset($parts['user']) ? $parts['user'] : false, $user,
isset($parts['pass']) ? $parts['pass'] : false, $pass,
$options, $options,
); );
} }

View File

@ -60,6 +60,13 @@ class Resque_Tests_DsnTest extends Resque_Tests_TestCase
false, false, false, false,
array(), array(),
)), )),
array('redis://foobar:1234/15', array(
'foobar',
1234,
15,
false, false,
array(),
)),
array('redis://user@foobar:1234', array( array('redis://user@foobar:1234', array(
'foobar', 'foobar',
1234, 1234,
@ -67,6 +74,13 @@ class Resque_Tests_DsnTest extends Resque_Tests_TestCase
'user', false, 'user', false,
array(), array(),
)), )),
array('redis://user@foobar:1234/15', array(
'foobar',
1234,
15,
'user', false,
array(),
)),
array('redis://user:pass@foobar:1234', array( array('redis://user:pass@foobar:1234', array(
'foobar', 'foobar',
1234, 1234,