ConnectionBase.php 3.6 KB

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