ConnectionBase.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Predis\Network;
  3. use \InvalidArgumentException;
  4. use Predis\Helpers;
  5. use Predis\IReplyObject;
  6. use Predis\IConnectionParameters;
  7. use Predis\ClientException;
  8. use Predis\Commands\ICommand;
  9. use Predis\Protocol\ProtocolException;
  10. abstract class ConnectionBase implements IConnectionSingle
  11. {
  12. private $_resource;
  13. private $_cachedId;
  14. protected $_params;
  15. protected $_initCmds;
  16. public function __construct(IConnectionParameters $parameters)
  17. {
  18. $this->_initCmds = array();
  19. $this->_params = $this->checkParameters($parameters);
  20. $this->initializeProtocol($parameters);
  21. }
  22. public function __destruct()
  23. {
  24. $this->disconnect();
  25. }
  26. protected function checkParameters(IConnectionParameters $parameters)
  27. {
  28. switch ($parameters->scheme) {
  29. case 'unix':
  30. if (!isset($parameters->path)) {
  31. throw new InvalidArgumentException('Missing UNIX domain socket path');
  32. }
  33. case 'tcp':
  34. return $parameters;
  35. default:
  36. throw new InvalidArgumentException("Invalid scheme: {$parameters->scheme}");
  37. }
  38. }
  39. protected function initializeProtocol(IConnectionParameters $parameters)
  40. {
  41. // NOOP
  42. }
  43. protected abstract function createResource();
  44. public function isConnected()
  45. {
  46. return isset($this->_resource);
  47. }
  48. public function connect()
  49. {
  50. if ($this->isConnected()) {
  51. throw new ClientException('Connection already estabilished');
  52. }
  53. $this->_resource = $this->createResource();
  54. }
  55. public function disconnect()
  56. {
  57. unset($this->_resource);
  58. }
  59. public function pushInitCommand(ICommand $command)
  60. {
  61. $this->_initCmds[] = $command;
  62. }
  63. public function executeCommand(ICommand $command)
  64. {
  65. $this->writeCommand($command);
  66. return $this->readResponse($command);
  67. }
  68. public function readResponse(ICommand $command)
  69. {
  70. $reply = $this->read();
  71. if ($reply instanceof IReplyObject) {
  72. return $reply;
  73. }
  74. return $command->parseResponse($reply);
  75. }
  76. protected function onConnectionError($message, $code = null)
  77. {
  78. Helpers::onCommunicationException(new ConnectionException($this, $message, $code));
  79. }
  80. protected function onProtocolError($message)
  81. {
  82. Helpers::onCommunicationException(new ProtocolException($this, $message));
  83. }
  84. protected function onInvalidOption($option, $parameters = null)
  85. {
  86. $message = "Invalid option: $option";
  87. if (isset($parameters)) {
  88. $message .= " [$parameters]";
  89. }
  90. throw new InvalidArgumentException($message);
  91. }
  92. public function getResource()
  93. {
  94. if (isset($this->_resource)) {
  95. return $this->_resource;
  96. }
  97. $this->connect();
  98. return $this->_resource;
  99. }
  100. public function getParameters()
  101. {
  102. return $this->_params;
  103. }
  104. protected function getIdentifier()
  105. {
  106. if ($this->_params->scheme === 'unix') {
  107. return $this->_params->path;
  108. }
  109. return "{$this->_params->host}:{$this->_params->port}";
  110. }
  111. public function __toString()
  112. {
  113. if (!isset($this->_cachedId)) {
  114. $this->_cachedId = $this->getIdentifier();
  115. }
  116. return $this->_cachedId;
  117. }
  118. }