ConnectionBase.php 3.6 KB

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