WebdisConnection.php 9.1 KB

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