123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace Predis\Connection;
- use Predis\ClientException;
- use Predis\Helpers;
- use Predis\NotSupportedException;
- use Predis\Command\CommandInterface;
- use Predis\Protocol\ProtocolException;
- abstract class AbstractConnection implements SingleConnectionInterface
- {
- private $resource;
- private $cachedId;
- protected $parameters;
- protected $initCmds = array();
-
- public function __construct(ConnectionParametersInterface $parameters)
- {
- $this->parameters = $this->checkParameters($parameters);
- }
-
- public function __destruct()
- {
- $this->disconnect();
- }
-
- 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}");
- }
- }
-
- protected abstract function createResource();
-
- public function isConnected()
- {
- return isset($this->resource);
- }
-
- public function connect()
- {
- if ($this->isConnected()) {
- throw new ClientException('Connection already estabilished');
- }
- $this->resource = $this->createResource();
- }
-
- public function disconnect()
- {
- unset($this->resource);
- }
-
- public function pushInitCommand(CommandInterface $command)
- {
- $this->initCmds[] = $command;
- }
-
- public function executeCommand(CommandInterface $command)
- {
- $this->writeCommand($command);
- return $this->readResponse($command);
- }
-
- public function readResponse(CommandInterface $command)
- {
- return $this->read();
- }
-
- protected function onConnectionError($message, $code = null)
- {
- Helpers::onCommunicationException(new ConnectionException($this, $message, $code));
- }
-
- protected function onProtocolError($message)
- {
- Helpers::onCommunicationException(new ProtocolException($this, $message));
- }
-
- protected function onInvalidOption($option, $parameters = null)
- {
- $class = get_called_class();
- $message = "Invalid option for connection $class: $option";
- if (isset($parameters)) {
- $message .= sprintf(' [%s => %s]', $option, $parameters->{$option});
- }
- throw new NotSupportedException($message);
- }
-
- public function getResource()
- {
- if (isset($this->resource)) {
- return $this->resource;
- }
- $this->connect();
- return $this->resource;
- }
-
- public function getParameters()
- {
- return $this->parameters;
- }
-
- protected function getIdentifier()
- {
- if ($this->parameters->scheme === 'unix') {
- return $this->parameters->path;
- }
- return "{$this->parameters->host}:{$this->parameters->port}";
- }
-
- public function __toString()
- {
- if (!isset($this->cachedId)) {
- $this->cachedId = $this->getIdentifier();
- }
- return $this->cachedId;
- }
-
- public function __sleep()
- {
- return array('parameters', 'initCmds');
- }
- }
|