WebdisConnection.php 8.7 KB

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