123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?php
- namespace Predis\Connection;
- use Predis\NotSupportedException;
- use Predis\ResponseError;
- use Predis\Command\CommandInterface;
- use Predis\Connection\ConnectionException;
- use Predis\Protocol\ProtocolException;
- class WebdisConnection implements SingleConnectionInterface
- {
- const ERR_MSG_EXTENSION = 'The %s extension must be loaded in order to be able to use this connection class';
- private $parameters;
- private $resource;
- private $reader;
-
- public function __construct(ConnectionParametersInterface $parameters)
- {
- $this->checkExtensions();
- if ($parameters->scheme !== 'http') {
- throw new \InvalidArgumentException("Invalid scheme: {$parameters->scheme}");
- }
- $this->parameters = $parameters;
- $this->resource = $this->initializeCurl($parameters);
- $this->reader = $this->initializeReader($parameters);
- }
-
- public function __destruct()
- {
- curl_close($this->resource);
- phpiredis_reader_destroy($this->reader);
- }
-
- private function throwNotSupportedException($function)
- {
- $class = __CLASS__;
- throw new NotSupportedException("The method $class::$function() is not supported");
- }
-
- private function checkExtensions()
- {
- if (!function_exists('curl_init')) {
- throw new NotSupportedException(sprintf(self::ERR_MSG_EXTENSION, 'curl'));
- }
- if (!function_exists('phpiredis_reader_create')) {
- throw new NotSupportedException(sprintf(self::ERR_MSG_EXTENSION, 'phpiredis'));
- }
- }
-
- private function initializeCurl(ConnectionParametersInterface $parameters)
- {
- $options = array(
- CURLOPT_FAILONERROR => true,
- CURLOPT_CONNECTTIMEOUT_MS => $parameters->timeout * 1000,
- CURLOPT_URL => "{$parameters->scheme}://{$parameters->host}:{$parameters->port}",
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_POST => true,
- CURLOPT_WRITEFUNCTION => array($this, 'feedReader'),
- );
- if (isset($parameters->user, $parameters->pass)) {
- $options[CURLOPT_USERPWD] = "{$parameters->user}:{$parameters->pass}";
- }
- curl_setopt_array($resource = curl_init(), $options);
- return $resource;
- }
-
- private function initializeReader(ConnectionParametersInterface $parameters)
- {
- $reader = phpiredis_reader_create();
- phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
- phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
- return $reader;
- }
-
- protected function getStatusHandler()
- {
- return function ($payload) {
- return $payload === 'OK' ? true : $payload;
- };
- }
-
- protected function getErrorHandler()
- {
- return function ($errorMessage) {
- return new ResponseError($errorMessage);
- };
- }
-
- protected function feedReader($resource, $buffer)
- {
- phpiredis_reader_feed($this->reader, $buffer);
- return strlen($buffer);
- }
-
- public function connect()
- {
-
- }
-
- public function disconnect()
- {
-
- }
-
- public function isConnected()
- {
- return true;
- }
-
- protected function getCommandId(CommandInterface $command)
- {
- switch (($commandId = $command->getId())) {
- case 'AUTH':
- case 'SELECT':
- case 'MULTI':
- case 'EXEC':
- case 'WATCH':
- case 'UNWATCH':
- case 'DISCARD':
- case 'MONITOR':
- throw new NotSupportedException("Disabled command: {$command->getId()}");
- default:
- return $commandId;
- }
- }
-
- public function writeCommand(CommandInterface $command)
- {
- $this->throwNotSupportedException(__FUNCTION__);
- }
-
- public function readResponse(CommandInterface $command)
- {
- $this->throwNotSupportedException(__FUNCTION__);
- }
-
- public function executeCommand(CommandInterface $command)
- {
- $resource = $this->resource;
- $commandId = $this->getCommandId($command);
- if ($arguments = $command->getArguments()) {
- $arguments = implode('/', array_map('urlencode', $arguments));
- $serializedCommand = "$commandId/$arguments.raw";
- } else {
- $serializedCommand = "$commandId.raw";
- }
- curl_setopt($resource, CURLOPT_POSTFIELDS, $serializedCommand);
- if (curl_exec($resource) === false) {
- $error = curl_error($resource);
- $errno = curl_errno($resource);
- throw new ConnectionException($this, trim($error), $errno);
- }
- if (phpiredis_reader_get_state($this->reader) !== PHPIREDIS_READER_STATE_COMPLETE) {
- throw new ProtocolException($this, phpiredis_reader_get_error($this->reader));
- }
- return phpiredis_reader_get_reply($this->reader);
- }
-
- public function getResource()
- {
- return $this->resource;
- }
-
- public function getParameters()
- {
- return $this->parameters;
- }
-
- public function pushInitCommand(CommandInterface $command)
- {
- $this->throwNotSupportedException(__FUNCTION__);
- }
-
- public function read()
- {
- $this->throwNotSupportedException(__FUNCTION__);
- }
-
- public function __toString()
- {
- return "{$this->parameters->host}:{$this->parameters->port}";
- }
-
- public function __sleep()
- {
- return array('parameters');
- }
-
- public function __wakeup()
- {
- $this->checkExtensions();
- $parameters = $this->getParameters();
- $this->resource = $this->initializeCurl($parameters);
- $this->reader = $this->initializeReader($parameters);
- }
- }
|