AbstractConnection.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 InvalidArgumentException;
  12. use Predis\CommunicationException;
  13. use Predis\Command\CommandInterface;
  14. use Predis\Protocol\ProtocolException;
  15. /**
  16. * Base class with the common logic used by connection classes to communicate
  17. * with Redis.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. abstract class AbstractConnection implements NodeConnectionInterface
  22. {
  23. private $resource;
  24. private $cachedId;
  25. protected $parameters;
  26. protected $initCommands = array();
  27. /**
  28. * @param ParametersInterface $parameters Initialization parameters for the connection.
  29. */
  30. public function __construct(ParametersInterface $parameters)
  31. {
  32. $this->parameters = $this->assertParameters($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 ParametersInterface $parameters Initialization parameters for the connection.
  46. *
  47. * @return ParametersInterface
  48. */
  49. protected function assertParameters(ParametersInterface $parameters)
  50. {
  51. $scheme = $parameters->scheme;
  52. if ($scheme !== 'tcp' && $scheme !== 'unix') {
  53. throw new InvalidArgumentException("Invalid scheme: '$scheme'.");
  54. }
  55. if ($scheme === 'unix' && !isset($parameters->path)) {
  56. throw new InvalidArgumentException('Missing UNIX domain socket path.');
  57. }
  58. return $parameters;
  59. }
  60. /**
  61. * Creates the underlying resource used to communicate with Redis.
  62. *
  63. * @return mixed
  64. */
  65. abstract protected 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. $this->resource = $this->createResource();
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function disconnect()
  88. {
  89. unset($this->resource);
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function addConnectCommand(CommandInterface $command)
  95. {
  96. $this->initCommands[] = $command;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function executeCommand(CommandInterface $command)
  102. {
  103. $this->writeRequest($command);
  104. return $this->readResponse($command);
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function readResponse(CommandInterface $command)
  110. {
  111. return $this->read();
  112. }
  113. /**
  114. * Helper method to handle connection errors.
  115. *
  116. * @param string $message Error message.
  117. * @param int $code Error code.
  118. */
  119. protected function onConnectionError($message, $code = null)
  120. {
  121. CommunicationException::handle(
  122. new ConnectionException(
  123. $this, "$message [{$this->parameters->scheme}://{$this->getIdentifier()}]", $code
  124. )
  125. );
  126. }
  127. /**
  128. * Helper method to handle protocol errors.
  129. *
  130. * @param string $message Error message.
  131. */
  132. protected function onProtocolError($message)
  133. {
  134. CommunicationException::handle(
  135. new ProtocolException(
  136. $this, "$message [{$this->parameters->scheme}://{$this->getIdentifier()}]"
  137. )
  138. );
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function getResource()
  144. {
  145. if (isset($this->resource)) {
  146. return $this->resource;
  147. }
  148. $this->connect();
  149. return $this->resource;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getParameters()
  155. {
  156. return $this->parameters;
  157. }
  158. /**
  159. * Gets an identifier for the connection.
  160. *
  161. * @return string
  162. */
  163. protected function getIdentifier()
  164. {
  165. if ($this->parameters->scheme === 'unix') {
  166. return $this->parameters->path;
  167. }
  168. return "{$this->parameters->host}:{$this->parameters->port}";
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function __toString()
  174. {
  175. if (!isset($this->cachedId)) {
  176. $this->cachedId = $this->getIdentifier();
  177. }
  178. return $this->cachedId;
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function __sleep()
  184. {
  185. return array('parameters', 'initCommands');
  186. }
  187. }