ConnectionBase.php 3.4 KB

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