mirror of
https://github.com/predis/predis.git
synced 2026-08-24 03:29:32 +00:00
dc14c29676
Now we follow a Symfony2-like naming convention for namespaces, interfaces and classes sticking with one clear rule. - Renamed namespaces: - Predis\Network - Predis\Profiles - Predis\Iterators - Predis\Options - Predis\Commands - Predis\Commands\Processors - Renamed interfaces: - Predis\IReplyObject - Predis\IRedisServerError - Predis\IConnectionFactory - Predis\IConnectionParameters - Predis\Options\IOption - Predis\Options\IClientOptions - Predis\Profile\IServerProfile - Predis\Pipeline\IPipelineExecutor - Predis\Distribution\INodeKeyGenerator - Predis\Distribution\IDistributionStrategy - Predis\Protocol\IProtocolProcessor - Predis\Protocol\IResponseReader - Predis\Protocol\IResponseHandler - Predis\Protocol\ICommandSerializer - Predis\Protocol\IComposableProtocolProcessor - Predis\Network\IConnection - Predis\Network\IConnectionSingle - Predis\Network\IConnectionComposable - Predis\Network\IConnectionCluster - Predis\Network\IConnectionReplication - Predis\Commands\ICommand - Predis\Commands\IPrefixable - Predis\Command\Processor\ICommandProcessor - Predis\Command\Processor\ICommandProcessorChain - Predis\Command\Processor\IProcessingSupport - Renamed Classes: - Predis\Commands\Command - Predis\Network\ConnectionBase - Classes moved to different namespaces: - Predis\MonitorContext Meh
244 lines
5.7 KiB
PHP
244 lines
5.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Predis\Connection;
|
|
|
|
use Predis\Helpers;
|
|
use Predis\ResponseObjectInterface;
|
|
use Predis\ConnectionParametersInterface;
|
|
use Predis\ClientException;
|
|
use Predis\NotSupportedException;
|
|
use Predis\Command\CommandInterface;
|
|
use Predis\Protocol\ProtocolException;
|
|
|
|
/**
|
|
* Base class with the common logic used by connection classes to communicate with Redis.
|
|
*
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
*/
|
|
abstract class AbstractConnection implements SingleConnectionInterface
|
|
{
|
|
private $resource;
|
|
private $cachedId;
|
|
|
|
protected $parameters;
|
|
protected $initCmds;
|
|
|
|
/**
|
|
* @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
|
|
*/
|
|
public function __construct(ConnectionParametersInterface $parameters)
|
|
{
|
|
$this->initCmds = array();
|
|
$this->parameters = $this->checkParameters($parameters);
|
|
$this->initializeProtocol($parameters);
|
|
}
|
|
|
|
/**
|
|
* Disconnects from the server and destroys the underlying resource when
|
|
* PHP's garbage collector kicks in.
|
|
*/
|
|
public function __destruct()
|
|
{
|
|
$this->disconnect();
|
|
}
|
|
|
|
/**
|
|
* Checks some of the parameters used to initialize the connection.
|
|
*
|
|
* @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
|
|
*/
|
|
protected function checkParameters(ConnectionParametersInterface $parameters)
|
|
{
|
|
switch ($parameters->scheme) {
|
|
case 'unix':
|
|
if (!isset($parameters->path)) {
|
|
throw new \InvalidArgumentException('Missing UNIX domain socket path');
|
|
}
|
|
|
|
case 'tcp':
|
|
return $parameters;
|
|
|
|
default:
|
|
throw new \InvalidArgumentException("Invalid scheme: {$parameters->scheme}");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initializes some common configurations of the underlying protocol processor
|
|
* from the connection parameters.
|
|
*
|
|
* @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
|
|
*/
|
|
protected function initializeProtocol(ConnectionParametersInterface $parameters)
|
|
{
|
|
// NOOP
|
|
}
|
|
|
|
/**
|
|
* Creates the underlying resource used to communicate with Redis.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
protected abstract function createResource();
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function isConnected()
|
|
{
|
|
return isset($this->resource);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function connect()
|
|
{
|
|
if ($this->isConnected()) {
|
|
throw new ClientException('Connection already estabilished');
|
|
}
|
|
$this->resource = $this->createResource();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function disconnect()
|
|
{
|
|
unset($this->resource);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function pushInitCommand(CommandInterface $command)
|
|
{
|
|
$this->initCmds[] = $command;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function executeCommand(CommandInterface $command)
|
|
{
|
|
$this->writeCommand($command);
|
|
return $this->readResponse($command);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function readResponse(CommandInterface $command)
|
|
{
|
|
$reply = $this->read();
|
|
|
|
if ($reply instanceof ResponseObjectInterface) {
|
|
return $reply;
|
|
}
|
|
|
|
return $command->parseResponse($reply);
|
|
}
|
|
|
|
/**
|
|
* Helper method to handle connection errors.
|
|
*
|
|
* @param string $message Error message.
|
|
* @param int $code Error code.
|
|
*/
|
|
protected function onConnectionError($message, $code = null)
|
|
{
|
|
Helpers::onCommunicationException(new ConnectionException($this, $message, $code));
|
|
}
|
|
|
|
/**
|
|
* Helper method to handle protocol errors.
|
|
*
|
|
* @param string $message Error message.
|
|
*/
|
|
protected function onProtocolError($message)
|
|
{
|
|
Helpers::onCommunicationException(new ProtocolException($this, $message));
|
|
}
|
|
|
|
/**
|
|
* Helper method to handle not supported connection parameters.
|
|
*
|
|
* @param string $option Name of the option.
|
|
* @param mixed $parameters Parameters used to initialize the connection.
|
|
*/
|
|
protected function onInvalidOption($option, $parameters = null)
|
|
{
|
|
$message = "Invalid option: $option";
|
|
if (isset($parameters)) {
|
|
$message .= " [$parameters]";
|
|
}
|
|
|
|
throw new NotSupportedException($message);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getResource()
|
|
{
|
|
if (isset($this->resource)) {
|
|
return $this->resource;
|
|
}
|
|
|
|
$this->connect();
|
|
|
|
return $this->resource;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getParameters()
|
|
{
|
|
return $this->parameters;
|
|
}
|
|
|
|
/**
|
|
* Gets an identifier for the connection.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function getIdentifier()
|
|
{
|
|
if ($this->parameters->scheme === 'unix') {
|
|
return $this->parameters->path;
|
|
}
|
|
|
|
return "{$this->parameters->host}:{$this->parameters->port}";
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function __toString()
|
|
{
|
|
if (!isset($this->cachedId)) {
|
|
$this->cachedId = $this->getIdentifier();
|
|
}
|
|
|
|
return $this->cachedId;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function __sleep()
|
|
{
|
|
return array('parameters', 'initCmds');
|
|
}
|
|
}
|