mirror of
https://github.com/predis/predis.git
synced 2026-10-14 07:47:49 +00:00
Change how parameters are handled internally.
This commit is contained in:
@@ -12,8 +12,8 @@ class ComposableStreamConnection extends StreamConnection implements IConnection
|
||||
private $_protocol;
|
||||
|
||||
public function __construct(ConnectionParameters $parameters, IProtocolProcessor $protocol = null) {
|
||||
$this->setProtocol($protocol ?: new TextProtocol());
|
||||
parent::__construct($parameters);
|
||||
$this->_protocol = $protocol ?: new TextProtocol();
|
||||
}
|
||||
|
||||
public function setProtocol(IProtocolProcessor $protocol) {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Predis\Network;
|
||||
|
||||
use \InvalidArgumentException;
|
||||
use Predis\Utils;
|
||||
use Predis\ConnectionParameters;
|
||||
use Predis\ClientException;
|
||||
@@ -14,19 +15,43 @@ abstract class ConnectionBase implements IConnectionSingle {
|
||||
|
||||
public function __construct(ConnectionParameters $parameters) {
|
||||
$this->_initCmds = array();
|
||||
$this->_params = $parameters;
|
||||
$this->_params = $this->checkParameters($parameters);
|
||||
$this->initializeProtocol($parameters);
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
$this->disconnect();
|
||||
}
|
||||
|
||||
public function isConnected() {
|
||||
return isset($this->_resource);
|
||||
protected function checkParameters(ConnectionParameters $parameters) {
|
||||
switch ($parameters->scheme) {
|
||||
case 'unix':
|
||||
$pathToSocket = $parameters->path;
|
||||
if (!isset($pathToSocket)) {
|
||||
throw new InvalidArgumentException('Missing UNIX domain socket path');
|
||||
}
|
||||
if (!file_exists($pathToSocket)) {
|
||||
throw new InvalidArgumentException("Could not find $pathToSocket");
|
||||
}
|
||||
case 'tcp':
|
||||
return $parameters;
|
||||
default:
|
||||
throw new InvalidArgumentException("Invalid scheme: {$parameters->scheme}");
|
||||
}
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
protected function initializeProtocol(ConnectionParameters $parameters) {
|
||||
$this->setProtocolOption('throw_errors', $parameters->throw_errors);
|
||||
$this->setProtocolOption('iterable_multibulk', $parameters->iterable_multibulk);
|
||||
}
|
||||
|
||||
protected abstract function createResource();
|
||||
|
||||
public function isConnected() {
|
||||
return isset($this->_resource);
|
||||
}
|
||||
|
||||
public function connect() {
|
||||
if ($this->isConnected()) {
|
||||
throw new ClientException('Connection already estabilished');
|
||||
@@ -38,7 +63,7 @@ abstract class ConnectionBase implements IConnectionSingle {
|
||||
unset($this->_resource);
|
||||
}
|
||||
|
||||
public function pushInitCommand(ICommand $command){
|
||||
public function pushInitCommand(ICommand $command) {
|
||||
$this->_initCmds[] = $command;
|
||||
}
|
||||
|
||||
@@ -53,6 +78,14 @@ abstract class ConnectionBase implements IConnectionSingle {
|
||||
);
|
||||
}
|
||||
|
||||
protected function onInvalidOption($option, $parameters = null) {
|
||||
$message = "Invalid option: $option";
|
||||
if (isset($parameters)) {
|
||||
$message .= " [$parameters]";
|
||||
}
|
||||
throw new InvalidArgumentException($message);
|
||||
}
|
||||
|
||||
public function getResource() {
|
||||
if (isset($this->_resource)) {
|
||||
return $this->_resource;
|
||||
|
||||
@@ -35,8 +35,7 @@ class PhpiredisConnection extends ConnectionBase {
|
||||
'use this connection class'
|
||||
);
|
||||
}
|
||||
parent::__construct($this->checkParameters($parameters));
|
||||
$this->_reader = $this->initializeReader();
|
||||
parent::__construct($parameters);
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
@@ -45,25 +44,13 @@ class PhpiredisConnection extends ConnectionBase {
|
||||
}
|
||||
|
||||
protected function checkParameters(ConnectionParameters $parameters) {
|
||||
switch ($parameters->scheme) {
|
||||
case 'unix':
|
||||
$pathToSocket = $parameters->path;
|
||||
if (!isset($pathToSocket)) {
|
||||
throw new \InvalidArgumentException('Missing UNIX domain socket path');
|
||||
}
|
||||
if (!file_exists($pathToSocket)) {
|
||||
throw new \InvalidArgumentException("Could not find $pathToSocket");
|
||||
}
|
||||
case 'tcp':
|
||||
return $parameters;
|
||||
default:
|
||||
throw new \InvalidArgumentException("Invalid scheme: {$parameters->scheme}");
|
||||
if (isset($parameters->iterable_multibulk)) {
|
||||
$this->onInvalidOption('iterable_multibulk', $parameters);
|
||||
}
|
||||
if ($parameters->connection_persistent == true) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Persistent connections are not supported by this connection class.'
|
||||
);
|
||||
if (isset($parameters->connection_persistent)) {
|
||||
$this->onInvalidOption('connection_persistent', $parameters);
|
||||
}
|
||||
return parent::checkParameters($parameters);
|
||||
}
|
||||
|
||||
private function initializeReader() {
|
||||
@@ -75,8 +62,13 @@ class PhpiredisConnection extends ConnectionBase {
|
||||
}
|
||||
$reader = phpiredis_reader_create();
|
||||
phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
|
||||
phpiredis_reader_set_error_handler($reader, $this->getErrorHandler(true));
|
||||
return $reader;
|
||||
phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
|
||||
$this->_reader = $reader;
|
||||
}
|
||||
|
||||
protected function initializeProtocol(ConnectionParameters $parameters) {
|
||||
$this->initializeReader();
|
||||
$this->setProtocolOption('throw_errors', $parameters->throw_errors);
|
||||
}
|
||||
|
||||
private function getStatusHandler() {
|
||||
@@ -92,7 +84,7 @@ class PhpiredisConnection extends ConnectionBase {
|
||||
};
|
||||
}
|
||||
|
||||
private function getErrorHandler($throwErrors) {
|
||||
private function getErrorHandler($throwErrors = true) {
|
||||
if ($throwErrors) {
|
||||
return function($errorMessage) {
|
||||
throw new ServerException(substr($errorMessage, 4));
|
||||
@@ -231,7 +223,7 @@ class PhpiredisConnection extends ConnectionBase {
|
||||
while (($length = strlen($buffer)) > 0) {
|
||||
$written = socket_write($socket, $buffer, $length);
|
||||
if ($length === $written) {
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
if ($written === false) {
|
||||
$this->onCommunicationException('Error while writing bytes to the server');
|
||||
@@ -270,15 +262,14 @@ class PhpiredisConnection extends ConnectionBase {
|
||||
|
||||
public function setProtocolOption($option, $value) {
|
||||
switch ($option) {
|
||||
case 'iterable_multibulk':
|
||||
// TODO: iterable multibulk replies cannot be supported
|
||||
break;
|
||||
case 'throw_errors':
|
||||
phpiredis_reader_set_error_handler(
|
||||
$this->_reader,
|
||||
$this->getErrorHandler((bool) $value)
|
||||
);
|
||||
break;
|
||||
default:
|
||||
$this->onInvalidOption($option, $this->getParameters());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,32 +14,15 @@ use Predis\Iterators\MultiBulkResponseSimple;
|
||||
class StreamConnection extends ConnectionBase {
|
||||
private $_commandSerializer, $_mbiterable, $_throwErrors;
|
||||
|
||||
public function __construct(ConnectionParameters $parameters) {
|
||||
parent::__construct($this->checkParameters($parameters));
|
||||
$this->_commandSerializer = new TextCommandSerializer();
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
if (!$this->_params->connection_persistent) {
|
||||
$this->disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
protected function checkParameters(ConnectionParameters $parameters) {
|
||||
switch ($parameters->scheme) {
|
||||
case 'unix':
|
||||
$pathToSocket = $parameters->path;
|
||||
if (!isset($pathToSocket)) {
|
||||
throw new \InvalidArgumentException('Missing UNIX domain socket path');
|
||||
}
|
||||
if (!file_exists($pathToSocket)) {
|
||||
throw new \InvalidArgumentException("Could not find $pathToSocket");
|
||||
}
|
||||
case 'tcp':
|
||||
return $parameters;
|
||||
default:
|
||||
throw new \InvalidArgumentException("Invalid scheme: {$parameters->scheme}");
|
||||
}
|
||||
protected function initializeProtocol(ConnectionParameters $parameters) {
|
||||
$this->_commandSerializer = new TextCommandSerializer();
|
||||
parent::initializeProtocol($parameters);
|
||||
}
|
||||
|
||||
protected function createResource() {
|
||||
@@ -213,6 +196,8 @@ class StreamConnection extends ConnectionBase {
|
||||
case 'throw_errors':
|
||||
$this->_throwErrors = (bool) $value;
|
||||
break;
|
||||
default:
|
||||
$this->onInvalidOption($option, $this->getParameters());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user