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\Profiles\IServerProfile;
  7. use Predis\Profiles\ServerProfile;
  8. class Client {
  9. const VERSION = '0.7.0-dev';
  10. private $_options, $_connectionFactory, $_profile, $_connection;
  11. public function __construct($parameters = null, $options = null) {
  12. $options = $this->filterOptions($options);
  13. $this->_options = $options;
  14. $profile = $options->profile;
  15. if (isset($options->prefix)) {
  16. $profile->setProcessor($options->prefix);
  17. }
  18. $this->_profile = $profile;
  19. $this->_connectionFactory = $options->connections;
  20. $this->_connection = $this->initializeConnection($parameters);
  21. }
  22. private function filterOptions($options) {
  23. if ($options === null) {
  24. return new ClientOptions();
  25. }
  26. if (is_array($options)) {
  27. return new ClientOptions($options);
  28. }
  29. if ($options instanceof ClientOptions) {
  30. return $options;
  31. }
  32. if ($options instanceof IServerProfile) {
  33. return new ClientOptions(array('profile' => $options));
  34. }
  35. if (is_string($options)) {
  36. return new ClientOptions(array('profile' => ServerProfile::get($options)));
  37. }
  38. throw new \InvalidArgumentException("Invalid type for client options");
  39. }
  40. private function initializeConnection($parameters) {
  41. if ($parameters === null) {
  42. return $this->createConnection(new ConnectionParameters());
  43. }
  44. if (is_array($parameters)) {
  45. if (isset($parameters[0])) {
  46. $cluster = $this->_options->cluster;
  47. foreach ($parameters as $single) {
  48. $cluster->add($single instanceof IConnectionSingle
  49. ? $single : $this->createConnection($single)
  50. );
  51. }
  52. return $cluster;
  53. }
  54. return $this->createConnection($parameters);
  55. }
  56. if ($parameters instanceof IConnection) {
  57. return $parameters;
  58. }
  59. return $this->createConnection($parameters);
  60. }
  61. protected function createConnection($parameters) {
  62. $connection = $this->_connectionFactory->create($parameters);
  63. $parameters = $connection->getParameters();
  64. if (isset($parameters->password)) {
  65. $connection->pushInitCommand(
  66. $this->createCommand('auth', array($parameters->password))
  67. );
  68. }
  69. if (isset($parameters->database)) {
  70. $connection->pushInitCommand(
  71. $this->createCommand('select', array($parameters->database))
  72. );
  73. }
  74. return $connection;
  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. }