WebdisConnection.php 9.5 KB

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