WebdisConnection.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. static $statusHandler;
  145. if (!$statusHandler) {
  146. $statusHandler = function ($payload) {
  147. return StatusResponse::get($payload);
  148. };
  149. }
  150. return $statusHandler;
  151. }
  152. /**
  153. * Returns the handler used by the protocol reader for error responses.
  154. *
  155. * @return \Closure
  156. */
  157. protected function getErrorHandler()
  158. {
  159. static $errorHandler;
  160. if (!$errorHandler) {
  161. $errorHandler = function ($errorMessage) {
  162. return new ErrorResponse($errorMessage);
  163. };
  164. }
  165. return $errorHandler;
  166. }
  167. /**
  168. * Feeds the phpredis reader resource with the data read from the network.
  169. *
  170. * @param resource $resource Reader resource.
  171. * @param string $buffer Buffer of data read from a connection.
  172. *
  173. * @return int
  174. */
  175. protected function feedReader($resource, $buffer)
  176. {
  177. phpiredis_reader_feed($this->reader, $buffer);
  178. return strlen($buffer);
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function connect()
  184. {
  185. // NOOP
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function disconnect()
  191. {
  192. // NOOP
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function isConnected()
  198. {
  199. return true;
  200. }
  201. /**
  202. * Checks if the specified command is supported by this connection class.
  203. *
  204. * @param CommandInterface $command Command instance.
  205. *
  206. * @throws NotSupportedException
  207. *
  208. * @return string
  209. */
  210. protected function getCommandId(CommandInterface $command)
  211. {
  212. switch ($commandID = $command->getId()) {
  213. case 'AUTH':
  214. case 'SELECT':
  215. case 'MULTI':
  216. case 'EXEC':
  217. case 'WATCH':
  218. case 'UNWATCH':
  219. case 'DISCARD':
  220. case 'MONITOR':
  221. throw new NotSupportedException("Command '$commandID' is not allowed by Webdis.");
  222. default:
  223. return $commandID;
  224. }
  225. }
  226. /**
  227. * {@inheritdoc}
  228. */
  229. public function writeRequest(CommandInterface $command)
  230. {
  231. $this->throwNotSupportedException(__FUNCTION__);
  232. }
  233. /**
  234. * {@inheritdoc}
  235. */
  236. public function readResponse(CommandInterface $command)
  237. {
  238. $this->throwNotSupportedException(__FUNCTION__);
  239. }
  240. /**
  241. * {@inheritdoc}
  242. */
  243. public function executeCommand(CommandInterface $command)
  244. {
  245. $resource = $this->resource;
  246. $commandId = $this->getCommandId($command);
  247. if ($arguments = $command->getArguments()) {
  248. $arguments = implode('/', array_map('urlencode', $arguments));
  249. $serializedCommand = "$commandId/$arguments.raw";
  250. } else {
  251. $serializedCommand = "$commandId.raw";
  252. }
  253. curl_setopt($resource, CURLOPT_POSTFIELDS, $serializedCommand);
  254. if (curl_exec($resource) === false) {
  255. $error = curl_error($resource);
  256. $errno = curl_errno($resource);
  257. throw new ConnectionException($this, trim($error), $errno);
  258. }
  259. if (phpiredis_reader_get_state($this->reader) !== PHPIREDIS_READER_STATE_COMPLETE) {
  260. throw new ProtocolException($this, phpiredis_reader_get_error($this->reader));
  261. }
  262. return phpiredis_reader_get_reply($this->reader);
  263. }
  264. /**
  265. * {@inheritdoc}
  266. */
  267. public function getResource()
  268. {
  269. return $this->resource;
  270. }
  271. /**
  272. * {@inheritdoc}
  273. */
  274. public function getParameters()
  275. {
  276. return $this->parameters;
  277. }
  278. /**
  279. * {@inheritdoc}
  280. */
  281. public function addConnectCommand(CommandInterface $command)
  282. {
  283. $this->throwNotSupportedException(__FUNCTION__);
  284. }
  285. /**
  286. * {@inheritdoc}
  287. */
  288. public function read()
  289. {
  290. $this->throwNotSupportedException(__FUNCTION__);
  291. }
  292. /**
  293. * {@inheritdoc}
  294. */
  295. public function __toString()
  296. {
  297. return "{$this->parameters->host}:{$this->parameters->port}";
  298. }
  299. /**
  300. * {@inheritdoc}
  301. */
  302. public function __sleep()
  303. {
  304. return array('parameters');
  305. }
  306. /**
  307. * {@inheritdoc}
  308. */
  309. public function __wakeup()
  310. {
  311. $this->assertExtensions();
  312. $this->resource = $this->createCurl();
  313. $this->reader = $this->createReader();
  314. }
  315. }