ConnectionBase.php 3.6 KB

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