Skip to content

Commit 1abbad3

Browse files
author
Iskandar Najmuddin
committed
Improve comments and readability
1 parent ad33efb commit 1abbad3

File tree

5 files changed

+53
-13
lines changed

5 files changed

+53
-13
lines changed

demo/check_status.php

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
date_default_timezone_set('GMT');
99
Resque::setBackend('127.0.0.1:6379');
10+
// You can also use a DSN-style format:
11+
//Resque::setBackend('redis://user:[email protected]:6379');
12+
//Resque::setBackend('redis://user:[email protected]:3432/2');
1013

1114
$status = new Resque_Job_Status($argv[1]);
1215
if(!$status->isTracking()) {

demo/queue.php

100644100755
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
date_default_timezone_set('GMT');
88
Resque::setBackend('127.0.0.1:6379');
99

10+
// You can also use a DSN-style format:
11+
//Resque::setBackend('redis://user:[email protected]:6379');
12+
//Resque::setBackend('redis://user:[email protected]:3432/2');
13+
1014
$args = array(
1115
'time' => time(),
1216
'array' => array(

lib/Resque.php

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Resque
3232
* Given a host/port combination separated by a colon, set it as
3333
* the redis server that Resque will talk to.
3434
*
35-
* @param mixed $server Host/port combination separated by a colon, or
35+
* @param mixed $server Host/port combination separated by a colon, DSN-formatted URI, or
3636
* a nested array of servers with host/port pairs.
3737
* @param int $database
3838
*/

lib/Resque/Redis.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function __construct($server, $database = null)
122122
} else {
123123

124124
list($host, $port, $dsnDatabase, $user, $password, $options) = $this->parseDsn($server);
125-
// $user is are unused here
125+
// $user is not used, only $password
126126

127127
// Look for known Credis_Client options
128128
$timeout = isset($options['timeout']) ? intval($options['timeout']) : null;
@@ -133,9 +133,11 @@ public function __construct($server, $database = null)
133133
$this->driver->auth($password);
134134
}
135135

136-
// If the `$database` constructor argument is not set, use the value from the DSN.
137-
if (is_null($database)) {
136+
// If we have found a database in our DSN, use it instead of the `$database`
137+
// value passed into the constructor
138+
if ($dsnDatabase !== false) {
138139
$database = $dsnDatabase;
140+
$this->database = $database;
139141
}
140142
}
141143

@@ -145,35 +147,52 @@ public function __construct($server, $database = null)
145147
}
146148

147149
/**
148-
* Parse a DSN string
149-
* @param string $dsn
150+
* Parse a DSN string, which can have one of the following formats:
151+
*
152+
* - host:port
153+
* - redis://user:pass@host:port/db?option1=val1&option2=val2
154+
* - tcp://user:pass@host:port/db?option1=val1&option2=val2
155+
*
156+
* Note: the 'user' part of the DSN is not used.
157+
*
158+
* @param string $dsn A DSN string
150159
* @return array [host, port, db, user, pass, options]
151160
*/
152161
public function parseDsn($dsn)
153162
{
154-
$validSchemes = array('redis', 'tcp');
155163
if ($dsn == '') {
156164
// Use a sensible default for an empty DNS string
157165
$dsn = 'redis://' . self::DEFAULT_HOST;
158166
}
159167
$parts = parse_url($dsn);
168+
169+
// Check the URI scheme
170+
$validSchemes = array('redis', 'tcp');
160171
if (isset($parts['scheme']) && ! in_array($parts['scheme'], $validSchemes)) {
161172
throw new \InvalidArgumentException("Invalid DSN. Supported schemes are " . implode(', ', $validSchemes));
162173
}
163174

164-
// Allow simple 'hostname' format, which parse_url treats as a path, not host.
165-
if ( ! isset($parts['host'])) {
166-
$parts = array('host' => $parts['path']);
175+
// Allow simple 'hostname' format, which `parse_url` treats as a path, not host.
176+
if ( ! isset($parts['host']) && isset($parts['path'])) {
177+
$parts['host'] = $parts['path'];
178+
unset($parts['path']);
167179
}
168180

181+
// Extract the port number as an integer
169182
$port = isset($parts['port']) ? intval($parts['port']) : self::DEFAULT_PORT;
170183

171-
$database = self::DEFAULT_DATABASE;
184+
// Get the database from the 'path' part of the URI
185+
$database = false;
172186
if (isset($parts['path'])) {
173187
// Strip non-digit chars from path
174188
$database = intval(preg_replace('/[^0-9]/', '', $parts['path']));
175189
}
176190

191+
// Extract any 'user' and 'pass' values
192+
$user = isset($parts['user']) ? $parts['user'] : false;
193+
$pass = isset($parts['pass']) ? $parts['pass'] : false;
194+
195+
// Convert the query string into an associative array
177196
$options = array();
178197
if (isset($parts['query'])) {
179198
// Parse the query string into an array
@@ -184,8 +203,8 @@ public function parseDsn($dsn)
184203
$parts['host'],
185204
$port,
186205
$database,
187-
isset($parts['user']) ? $parts['user'] : false,
188-
isset($parts['pass']) ? $parts['pass'] : false,
206+
$user,
207+
$pass,
189208
$options,
190209
);
191210
}

test/Resque/Tests/DsnTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,27 @@ public function validDsnStringProvider()
6060
false, false,
6161
array(),
6262
)),
63+
array('redis://foobar:1234/15', array(
64+
'foobar',
65+
1234,
66+
15,
67+
false, false,
68+
array(),
69+
)),
6370
array('redis://user@foobar:1234', array(
6471
'foobar',
6572
1234,
6673
Resque_Redis::DEFAULT_DATABASE,
6774
'user', false,
6875
array(),
6976
)),
77+
array('redis://user@foobar:1234/15', array(
78+
'foobar',
79+
1234,
80+
15,
81+
'user', false,
82+
array(),
83+
)),
7084
array('redis://user:pass@foobar:1234', array(
7185
'foobar',
7286
1234,

0 commit comments

Comments
 (0)