mirror of
https://github.com/predis/predis.git
synced 2026-08-22 13:32:20 +00:00
Map URI schemes to connection classes.
This commit is contained in:
+37
-6
@@ -113,7 +113,7 @@ class Client {
|
||||
|
||||
private function createConnection($parameters) {
|
||||
$params = new ConnectionParameters($parameters);
|
||||
$connection = $this->createConnectionInstance($params);
|
||||
$connection = Connection::create($params, $this->_responseReader);
|
||||
|
||||
if ($params->password !== null) {
|
||||
$connection->pushInitCommand($this->createCommand(
|
||||
@@ -129,11 +129,6 @@ class Client {
|
||||
return $connection;
|
||||
}
|
||||
|
||||
private function createConnectionInstance(ConnectionParameters $parameters) {
|
||||
// TODO: check scheme and return the appropriate IConnectionSingle instance
|
||||
return new TcpConnection($parameters, $this->_responseReader);
|
||||
}
|
||||
|
||||
private function setConnection(IConnection $connection) {
|
||||
$this->_connection = $connection;
|
||||
}
|
||||
@@ -1102,6 +1097,42 @@ interface IConnectionCluster extends IConnection {
|
||||
public function getConnectionById($connectionId);
|
||||
}
|
||||
|
||||
class Connection {
|
||||
private static $_registeredSchemes;
|
||||
|
||||
private static function ensureInitialized() {
|
||||
if (!isset(self::$_registeredSchemes)) {
|
||||
self::$_registeredSchemes = self::getDefaultSchemes();
|
||||
}
|
||||
}
|
||||
|
||||
private static function getDefaultSchemes() {
|
||||
return array(
|
||||
'tcp' => '\Predis\TcpConnection',
|
||||
);
|
||||
}
|
||||
|
||||
public static function registerScheme($scheme, $connectionClass) {
|
||||
self::ensureInitialized();
|
||||
$connectionReflection = new \ReflectionClass($connectionClass);
|
||||
if (!$connectionReflection->isSubclassOf('\Predis\IConnectionSingle')) {
|
||||
throw new ClientException(
|
||||
"Cannot register '$connectionClass' as it is not a valid connection class"
|
||||
);
|
||||
}
|
||||
self::$_registeredSchemes[$scheme] = $connectionClass;
|
||||
}
|
||||
|
||||
public static function create(ConnectionParameters $parameters, ResponseReader $reader) {
|
||||
self::ensureInitialized();
|
||||
if (!isset(self::$_registeredSchemes[$parameters->scheme])) {
|
||||
throw new ClientException("Unknown connection scheme: {$parameters->scheme}");
|
||||
}
|
||||
$connection = self::$_registeredSchemes[$parameters->scheme];
|
||||
return new $connection($parameters, $reader);
|
||||
}
|
||||
}
|
||||
|
||||
class TcpConnection implements IConnectionSingle {
|
||||
private $_params, $_socket, $_initCmds, $_reader;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user