ConnectionBase.php 3.6 KB

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