From 1abbad3f5ee83ef05b19199bdbb29f7176c5eb7d Mon Sep 17 00:00:00 2001 From: Iskandar Najmuddin Date: Mon, 5 May 2014 14:47:43 +0000 Subject: [PATCH] Improve comments and readability --- demo/check_status.php | 3 +++ demo/queue.php | 4 ++++ lib/Resque.php | 2 +- lib/Resque/Redis.php | 43 +++++++++++++++++++++++++---------- test/Resque/Tests/DsnTest.php | 14 ++++++++++++ 5 files changed, 53 insertions(+), 13 deletions(-) mode change 100644 => 100755 demo/check_status.php mode change 100644 => 100755 demo/queue.php mode change 100644 => 100755 lib/Resque.php diff --git a/demo/check_status.php b/demo/check_status.php old mode 100644 new mode 100755 index 645bf6d..871daba --- a/demo/check_status.php +++ b/demo/check_status.php @@ -7,6 +7,9 @@ require __DIR__ . '/init.php'; date_default_timezone_set('GMT'); 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]); if(!$status->isTracking()) { diff --git a/demo/queue.php b/demo/queue.php old mode 100644 new mode 100755 index 52f2f0b..1eca124 --- a/demo/queue.php +++ b/demo/queue.php @@ -7,6 +7,10 @@ require __DIR__ . '/init.php'; date_default_timezone_set('GMT'); 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( 'time' => time(), 'array' => array( diff --git a/lib/Resque.php b/lib/Resque.php old mode 100644 new mode 100755 index 2b47084..13c841e --- a/lib/Resque.php +++ b/lib/Resque.php @@ -32,7 +32,7 @@ class Resque * Given a host/port combination separated by a colon, set it as * 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. * @param int $database */ diff --git a/lib/Resque/Redis.php b/lib/Resque/Redis.php index e44a962..41fec07 100755 --- a/lib/Resque/Redis.php +++ b/lib/Resque/Redis.php @@ -122,7 +122,7 @@ class Resque_Redis } else { 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 $timeout = isset($options['timeout']) ? intval($options['timeout']) : null; @@ -133,9 +133,11 @@ class Resque_Redis $this->driver->auth($password); } - // If the `$database` constructor argument is not set, use the value from the DSN. - if (is_null($database)) { + // If we have found a database in our DSN, use it instead of the `$database` + // value passed into the constructor + if ($dsnDatabase !== false) { $database = $dsnDatabase; + $this->database = $database; } } @@ -145,35 +147,52 @@ class Resque_Redis } /** - * Parse a DSN string - * @param string $dsn + * Parse a DSN string, which can have one of the following formats: + * + * - 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] */ public function parseDsn($dsn) { - $validSchemes = array('redis', 'tcp'); if ($dsn == '') { // Use a sensible default for an empty DNS string $dsn = 'redis://' . self::DEFAULT_HOST; } $parts = parse_url($dsn); + + // Check the URI scheme + $validSchemes = array('redis', 'tcp'); if (isset($parts['scheme']) && ! in_array($parts['scheme'], $validSchemes)) { throw new \InvalidArgumentException("Invalid DSN. Supported schemes are " . implode(', ', $validSchemes)); } - // Allow simple 'hostname' format, which parse_url treats as a path, not host. - if ( ! isset($parts['host'])) { - $parts = array('host' => $parts['path']); + // Allow simple 'hostname' format, which `parse_url` treats as a path, not host. + if ( ! isset($parts['host']) && isset($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; - $database = self::DEFAULT_DATABASE; + // Get the database from the 'path' part of the URI + $database = false; if (isset($parts['path'])) { // Strip non-digit chars from 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(); if (isset($parts['query'])) { // Parse the query string into an array @@ -184,8 +203,8 @@ class Resque_Redis $parts['host'], $port, $database, - isset($parts['user']) ? $parts['user'] : false, - isset($parts['pass']) ? $parts['pass'] : false, + $user, + $pass, $options, ); } diff --git a/test/Resque/Tests/DsnTest.php b/test/Resque/Tests/DsnTest.php index c9cd471..2fc6654 100755 --- a/test/Resque/Tests/DsnTest.php +++ b/test/Resque/Tests/DsnTest.php @@ -60,6 +60,13 @@ class Resque_Tests_DsnTest extends Resque_Tests_TestCase false, false, array(), )), + array('redis://foobar:1234/15', array( + 'foobar', + 1234, + 15, + false, false, + array(), + )), array('redis://user@foobar:1234', array( 'foobar', 1234, @@ -67,6 +74,13 @@ class Resque_Tests_DsnTest extends Resque_Tests_TestCase 'user', false, array(), )), + array('redis://user@foobar:1234/15', array( + 'foobar', + 1234, + 15, + 'user', false, + array(), + )), array('redis://user:pass@foobar:1234', array( 'foobar', 1234,