Client.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace Predis;
  3. use Predis\Commands\ICommand;
  4. use Predis\Network\IConnection;
  5. use Predis\Network\IConnectionSingle;
  6. use Predis\Network\ConnectionCluster;
  7. use Predis\Profiles\ServerProfile;
  8. use Predis\Profiles\IServerProfile;
  9. class Client {
  10. const VERSION = '0.7.0-dev';
  11. private $_options, $_connectionFactory, $_profile, $_connection;
  12. public function __construct($parameters = null, $options = null) {
  13. $options = $this->filterOptions($options);
  14. $this->_options = $options;
  15. $this->_profile = $options->profile;
  16. $this->_connectionFactory = $options->connections;
  17. $this->_connection = $this->initializeConnection($parameters);
  18. }
  19. private function filterOptions($options) {
  20. if ($options === null) {
  21. return new ClientOptions();
  22. }
  23. if ($options instanceof ClientOptions) {
  24. return $options;
  25. }
  26. if (is_array($options)) {
  27. return new ClientOptions($options);
  28. }
  29. if ($options instanceof IServerProfile) {
  30. return new ClientOptions(array('profile' => $options));
  31. }
  32. if (is_string($options)) {
  33. return new ClientOptions(array('profile' => ServerProfile::get($options)));
  34. }
  35. throw new \InvalidArgumentException("Invalid type for client options");
  36. }
  37. private function initializeConnection($parameters) {
  38. if ($parameters === null) {
  39. return $this->createConnection(new ConnectionParameters());
  40. }
  41. if (is_array($parameters)) {
  42. if (isset($parameters[0])) {
  43. $cluster = new ConnectionCluster($this->_options->key_distribution);
  44. foreach ($parameters as $single) {
  45. $cluster->add($single instanceof IConnectionSingle
  46. ? $single : $this->createConnection($single)
  47. );
  48. }
  49. return $cluster;
  50. }
  51. return $this->createConnection($parameters);
  52. }
  53. if ($parameters instanceof IConnection) {
  54. return $parameters;
  55. }
  56. return $this->createConnection($parameters);
  57. }
  58. private function createConnection($parameters) {
  59. $connection = $this->_connectionFactory->create($parameters);
  60. $this->pushInitCommands($connection);
  61. return $connection;
  62. }
  63. private function pushInitCommands(IConnectionSingle $connection) {
  64. $params = $connection->getParameters();
  65. if (isset($params->password)) {
  66. $connection->pushInitCommand($this->createCommand(
  67. 'auth', array($params->password)
  68. ));
  69. }
  70. if (isset($params->database)) {
  71. $connection->pushInitCommand($this->createCommand(
  72. 'select', array($params->database)
  73. ));
  74. }
  75. }
  76. public function getProfile() {
  77. return $this->_profile;
  78. }
  79. public function getOptions() {
  80. return $this->_options;
  81. }
  82. public function getConnectionFactory() {
  83. return $this->_connectionFactory;
  84. }
  85. public function getClientFor($connectionAlias) {
  86. if (!Helpers::isCluster($this->_connection)) {
  87. throw new ClientException(
  88. 'This method is supported only when the client is connected to a cluster of connections'
  89. );
  90. }
  91. $connection = $this->_connection->getConnectionById($connectionAlias);
  92. if ($connection === null) {
  93. throw new \InvalidArgumentException(
  94. "Invalid connection alias: '$connectionAlias'"
  95. );
  96. }
  97. return new Client($connection, $this->_options);
  98. }
  99. public function connect() {
  100. $this->_connection->connect();
  101. }
  102. public function disconnect() {
  103. $this->_connection->disconnect();
  104. }
  105. public function quit() {
  106. $this->disconnect();
  107. }
  108. public function isConnected() {
  109. return $this->_connection->isConnected();
  110. }
  111. public function getConnection($id = null) {
  112. if ($id === null) {
  113. return $this->_connection;
  114. }
  115. $connection = $this->_connection;
  116. $isCluster = Helpers::isCluster($connection);
  117. return $isCluster ? $connection->getConnectionById($id) : $connection;
  118. }
  119. public function __call($method, $arguments) {
  120. $command = $this->_profile->createCommand($method, $arguments);
  121. return $this->_connection->executeCommand($command);
  122. }
  123. public function createCommand($method, $arguments = array()) {
  124. return $this->_profile->createCommand($method, $arguments);
  125. }
  126. public function executeCommand(ICommand $command) {
  127. return $this->_connection->executeCommand($command);
  128. }
  129. public function executeCommandOnShards(ICommand $command) {
  130. if (Helpers::isCluster($this->_connection)) {
  131. $replies = array();
  132. foreach ($this->_connection as $connection) {
  133. $replies[] = $connection->executeCommand($command);
  134. }
  135. return $replies;
  136. }
  137. return array($this->_connection->executeCommand($command));
  138. }
  139. private function sharedInitializer($argv, $initializer) {
  140. switch (count($argv)) {
  141. case 0:
  142. return $this->$initializer();
  143. case 1:
  144. list($arg0) = $argv;
  145. return is_array($arg0)
  146. ? $this->$initializer($arg0)
  147. : $this->$initializer(null, $arg0);
  148. case 2:
  149. list($arg0, $arg1) = $argv;
  150. return $this->$initializer($arg0, $arg1);
  151. default:
  152. return $this->$initializer($this, $argv);
  153. }
  154. }
  155. public function pipeline(/* arguments */) {
  156. return $this->sharedInitializer(func_get_args(), 'initPipeline');
  157. }
  158. private function initPipeline(Array $options = null, $pipelineBlock = null) {
  159. $pipeline = null;
  160. if (isset($options)) {
  161. if (isset($options['safe']) && $options['safe'] == true) {
  162. $connection = $this->_connection;
  163. $pipeline = new PipelineContext($this,
  164. Helpers::isCluster($connection)
  165. ? new Pipeline\SafeClusterExecutor($connection)
  166. : new Pipeline\SafeExecutor($connection)
  167. );
  168. }
  169. else {
  170. $pipeline = new PipelineContext($this);
  171. }
  172. }
  173. return $this->pipelineExecute(
  174. $pipeline ?: new PipelineContext($this), $pipelineBlock
  175. );
  176. }
  177. private function pipelineExecute(PipelineContext $pipeline, $block) {
  178. return $block !== null ? $pipeline->execute($block) : $pipeline;
  179. }
  180. public function multiExec(/* arguments */) {
  181. return $this->sharedInitializer(func_get_args(), 'initMultiExec');
  182. }
  183. private function initMultiExec(Array $options = null, $transBlock = null) {
  184. $multi = isset($options) ? new MultiExecContext($this, $options) : new MultiExecContext($this);
  185. return $transBlock !== null ? $multi->execute($transBlock) : $multi;
  186. }
  187. public function pubSubContext(Array $options = null) {
  188. return new PubSubContext($this, $options);
  189. }
  190. }