AbstractConnection.php 5.3 KB

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