Client.php 7.0 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. $this->_profile = $options->profile;
  15. $this->_connectionFactory = $options->connections;
  16. $this->_connection = $this->initializeConnection($parameters);
  17. }
  18. private function filterOptions($options) {
  19. if ($options === null) {
  20. return new ClientOptions();
  21. }
  22. if ($options instanceof ClientOptions) {
  23. return $options;
  24. }
  25. if (is_array($options)) {
  26. return new ClientOptions($options);
  27. }
  28. if ($options instanceof IServerProfile) {
  29. return new ClientOptions(array('profile' => $options));
  30. }
  31. if (is_string($options)) {
  32. return new ClientOptions(array('profile' => ServerProfile::get($options)));
  33. }
  34. throw new \InvalidArgumentException("Invalid type for client options");
  35. }
  36. private function initializeConnection($parameters) {
  37. if ($parameters === null) {
  38. return $this->createConnection(new ConnectionParameters());
  39. }
  40. if (is_array($parameters)) {
  41. if (isset($parameters[0])) {
  42. $cluster = $this->_options->cluster;
  43. foreach ($parameters as $single) {
  44. $cluster->add($single instanceof IConnectionSingle
  45. ? $single : $this->createConnection($single)
  46. );
  47. }
  48. return $cluster;
  49. }
  50. return $this->createConnection($parameters);
  51. }
  52. if ($parameters instanceof IConnection) {
  53. return $parameters;
  54. }
  55. return $this->createConnection($parameters);
  56. }
  57. private function createConnection($parameters) {
  58. $connection = $this->_connectionFactory->create($parameters);
  59. $this->pushInitCommands($connection);
  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 getConnectionFactory() {
  82. return $this->_connectionFactory;
  83. }
  84. public function getClientFor($connectionAlias) {
  85. if (($connection = $this->getConnection($connectionAlias)) === null) {
  86. throw new \InvalidArgumentException(
  87. "Invalid connection alias: '$connectionAlias'"
  88. );
  89. }
  90. return new Client($connection, $this->_options);
  91. }
  92. public function connect() {
  93. $this->_connection->connect();
  94. }
  95. public function disconnect() {
  96. $this->_connection->disconnect();
  97. }
  98. public function quit() {
  99. $this->disconnect();
  100. }
  101. public function isConnected() {
  102. return $this->_connection->isConnected();
  103. }
  104. public function getConnection($id = null) {
  105. if (isset($id)) {
  106. if (!Helpers::isCluster($this->_connection)) {
  107. throw new ClientException(
  108. 'Retrieving connections by alias is supported '.
  109. 'only with clustered connections'
  110. );
  111. }
  112. return $this->_connection->getConnectionById($id);
  113. }
  114. return $this->_connection;
  115. }
  116. public function __call($method, $arguments) {
  117. $command = $this->_profile->createCommand($method, $arguments);
  118. return $this->_connection->executeCommand($command);
  119. }
  120. public function createCommand($method, $arguments = array()) {
  121. return $this->_profile->createCommand($method, $arguments);
  122. }
  123. public function executeCommand(ICommand $command) {
  124. return $this->_connection->executeCommand($command);
  125. }
  126. public function executeCommandOnShards(ICommand $command) {
  127. if (Helpers::isCluster($this->_connection)) {
  128. $replies = array();
  129. foreach ($this->_connection as $connection) {
  130. $replies[] = $connection->executeCommand($command);
  131. }
  132. return $replies;
  133. }
  134. return array($this->_connection->executeCommand($command));
  135. }
  136. private function sharedInitializer($argv, $initializer) {
  137. switch (count($argv)) {
  138. case 0:
  139. return $this->$initializer();
  140. case 1:
  141. list($arg0) = $argv;
  142. return is_array($arg0)
  143. ? $this->$initializer($arg0)
  144. : $this->$initializer(null, $arg0);
  145. case 2:
  146. list($arg0, $arg1) = $argv;
  147. return $this->$initializer($arg0, $arg1);
  148. default:
  149. return $this->$initializer($this, $argv);
  150. }
  151. }
  152. public function pipeline(/* arguments */) {
  153. return $this->sharedInitializer(func_get_args(), 'initPipeline');
  154. }
  155. private function initPipeline(Array $options = null, $pipelineBlock = null) {
  156. $pipeline = null;
  157. if (isset($options)) {
  158. if (isset($options['safe']) && $options['safe'] == true) {
  159. $connection = $this->_connection;
  160. $pipeline = new PipelineContext($this,
  161. Helpers::isCluster($connection)
  162. ? new Pipeline\SafeClusterExecutor()
  163. : new Pipeline\SafeExecutor()
  164. );
  165. }
  166. else {
  167. $pipeline = new PipelineContext($this);
  168. }
  169. }
  170. return $this->pipelineExecute(
  171. $pipeline ?: new PipelineContext($this), $pipelineBlock
  172. );
  173. }
  174. private function pipelineExecute(PipelineContext $pipeline, $block) {
  175. return $block !== null ? $pipeline->execute($block) : $pipeline;
  176. }
  177. public function multiExec(/* arguments */) {
  178. return $this->sharedInitializer(func_get_args(), 'initMultiExec');
  179. }
  180. private function initMultiExec(Array $options = null, $transBlock = null) {
  181. $multi = isset($options) ? new MultiExecContext($this, $options) : new MultiExecContext($this);
  182. return $transBlock !== null ? $multi->execute($transBlock) : $multi;
  183. }
  184. public function pubSubContext(Array $options = null) {
  185. return new PubSubContext($this, $options);
  186. }
  187. public function monitor() {
  188. return new MonitorContext($this);
  189. }
  190. }