AbstractConnection.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\CommunicationException;
  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 Initialization parameters for the connection.
  46. * @return ConnectionParametersInterface
  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. abstract protected 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. 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(new ConnectionException($this, "$message [{$this->parameters->scheme}://{$this->getIdentifier()}]", $code));
  122. }
  123. /**
  124. * Helper method to handle protocol errors.
  125. *
  126. * @param string $message Error message.
  127. */
  128. protected function onProtocolError($message)
  129. {
  130. CommunicationException::handle(new ProtocolException($this, "$message [{$this->parameters->scheme}://{$this->getIdentifier()}]"));
  131. }
  132. /**
  133. * Helper method to handle not supported connection parameters.
  134. *
  135. * @param string $option Name of the option.
  136. * @param mixed $parameters Parameters used to initialize the connection.
  137. */
  138. protected function onInvalidOption($option, $parameters = null)
  139. {
  140. $class = get_called_class();
  141. $message = "Invalid option for connection $class: $option";
  142. if (isset($parameters)) {
  143. $message .= sprintf(' [%s => %s]', $option, $parameters->{$option});
  144. }
  145. throw new NotSupportedException($message);
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function getResource()
  151. {
  152. if (isset($this->resource)) {
  153. return $this->resource;
  154. }
  155. $this->connect();
  156. return $this->resource;
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getParameters()
  162. {
  163. return $this->parameters;
  164. }
  165. /**
  166. * Gets an identifier for the connection.
  167. *
  168. * @return string
  169. */
  170. protected function getIdentifier()
  171. {
  172. if ($this->parameters->scheme === 'unix') {
  173. return $this->parameters->path;
  174. }
  175. return "{$this->parameters->host}:{$this->parameters->port}";
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function __toString()
  181. {
  182. if (!isset($this->cachedId)) {
  183. $this->cachedId = $this->getIdentifier();
  184. }
  185. return $this->cachedId;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function __sleep()
  191. {
  192. return array('parameters', 'initCmds');
  193. }
  194. }