WebdisConnection.php 9.0 KB

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