Client.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis;
  11. use Predis\Commands\ICommand;
  12. use Predis\Network\IConnection;
  13. use Predis\Network\IConnectionSingle;
  14. use Predis\Profiles\IServerProfile;
  15. use Predis\Profiles\ServerProfile;
  16. use Predis\Pipeline\PipelineContext;
  17. use Predis\Transaction\MultiExecContext;
  18. class Client
  19. {
  20. const VERSION = '0.7.0-dev';
  21. private $_options;
  22. private $_profile;
  23. private $_connection;
  24. private $_connectionFactory;
  25. public function __construct($parameters = null, $options = null)
  26. {
  27. $options = $this->filterOptions($options);
  28. $profile = $options->profile;
  29. if (isset($options->prefix)) {
  30. $profile->setProcessor($options->prefix);
  31. }
  32. $this->_options = $options;
  33. $this->_profile = $profile;
  34. $this->_connectionFactory = $options->connections;
  35. $this->_connection = $this->initializeConnection($parameters);
  36. }
  37. private function filterOptions($options)
  38. {
  39. if ($options === null) {
  40. return new ClientOptions();
  41. }
  42. if (is_array($options)) {
  43. return new ClientOptions($options);
  44. }
  45. if ($options instanceof ClientOptions) {
  46. return $options;
  47. }
  48. if ($options instanceof IServerProfile) {
  49. return new ClientOptions(array('profile' => $options));
  50. }
  51. if (is_string($options)) {
  52. return new ClientOptions(array('profile' => ServerProfile::get($options)));
  53. }
  54. throw new \InvalidArgumentException("Invalid type for client options");
  55. }
  56. private function initializeConnection($parameters)
  57. {
  58. if ($parameters === null) {
  59. return $this->createConnection(new ConnectionParameters());
  60. }
  61. if (is_array($parameters)) {
  62. if (isset($parameters[0])) {
  63. $cluster = $this->_options->cluster;
  64. foreach ($parameters as $node) {
  65. $connection = $node instanceof IConnectionSingle ? $node : $this->createConnection($node);
  66. $cluster->add($connection);
  67. }
  68. return $cluster;
  69. }
  70. return $this->createConnection($parameters);
  71. }
  72. if ($parameters instanceof IConnection) {
  73. return $parameters;
  74. }
  75. return $this->createConnection($parameters);
  76. }
  77. protected function createConnection($parameters)
  78. {
  79. $connection = $this->_connectionFactory->create($parameters);
  80. $parameters = $connection->getParameters();
  81. if (isset($parameters->password)) {
  82. $command = $this->createCommand('auth', array($parameters->password));
  83. $connection->pushInitCommand($command);
  84. }
  85. if (isset($parameters->database)) {
  86. $command = $this->createCommand('select', array($parameters->database));
  87. $connection->pushInitCommand($command);
  88. }
  89. return $connection;
  90. }
  91. public function getProfile()
  92. {
  93. return $this->_profile;
  94. }
  95. public function getOptions()
  96. {
  97. return $this->_options;
  98. }
  99. public function getConnectionFactory()
  100. {
  101. return $this->_connectionFactory;
  102. }
  103. public function getClientFor($connectionAlias)
  104. {
  105. if (($connection = $this->getConnection($connectionAlias)) === null) {
  106. throw new \InvalidArgumentException("Invalid connection alias: '$connectionAlias'");
  107. }
  108. return new Client($connection, $this->_options);
  109. }
  110. public function connect()
  111. {
  112. $this->_connection->connect();
  113. }
  114. public function disconnect()
  115. {
  116. $this->_connection->disconnect();
  117. }
  118. public function quit()
  119. {
  120. $this->disconnect();
  121. }
  122. public function isConnected()
  123. {
  124. return $this->_connection->isConnected();
  125. }
  126. public function getConnection($id = null)
  127. {
  128. if (isset($id)) {
  129. if (!Helpers::isCluster($this->_connection)) {
  130. throw new ClientException(
  131. 'Retrieving connections by alias is supported only with clustered connections'
  132. );
  133. }
  134. return $this->_connection->getConnectionById($id);
  135. }
  136. return $this->_connection;
  137. }
  138. public function __call($method, $arguments)
  139. {
  140. $command = $this->_profile->createCommand($method, $arguments);
  141. return $this->_connection->executeCommand($command);
  142. }
  143. public function createCommand($method, $arguments = array())
  144. {
  145. return $this->_profile->createCommand($method, $arguments);
  146. }
  147. public function executeCommand(ICommand $command)
  148. {
  149. return $this->_connection->executeCommand($command);
  150. }
  151. public function executeCommandOnShards(ICommand $command)
  152. {
  153. if (Helpers::isCluster($this->_connection)) {
  154. $replies = array();
  155. foreach ($this->_connection as $connection) {
  156. $replies[] = $connection->executeCommand($command);
  157. }
  158. return $replies;
  159. }
  160. return array($this->_connection->executeCommand($command));
  161. }
  162. private function sharedInitializer($argv, $initializer)
  163. {
  164. switch (count($argv)) {
  165. case 0:
  166. return $this->$initializer();
  167. case 1:
  168. list($arg0) = $argv;
  169. return is_array($arg0) ? $this->$initializer($arg0) : $this->$initializer(null, $arg0);
  170. case 2:
  171. list($arg0, $arg1) = $argv;
  172. return $this->$initializer($arg0, $arg1);
  173. default:
  174. return $this->$initializer($this, $argv);
  175. }
  176. }
  177. public function pipeline(/* arguments */)
  178. {
  179. return $this->sharedInitializer(func_get_args(), 'initPipeline');
  180. }
  181. protected function initPipeline(Array $options = null, $pipelineBlock = null)
  182. {
  183. $pipeline = new PipelineContext($this, $options);
  184. return $this->pipelineExecute($pipeline, $pipelineBlock);
  185. }
  186. private function pipelineExecute(PipelineContext $pipeline, $block)
  187. {
  188. return $block !== null ? $pipeline->execute($block) : $pipeline;
  189. }
  190. public function multiExec(/* arguments */)
  191. {
  192. return $this->sharedInitializer(func_get_args(), 'initMultiExec');
  193. }
  194. protected function initMultiExec(Array $options = null, $block = null)
  195. {
  196. $transaction = new MultiExecContext($this, $options ?: array());
  197. return isset($block) ? $transaction->execute($block) : $transaction;
  198. }
  199. public function pubSub(/* arguments */)
  200. {
  201. return $this->sharedInitializer(func_get_args(), 'initPubSub');
  202. }
  203. protected function initPubSub(Array $options = null, $block = null)
  204. {
  205. $pubsub = new PubSubContext($this, $options);
  206. if (!isset($block)) {
  207. return $pubsub;
  208. }
  209. foreach ($pubsub as $message) {
  210. if ($block($pubsub, $message) === false) {
  211. $pubsub->closeContext();
  212. }
  213. }
  214. }
  215. public function monitor()
  216. {
  217. return new MonitorContext($this);
  218. }
  219. }