ConnectionBase.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. }
  36. protected function initializeProtocol(IConnectionParameters $parameters) {
  37. // NOOP
  38. }
  39. protected abstract function createResource();
  40. public function isConnected() {
  41. return isset($this->_resource);
  42. }
  43. public function connect() {
  44. if ($this->isConnected()) {
  45. throw new ClientException('Connection already estabilished');
  46. }
  47. $this->_resource = $this->createResource();
  48. }
  49. public function disconnect() {
  50. unset($this->_resource);
  51. }
  52. public function pushInitCommand(ICommand $command) {
  53. $this->_initCmds[] = $command;
  54. }
  55. public function executeCommand(ICommand $command) {
  56. $this->writeCommand($command);
  57. return $this->readResponse($command);
  58. }
  59. public function readResponse(ICommand $command) {
  60. $reply = $this->read();
  61. if ($reply instanceof IReplyObject) {
  62. return $reply;
  63. }
  64. return $command->parseResponse($reply);
  65. }
  66. protected function onConnectionError($message, $code = null) {
  67. Helpers::onCommunicationException(
  68. new ConnectionException($this, $message, $code)
  69. );
  70. }
  71. protected function onProtocolError($message) {
  72. Helpers::onCommunicationException(
  73. new ProtocolException($this, $message)
  74. );
  75. }
  76. protected function onInvalidOption($option, $parameters = null) {
  77. $message = "Invalid option: $option";
  78. if (isset($parameters)) {
  79. $message .= " [$parameters]";
  80. }
  81. throw new InvalidArgumentException($message);
  82. }
  83. public function getResource() {
  84. if (isset($this->_resource)) {
  85. return $this->_resource;
  86. }
  87. $this->connect();
  88. return $this->_resource;
  89. }
  90. public function getParameters() {
  91. return $this->_params;
  92. }
  93. protected function getIdentifier() {
  94. if ($this->_params->scheme === 'unix') {
  95. return $this->_params->path;
  96. }
  97. return "{$this->_params->host}:{$this->_params->port}";
  98. }
  99. public function __toString() {
  100. if (!isset($this->_cachedId)) {
  101. $this->_cachedId = $this->getIdentifier();
  102. }
  103. return $this->_cachedId;
  104. }
  105. }