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