Client.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 = $this->_options->cluster;
  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 (($connection = $this->getConnection($connectionAlias)) === null) {
  87. throw new \InvalidArgumentException(
  88. "Invalid connection alias: '$connectionAlias'"
  89. );
  90. }
  91. return new Client($connection, $this->_options);
  92. }
  93. public function connect() {
  94. $this->_connection->connect();
  95. }
  96. public function disconnect() {
  97. $this->_connection->disconnect();
  98. }
  99. public function quit() {
  100. $this->disconnect();
  101. }
  102. public function isConnected() {
  103. return $this->_connection->isConnected();
  104. }
  105. public function getConnection($id = null) {
  106. if (isset($id)) {
  107. if (!Helpers::isCluster($this->_connection)) {
  108. throw new ClientException(
  109. 'Retrieving connections by alias is supported '.
  110. 'only with clustered connections'
  111. );
  112. }
  113. return $this->_connection->getConnectionById($id);
  114. }
  115. return $this->_connection;
  116. }
  117. public function __call($method, $arguments) {
  118. $command = $this->_profile->createCommand($method, $arguments);
  119. return $this->_connection->executeCommand($command);
  120. }
  121. public function createCommand($method, $arguments = array()) {
  122. return $this->_profile->createCommand($method, $arguments);
  123. }
  124. public function executeCommand(ICommand $command) {
  125. return $this->_connection->executeCommand($command);
  126. }
  127. public function executeCommandOnShards(ICommand $command) {
  128. if (Helpers::isCluster($this->_connection)) {
  129. $replies = array();
  130. foreach ($this->_connection as $connection) {
  131. $replies[] = $connection->executeCommand($command);
  132. }
  133. return $replies;
  134. }
  135. return array($this->_connection->executeCommand($command));
  136. }
  137. private function sharedInitializer($argv, $initializer) {
  138. switch (count($argv)) {
  139. case 0:
  140. return $this->$initializer();
  141. case 1:
  142. list($arg0) = $argv;
  143. return is_array($arg0)
  144. ? $this->$initializer($arg0)
  145. : $this->$initializer(null, $arg0);
  146. case 2:
  147. list($arg0, $arg1) = $argv;
  148. return $this->$initializer($arg0, $arg1);
  149. default:
  150. return $this->$initializer($this, $argv);
  151. }
  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 PipelineContext($this,
  162. Helpers::isCluster($connection)
  163. ? new Pipeline\SafeClusterExecutor()
  164. : new Pipeline\SafeExecutor()
  165. );
  166. }
  167. else {
  168. $pipeline = new PipelineContext($this);
  169. }
  170. }
  171. return $this->pipelineExecute(
  172. $pipeline ?: new PipelineContext($this), $pipelineBlock
  173. );
  174. }
  175. private function pipelineExecute(PipelineContext $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. public function monitor() {
  189. return new MonitorContext($this);
  190. }
  191. }