WebdisConnection.php 8.8 KB

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