Client.php 7.0 KB

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