ConnectionBase.php 3.4 KB

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