Client.php 7.2 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. 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. private function createConnection($parameters) {
  62. $connection = $this->_connectionFactory->create($parameters);
  63. $this->pushInitCommands($connection);
  64. return $connection;
  65. }
  66. private function pushInitCommands(IConnectionSingle $connection) {
  67. $params = $connection->getParameters();
  68. if (isset($params->password)) {
  69. $connection->pushInitCommand($this->createCommand(
  70. 'auth', array($params->password)
  71. ));
  72. }
  73. if (isset($params->database)) {
  74. $connection->pushInitCommand($this->createCommand(
  75. 'select', array($params->database)
  76. ));
  77. }
  78. }
  79. public function getProfile() {
  80. return $this->_profile;
  81. }
  82. public function getOptions() {
  83. return $this->_options;
  84. }
  85. public function getConnectionFactory() {
  86. return $this->_connectionFactory;
  87. }
  88. public function getClientFor($connectionAlias) {
  89. if (($connection = $this->getConnection($connectionAlias)) === null) {
  90. throw new \InvalidArgumentException(
  91. "Invalid connection alias: '$connectionAlias'"
  92. );
  93. }
  94. return new Client($connection, $this->_options);
  95. }
  96. public function connect() {
  97. $this->_connection->connect();
  98. }
  99. public function disconnect() {
  100. $this->_connection->disconnect();
  101. }
  102. public function quit() {
  103. $this->disconnect();
  104. }
  105. public function isConnected() {
  106. return $this->_connection->isConnected();
  107. }
  108. public function getConnection($id = null) {
  109. if (isset($id)) {
  110. if (!Helpers::isCluster($this->_connection)) {
  111. throw new ClientException(
  112. 'Retrieving connections by alias is supported '.
  113. 'only with clustered connections'
  114. );
  115. }
  116. return $this->_connection->getConnectionById($id);
  117. }
  118. return $this->_connection;
  119. }
  120. public function __call($method, $arguments) {
  121. $command = $this->_profile->createCommand($method, $arguments);
  122. return $this->_connection->executeCommand($command);
  123. }
  124. public function createCommand($method, $arguments = array()) {
  125. return $this->_profile->createCommand($method, $arguments);
  126. }
  127. public function executeCommand(ICommand $command) {
  128. return $this->_connection->executeCommand($command);
  129. }
  130. public function executeCommandOnShards(ICommand $command) {
  131. if (Helpers::isCluster($this->_connection)) {
  132. $replies = array();
  133. foreach ($this->_connection as $connection) {
  134. $replies[] = $connection->executeCommand($command);
  135. }
  136. return $replies;
  137. }
  138. return array($this->_connection->executeCommand($command));
  139. }
  140. private function sharedInitializer($argv, $initializer) {
  141. switch (count($argv)) {
  142. case 0:
  143. return $this->$initializer();
  144. case 1:
  145. list($arg0) = $argv;
  146. return is_array($arg0)
  147. ? $this->$initializer($arg0)
  148. : $this->$initializer(null, $arg0);
  149. case 2:
  150. list($arg0, $arg1) = $argv;
  151. return $this->$initializer($arg0, $arg1);
  152. default:
  153. return $this->$initializer($this, $argv);
  154. }
  155. }
  156. public function pipeline(/* arguments */) {
  157. return $this->sharedInitializer(func_get_args(), 'initPipeline');
  158. }
  159. private function initPipeline(Array $options = null, $pipelineBlock = null) {
  160. $pipeline = null;
  161. if (isset($options)) {
  162. if (isset($options['safe']) && $options['safe'] == true) {
  163. $connection = $this->_connection;
  164. $pipeline = new PipelineContext($this,
  165. Helpers::isCluster($connection)
  166. ? new Pipeline\SafeClusterExecutor()
  167. : new Pipeline\SafeExecutor()
  168. );
  169. }
  170. else {
  171. $pipeline = new PipelineContext($this);
  172. }
  173. }
  174. return $this->pipelineExecute(
  175. $pipeline ?: new PipelineContext($this), $pipelineBlock
  176. );
  177. }
  178. private function pipelineExecute(PipelineContext $pipeline, $block) {
  179. return $block !== null ? $pipeline->execute($block) : $pipeline;
  180. }
  181. public function multiExec(/* arguments */) {
  182. return $this->sharedInitializer(func_get_args(), 'initMultiExec');
  183. }
  184. private function initMultiExec(Array $options = null, $transBlock = null) {
  185. $multi = isset($options) ? new MultiExecContext($this, $options) : new MultiExecContext($this);
  186. return $transBlock !== null ? $multi->execute($transBlock) : $multi;
  187. }
  188. public function pubSubContext(Array $options = null) {
  189. return new PubSubContext($this, $options);
  190. }
  191. public function monitor() {
  192. return new MonitorContext($this);
  193. }
  194. }