WebdisConnection.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Connection;
  11. use Predis\Command\CommandInterface;
  12. use Predis\ResponseObjectInterface;
  13. use Predis\ConnectionParametersInterface;
  14. use Predis\ResponseError;
  15. use Predis\NotSupportedException;
  16. use Predis\Protocol\ProtocolException;
  17. use Predis\Connection\ConnectionException;
  18. /**
  19. * This class implements a Predis connection that actually talks with Webdis
  20. * instead of connecting directly to Redis. It relies on the cURL extension to
  21. * communicate with the web server and the phpiredis extension to parse the
  22. * protocol of the replies returned in the http response bodies.
  23. *
  24. * Some features are not yet available or they simply cannot be implemented:
  25. * - Pipelining commands.
  26. * - Publish / Subscribe.
  27. * - MULTI / EXEC transactions (not yet supported by Webdis).
  28. *
  29. * The connection parameters supported by this class are:
  30. *
  31. * - scheme: must be 'http'.
  32. * - host: hostname or IP address of the server.
  33. * - port: TCP port of the server.
  34. * - timeout: timeout to perform the connection.
  35. * - user: username for authentication.
  36. * - pass: password for authentication.
  37. *
  38. * @link http://webd.is
  39. * @link http://github.com/nicolasff/webdis
  40. * @link http://github.com/seppo0010/phpiredis
  41. * @author Daniele Alessandri <suppakilla@gmail.com>
  42. */
  43. class WebdisConnection implements SingleConnectionInterface
  44. {
  45. const ERR_MSG_EXTENSION = 'The %s extension must be loaded in order to be able to use this connection class';
  46. private $parameters;
  47. private $resource;
  48. private $reader;
  49. /**
  50. * @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
  51. */
  52. public function __construct(ConnectionParametersInterface $parameters)
  53. {
  54. $this->checkExtensions();
  55. if ($parameters->scheme !== 'http') {
  56. throw new \InvalidArgumentException("Invalid scheme: {$parameters->scheme}");
  57. }
  58. $this->parameters = $parameters;
  59. $this->resource = $this->initializeCurl($parameters);
  60. $this->reader = $this->initializeReader($parameters);
  61. }
  62. /**
  63. * Frees the underlying cURL and protocol reader resources when PHP's
  64. * garbage collector kicks in.
  65. */
  66. public function __destruct()
  67. {
  68. curl_close($this->resource);
  69. phpiredis_reader_destroy($this->reader);
  70. }
  71. /**
  72. * Helper method used to throw on unsupported methods.
  73. */
  74. private function throwNotSupportedException($function)
  75. {
  76. $class = __CLASS__;
  77. throw new NotSupportedException("The method $class::$function() is not supported");
  78. }
  79. /**
  80. * Checks if the cURL and phpiredis extensions are loaded in PHP.
  81. */
  82. private function checkExtensions()
  83. {
  84. if (!function_exists('curl_init')) {
  85. throw new NotSupportedException(sprintf(self::ERR_MSG_EXTENSION, 'curl'));
  86. }
  87. if (!function_exists('phpiredis_reader_create')) {
  88. throw new NotSupportedException(sprintf(self::ERR_MSG_EXTENSION, 'phpiredis'));
  89. }
  90. }
  91. /**
  92. * Initializes cURL.
  93. *
  94. * @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
  95. * @return resource
  96. */
  97. private function initializeCurl(ConnectionParametersInterface $parameters)
  98. {
  99. $options = array(
  100. CURLOPT_FAILONERROR => true,
  101. CURLOPT_CONNECTTIMEOUT_MS => $parameters->timeout * 1000,
  102. CURLOPT_URL => "{$parameters->scheme}://{$parameters->host}:{$parameters->port}",
  103. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  104. CURLOPT_POST => true,
  105. CURLOPT_WRITEFUNCTION => array($this, 'feedReader'),
  106. );
  107. if (isset($parameters->user, $parameters->pass)) {
  108. $options[CURLOPT_USERPWD] = "{$parameters->user}:{$parameters->pass}";
  109. }
  110. $resource = curl_init();
  111. curl_setopt_array($resource, $options);
  112. return $resource;
  113. }
  114. /**
  115. * Initializes phpiredis' protocol reader.
  116. *
  117. * @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
  118. * @return resource
  119. */
  120. private function initializeReader(ConnectionParametersInterface $parameters)
  121. {
  122. $reader = phpiredis_reader_create();
  123. phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
  124. phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
  125. return $reader;
  126. }
  127. /**
  128. * Gets the handler used by the protocol reader to handle status replies.
  129. *
  130. * @return \Closure
  131. */
  132. protected function getStatusHandler()
  133. {
  134. return function($payload) {
  135. return $payload === 'OK' ? true : $payload;
  136. };
  137. }
  138. /**
  139. * Gets the handler used by the protocol reader to handle Redis errors.
  140. *
  141. * @return \Closure
  142. */
  143. protected function getErrorHandler()
  144. {
  145. return function($errorMessage) {
  146. return new ResponseError($errorMessage);
  147. };
  148. }
  149. /**
  150. * Feeds phpredis' reader resource with the data read from the network.
  151. *
  152. * @param resource $resource Reader resource.
  153. * @param string $buffer Buffer with the reply read from the network.
  154. * @return int
  155. */
  156. protected function feedReader($resource, $buffer)
  157. {
  158. phpiredis_reader_feed($this->reader, $buffer);
  159. return strlen($buffer);
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function connect()
  165. {
  166. // NOOP
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function disconnect()
  172. {
  173. // NOOP
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function isConnected()
  179. {
  180. return true;
  181. }
  182. /**
  183. * Checks if the specified command is supported by this connection class.
  184. *
  185. * @param CommandInterface $command The instance of a Redis command.
  186. * @return string
  187. */
  188. protected function getCommandId(CommandInterface $command)
  189. {
  190. switch (($commandId = $command->getId())) {
  191. case 'AUTH':
  192. case 'SELECT':
  193. case 'MULTI':
  194. case 'EXEC':
  195. case 'WATCH':
  196. case 'UNWATCH':
  197. case 'DISCARD':
  198. case 'MONITOR':
  199. throw new NotSupportedException("Disabled command: {$command->getId()}");
  200. default:
  201. return $commandId;
  202. }
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function writeCommand(CommandInterface $command)
  208. {
  209. $this->throwNotSupportedException(__FUNCTION__);
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function readResponse(CommandInterface $command)
  215. {
  216. $this->throwNotSupportedException(__FUNCTION__);
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function executeCommand(CommandInterface $command)
  222. {
  223. $resource = $this->resource;
  224. $commandId = $this->getCommandId($command);
  225. if ($arguments = $command->getArguments()) {
  226. $arguments = implode('/', array_map('urlencode', $arguments));
  227. $serializedCommand = "$commandId/$arguments.raw";
  228. }
  229. else {
  230. $serializedCommand = "$commandId.raw";
  231. }
  232. curl_setopt($resource, CURLOPT_POSTFIELDS, $serializedCommand);
  233. if (curl_exec($resource) === false) {
  234. $error = curl_error($resource);
  235. $errno = curl_errno($resource);
  236. throw new ConnectionException($this, trim($error), $errno);
  237. }
  238. $readerState = phpiredis_reader_get_state($this->reader);
  239. if ($readerState === PHPIREDIS_READER_STATE_COMPLETE) {
  240. $reply = phpiredis_reader_get_reply($this->reader);
  241. if ($reply instanceof ResponseObjectInterface) {
  242. return $reply;
  243. }
  244. return $command->parseResponse($reply);
  245. }
  246. else {
  247. $error = phpiredis_reader_get_error($this->reader);
  248. throw new ProtocolException($this, $error);
  249. }
  250. }
  251. /**
  252. * {@inheritdoc}
  253. */
  254. public function getResource()
  255. {
  256. return $this->resource;
  257. }
  258. /**
  259. * {@inheritdoc}
  260. */
  261. public function getParameters()
  262. {
  263. return $this->parameters;
  264. }
  265. /**
  266. * {@inheritdoc}
  267. */
  268. public function pushInitCommand(CommandInterface $command)
  269. {
  270. $this->throwNotSupportedException(__FUNCTION__);
  271. }
  272. /**
  273. * {@inheritdoc}
  274. */
  275. public function read()
  276. {
  277. $this->throwNotSupportedException(__FUNCTION__);
  278. }
  279. /**
  280. * {@inheritdoc}
  281. */
  282. public function __toString()
  283. {
  284. return "{$this->parameters->host}:{$this->parameters->port}";
  285. }
  286. /**
  287. * {@inheritdoc}
  288. */
  289. public function __sleep()
  290. {
  291. return array('parameters');
  292. }
  293. /**
  294. * {@inheritdoc}
  295. */
  296. public function __wakeup()
  297. {
  298. $this->checkExtensions();
  299. $parameters = $this->getParameters();
  300. $this->resource = $this->initializeCurl($parameters);
  301. $this->reader = $this->initializeReader($parameters);
  302. }
  303. }