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