AbstractConnection.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\CommunicationException;
  13. use Predis\Protocol\ProtocolException;
  14. /**
  15. * Base class with the common logic used by connection classes to communicate
  16. * with Redis.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. abstract class AbstractConnection implements NodeConnectionInterface
  21. {
  22. private $resource;
  23. private $cachedId;
  24. protected $parameters;
  25. protected $initCommands = array();
  26. /**
  27. * @param ParametersInterface $parameters Initialization parameters for the connection.
  28. */
  29. public function __construct(ParametersInterface $parameters)
  30. {
  31. $this->parameters = $this->assertParameters($parameters);
  32. }
  33. /**
  34. * Disconnects from the server and destroys the underlying resource when
  35. * PHP's garbage collector kicks in.
  36. */
  37. public function __destruct()
  38. {
  39. $this->disconnect();
  40. }
  41. /**
  42. * Checks some of the parameters used to initialize the connection.
  43. *
  44. * @param ParametersInterface $parameters Initialization parameters for the connection.
  45. *
  46. * @throws \InvalidArgumentException
  47. *
  48. * @return ParametersInterface
  49. */
  50. protected function assertParameters(ParametersInterface $parameters)
  51. {
  52. switch ($parameters->scheme) {
  53. case 'tcp':
  54. case 'redis':
  55. case 'unix':
  56. break;
  57. default:
  58. throw new \InvalidArgumentException("Invalid scheme: '$parameters->scheme'.");
  59. }
  60. return $parameters;
  61. }
  62. /**
  63. * Creates the underlying resource used to communicate with Redis.
  64. *
  65. * @return mixed
  66. */
  67. abstract protected function createResource();
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function isConnected()
  72. {
  73. return isset($this->resource);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function connect()
  79. {
  80. if (!$this->isConnected()) {
  81. $this->resource = $this->createResource();
  82. return true;
  83. }
  84. return false;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function disconnect()
  90. {
  91. unset($this->resource);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function addConnectCommand(CommandInterface $command)
  97. {
  98. $this->initCommands[] = $command;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function executeCommand(CommandInterface $command)
  104. {
  105. $this->writeRequest($command);
  106. return $this->readResponse($command);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function readResponse(CommandInterface $command)
  112. {
  113. return $this->read();
  114. }
  115. /**
  116. * Helper method that returns an exception message augmented with useful
  117. * details from the connection parameters.
  118. *
  119. * @param string $message Error message.
  120. *
  121. * @return string
  122. */
  123. private function createExceptionMessage($message)
  124. {
  125. $parameters = $this->parameters;
  126. if ($parameters->scheme === 'unix') {
  127. return "$message [$parameters->scheme:$parameters->path]";
  128. }
  129. if (filter_var($parameters->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  130. return "$message [$parameters->scheme://[$parameters->host]:$parameters->port]";
  131. }
  132. return "$message [$parameters->scheme://$parameters->host:$parameters->port]";
  133. }
  134. /**
  135. * Helper method to handle connection errors.
  136. *
  137. * @param string $message Error message.
  138. * @param int $code Error code.
  139. */
  140. protected function onConnectionError($message, $code = null)
  141. {
  142. CommunicationException::handle(
  143. new ConnectionException($this, static::createExceptionMessage($message), $code)
  144. );
  145. }
  146. /**
  147. * Helper method to handle protocol errors.
  148. *
  149. * @param string $message Error message.
  150. */
  151. protected function onProtocolError($message)
  152. {
  153. CommunicationException::handle(
  154. new ProtocolException($this, static::createExceptionMessage($message))
  155. );
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function getResource()
  161. {
  162. if (isset($this->resource)) {
  163. return $this->resource;
  164. }
  165. $this->connect();
  166. return $this->resource;
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function getParameters()
  172. {
  173. return $this->parameters;
  174. }
  175. /**
  176. * Gets an identifier for the connection.
  177. *
  178. * @return string
  179. */
  180. protected function getIdentifier()
  181. {
  182. if ($this->parameters->scheme === 'unix') {
  183. return $this->parameters->path;
  184. }
  185. return "{$this->parameters->host}:{$this->parameters->port}";
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function __toString()
  191. {
  192. if (!isset($this->cachedId)) {
  193. $this->cachedId = $this->getIdentifier();
  194. }
  195. return $this->cachedId;
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function __sleep()
  201. {
  202. return array('parameters', 'initCommands');
  203. }
  204. }