ConnectionBase.php 5.4 KB

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