WebdisConnection.php 8.8 KB

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