ConnectionBase.php 3.4 KB

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