mirror of
https://github.com/idanoo/php-resque.git
synced 2024-11-22 00:11:53 +00:00
Improve comments and readability
This commit is contained in:
parent
ad33efbc67
commit
1abbad3f5e
3
demo/check_status.php
Normal file → Executable file
3
demo/check_status.php
Normal file → Executable file
@ -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()) {
|
||||
|
4
demo/queue.php
Normal file → Executable file
4
demo/queue.php
Normal file → Executable file
@ -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(
|
||||
|
2
lib/Resque.php
Normal file → Executable file
2
lib/Resque.php
Normal file → Executable file
@ -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
|
||||
*/
|
||||
|
@ -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,
|
||||
);
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user