AbstractConnection.php 5.2 KB

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