Client.php 7.1 KB

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