WebdisConnection.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 InvalidArgumentException;
  12. use Predis\NotSupportedException;
  13. use Predis\Command\CommandInterface;
  14. use Predis\Connection\ConnectionException;
  15. use Predis\Protocol\ProtocolException;
  16. use Predis\Response\Error as ErrorResponse;
  17. use Predis\Response\Status as StatusResponse;
  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 for responses 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. private $parameters;
  46. private $resource;
  47. private $reader;
  48. /**
  49. * @param ParametersInterface $parameters Initialization parameters for the connection.
  50. */
  51. public function __construct(ParametersInterface $parameters)
  52. {
  53. $this->assertExtensions();
  54. if ($parameters->scheme !== 'http') {
  55. throw new InvalidArgumentException("Invalid scheme: '{$parameters->scheme}'.");
  56. }
  57. $this->parameters = $parameters;
  58. $this->resource = $this->createCurl($parameters);
  59. $this->reader = $this->createReader($parameters);
  60. }
  61. /**
  62. * Frees the underlying cURL and protocol reader resources when the garbage
  63. * collector kicks in.
  64. */
  65. public function __destruct()
  66. {
  67. curl_close($this->resource);
  68. phpiredis_reader_destroy($this->reader);
  69. }
  70. /**
  71. * Helper method used to throw on unsupported methods.
  72. */
  73. private function throwNotSupportedException($function)
  74. {
  75. $class = __CLASS__;
  76. throw new NotSupportedException("The method $class::$function() is not supported.");
  77. }
  78. /**
  79. * Checks if the cURL and phpiredis extensions are loaded in PHP.
  80. */
  81. private function assertExtensions()
  82. {
  83. if (!extension_loaded('curl')) {
  84. throw new NotSupportedException(
  85. 'The "curl" extension is required by this connection backend.'
  86. );
  87. }
  88. if (!extension_loaded('phpiredis')) {
  89. throw new NotSupportedException(
  90. 'The "phpiredis" extension is required by this connection backend.'
  91. );
  92. }
  93. }
  94. /**
  95. * Initializes cURL.
  96. *
  97. * @param ParametersInterface $parameters Initialization parameters for the connection.
  98. * @return resource
  99. */
  100. private function createCurl(ParametersInterface $parameters)
  101. {
  102. $options = array(
  103. CURLOPT_FAILONERROR => true,
  104. CURLOPT_CONNECTTIMEOUT_MS => $parameters->timeout * 1000,
  105. CURLOPT_URL => "{$parameters->scheme}://{$parameters->host}:{$parameters->port}",
  106. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  107. CURLOPT_POST => true,
  108. CURLOPT_WRITEFUNCTION => array($this, 'feedReader'),
  109. );
  110. if (isset($parameters->user, $parameters->pass)) {
  111. $options[CURLOPT_USERPWD] = "{$parameters->user}:{$parameters->pass}";
  112. }
  113. curl_setopt_array($resource = curl_init(), $options);
  114. return $resource;
  115. }
  116. /**
  117. * Initializes the phpiredis protocol reader.
  118. *
  119. * @param ParametersInterface $parameters Initialization parameters for the connection.
  120. * @return resource
  121. */
  122. private function createReader(ParametersInterface $parameters)
  123. {
  124. $reader = phpiredis_reader_create();
  125. phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
  126. phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
  127. return $reader;
  128. }
  129. /**
  130. * Returns the handler used by the protocol reader for inline responses.
  131. *
  132. * @return \Closure
  133. */
  134. protected function getStatusHandler()
  135. {
  136. return function ($payload) {
  137. return StatusResponse::get($payload);
  138. };
  139. }
  140. /**
  141. * Returns the handler used by the protocol reader for error responses.
  142. *
  143. * @return \Closure
  144. */
  145. protected function getErrorHandler()
  146. {
  147. return function ($payload) {
  148. return new ErrorResponse($payload);
  149. };
  150. }
  151. /**
  152. * Feeds the phpredis reader resource with the data read from the network.
  153. *
  154. * @param resource $resource Reader resource.
  155. * @param string $buffer Buffer of data read from a connection.
  156. * @return int
  157. */
  158. protected function feedReader($resource, $buffer)
  159. {
  160. phpiredis_reader_feed($this->reader, $buffer);
  161. return strlen($buffer);
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function connect()
  167. {
  168. // NOOP
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function disconnect()
  174. {
  175. // NOOP
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function isConnected()
  181. {
  182. return true;
  183. }
  184. /**
  185. * Checks if the specified command is supported by this connection class.
  186. *
  187. * @param CommandInterface $command Command instance.
  188. * @return string
  189. */
  190. protected function getCommandId(CommandInterface $command)
  191. {
  192. switch ($commandID = $command->getId()) {
  193. case 'AUTH':
  194. case 'SELECT':
  195. case 'MULTI':
  196. case 'EXEC':
  197. case 'WATCH':
  198. case 'UNWATCH':
  199. case 'DISCARD':
  200. case 'MONITOR':
  201. throw new NotSupportedException("Command '$commandID' is not allowed by Webdis.");
  202. default:
  203. return $commandID;
  204. }
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function writeRequest(CommandInterface $command)
  210. {
  211. $this->throwNotSupportedException(__FUNCTION__);
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function readResponse(CommandInterface $command)
  217. {
  218. $this->throwNotSupportedException(__FUNCTION__);
  219. }
  220. /**
  221. * {@inheritdoc}
  222. */
  223. public function executeCommand(CommandInterface $command)
  224. {
  225. $resource = $this->resource;
  226. $commandId = $this->getCommandId($command);
  227. if ($arguments = $command->getArguments()) {
  228. $arguments = implode('/', array_map('urlencode', $arguments));
  229. $serializedCommand = "$commandId/$arguments.raw";
  230. } else {
  231. $serializedCommand = "$commandId.raw";
  232. }
  233. curl_setopt($resource, CURLOPT_POSTFIELDS, $serializedCommand);
  234. if (curl_exec($resource) === false) {
  235. $error = curl_error($resource);
  236. $errno = curl_errno($resource);
  237. throw new ConnectionException($this, trim($error), $errno);
  238. }
  239. if (phpiredis_reader_get_state($this->reader) !== PHPIREDIS_READER_STATE_COMPLETE) {
  240. throw new ProtocolException($this, phpiredis_reader_get_error($this->reader));
  241. }
  242. return phpiredis_reader_get_reply($this->reader);
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. public function getResource()
  248. {
  249. return $this->resource;
  250. }
  251. /**
  252. * {@inheritdoc}
  253. */
  254. public function getParameters()
  255. {
  256. return $this->parameters;
  257. }
  258. /**
  259. * {@inheritdoc}
  260. */
  261. public function addConnectCommand(CommandInterface $command)
  262. {
  263. $this->throwNotSupportedException(__FUNCTION__);
  264. }
  265. /**
  266. * {@inheritdoc}
  267. */
  268. public function read()
  269. {
  270. $this->throwNotSupportedException(__FUNCTION__);
  271. }
  272. /**
  273. * {@inheritdoc}
  274. */
  275. public function __toString()
  276. {
  277. return "{$this->parameters->host}:{$this->parameters->port}";
  278. }
  279. /**
  280. * {@inheritdoc}
  281. */
  282. public function __sleep()
  283. {
  284. return array('parameters');
  285. }
  286. /**
  287. * {@inheritdoc}
  288. */
  289. public function __wakeup()
  290. {
  291. $this->assertExtensions();
  292. $parameters = $this->getParameters();
  293. $this->resource = $this->createCurl($parameters);
  294. $this->reader = $this->createReader($parameters);
  295. }
  296. }