ConnectionBase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Predis\Network;
  3. use \InvalidArgumentException;
  4. use Predis\Utils;
  5. use Predis\ConnectionParameters;
  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(ConnectionParameters $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(ConnectionParameters $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(ConnectionParameters $parameters) {
  38. $this->setProtocolOption('throw_errors', $parameters->throw_errors);
  39. $this->setProtocolOption('iterable_multibulk', $parameters->iterable_multibulk);
  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. protected function onCommunicationException($message, $code = null) {
  62. Utils::onCommunicationException(
  63. new CommunicationException($this, $message, $code)
  64. );
  65. }
  66. protected function onInvalidOption($option, $parameters = null) {
  67. $message = "Invalid option: $option";
  68. if (isset($parameters)) {
  69. $message .= " [$parameters]";
  70. }
  71. throw new InvalidArgumentException($message);
  72. }
  73. public function getResource() {
  74. if (isset($this->_resource)) {
  75. return $this->_resource;
  76. }
  77. $this->connect();
  78. return $this->_resource;
  79. }
  80. public function getParameters() {
  81. return $this->_params;
  82. }
  83. public function __toString() {
  84. if (!isset($this->_cachedId)) {
  85. $this->_cachedId = "{$this->_params->host}:{$this->_params->port}";
  86. }
  87. return $this->_cachedId;
  88. }
  89. }