Client.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace Predis;
  3. use Predis\Commands\ICommand;
  4. use Predis\Network\IConnection;
  5. use Predis\Network\IConnectionSingle;
  6. use Predis\Network\ConnectionCluster;
  7. use Predis\Profiles\ServerProfile;
  8. use Predis\Profiles\IServerProfile;
  9. class Client {
  10. const VERSION = '0.7.0-dev';
  11. private static $_connectionSchemes;
  12. private $_options, $_profile, $_connection;
  13. public function __construct($parameters = null, $options = null) {
  14. $this->_options = $this->filterOptions($options ?: new ClientOptions());
  15. $this->_profile = $this->_options->profile;
  16. $this->_connection = $this->initializeConnection($parameters);
  17. }
  18. private function filterOptions($options) {
  19. if ($options instanceof ClientOptions) {
  20. return $options;
  21. }
  22. if (is_array($options)) {
  23. return new ClientOptions($options);
  24. }
  25. if ($options instanceof IServerProfile) {
  26. return new ClientOptions(array('profile' => $options));
  27. }
  28. if (is_string($options)) {
  29. $profile = ServerProfile::get($options);
  30. return new ClientOptions(array('profile' => $profile));
  31. }
  32. throw new \InvalidArgumentException("Invalid type for client options");
  33. }
  34. private function initializeConnection($parameters = array()) {
  35. if ($parameters === null) {
  36. return $this->createConnection(array());
  37. }
  38. if ($parameters instanceof IConnection) {
  39. return $parameters;
  40. }
  41. if (is_array($parameters) && isset($parameters[0])) {
  42. $cluster = new ConnectionCluster($this->_options->key_distribution);
  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. private function createConnection($parameters) {
  53. $connection = self::newConnection($parameters);
  54. $this->pushInitCommands($connection);
  55. $callback = $this->_options->on_connection_initialized;
  56. if ($callback !== null) {
  57. $callback($this, $connection);
  58. }
  59. return $connection;
  60. }
  61. private function pushInitCommands(IConnectionSingle $connection) {
  62. $params = $connection->getParameters();
  63. if (isset($params->password)) {
  64. $connection->pushInitCommand($this->createCommand(
  65. 'auth', array($params->password)
  66. ));
  67. }
  68. if (isset($params->database)) {
  69. $connection->pushInitCommand($this->createCommand(
  70. 'select', array($params->database)
  71. ));
  72. }
  73. }
  74. public function getProfile() {
  75. return $this->_profile;
  76. }
  77. public function getOptions() {
  78. return $this->_options;
  79. }
  80. public function getClientFor($connectionAlias) {
  81. if (!Utils::isCluster($this->_connection)) {
  82. throw new ClientException(
  83. 'This method is supported only when the client is connected to a cluster of connections'
  84. );
  85. }
  86. $connection = $this->_connection->getConnectionById($connectionAlias);
  87. if ($connection === null) {
  88. throw new \InvalidArgumentException(
  89. "Invalid connection alias: '$connectionAlias'"
  90. );
  91. }
  92. return new Client($connection, $this->_options);
  93. }
  94. public function connect() {
  95. $this->_connection->connect();
  96. }
  97. public function disconnect() {
  98. $this->_connection->disconnect();
  99. }
  100. public function quit() {
  101. $this->disconnect();
  102. }
  103. public function isConnected() {
  104. return $this->_connection->isConnected();
  105. }
  106. public function getConnection($id = null) {
  107. if ($id === null) {
  108. return $this->_connection;
  109. }
  110. $connection = $this->_connection;
  111. $isCluster = Utils::isCluster($connection);
  112. return $isCluster ? $connection->getConnectionById($id) : $connection;
  113. }
  114. public function __call($method, $arguments) {
  115. $command = $this->_profile->createCommand($method, $arguments);
  116. return $this->_connection->executeCommand($command);
  117. }
  118. public function createCommand($method, $arguments = array()) {
  119. return $this->_profile->createCommand($method, $arguments);
  120. }
  121. public function executeCommand(ICommand $command) {
  122. return $this->_connection->executeCommand($command);
  123. }
  124. public function executeCommandOnShards(ICommand $command) {
  125. if (Utils::isCluster($this->_connection)) {
  126. $replies = array();
  127. foreach ($this->_connection as $connection) {
  128. $replies[] = $connection->executeCommand($command);
  129. }
  130. return $replies;
  131. }
  132. return array($this->_connection->executeCommand($command));
  133. }
  134. private function sharedInitializer($argv, $initializer) {
  135. $argc = count($argv);
  136. if ($argc === 0) {
  137. return $this->$initializer();
  138. }
  139. else if ($argc === 1) {
  140. list($arg0) = $argv;
  141. return is_array($arg0) ? $this->$initializer($arg0) : $this->$initializer(null, $arg0);
  142. }
  143. else if ($argc === 2) {
  144. list($arg0, $arg1) = $argv;
  145. return $this->$initializer($arg0, $arg1);
  146. }
  147. return $this->$initializer($this, $arguments);
  148. }
  149. public function pipeline(/* arguments */) {
  150. return $this->sharedInitializer(func_get_args(), 'initPipeline');
  151. }
  152. private function initPipeline(Array $options = null, $pipelineBlock = null) {
  153. $pipeline = null;
  154. if (isset($options)) {
  155. if (isset($options['safe']) && $options['safe'] == true) {
  156. $connection = $this->_connection;
  157. $pipeline = new CommandPipeline($this,
  158. Utils::isCluster($connection)
  159. ? new Pipeline\SafeClusterExecutor($connection)
  160. : new Pipeline\SafeExecutor($connection)
  161. );
  162. }
  163. else {
  164. $pipeline = new CommandPipeline($this);
  165. }
  166. }
  167. return $this->pipelineExecute(
  168. $pipeline ?: new CommandPipeline($this), $pipelineBlock
  169. );
  170. }
  171. private function pipelineExecute(CommandPipeline $pipeline, $block) {
  172. return $block !== null ? $pipeline->execute($block) : $pipeline;
  173. }
  174. public function multiExec(/* arguments */) {
  175. return $this->sharedInitializer(func_get_args(), 'initMultiExec');
  176. }
  177. private function initMultiExec(Array $options = null, $transBlock = null) {
  178. $multi = isset($options) ? new MultiExecContext($this, $options) : new MultiExecContext($this);
  179. return $transBlock !== null ? $multi->execute($transBlock) : $multi;
  180. }
  181. public function pubSubContext(Array $options = null) {
  182. return new PubSubContext($this, $options);
  183. }
  184. private static function ensureDefaultSchemes() {
  185. if (!isset(self::$_connectionSchemes)) {
  186. self::$_connectionSchemes = array(
  187. 'tcp' => '\Predis\Network\StreamConnection',
  188. 'unix' => '\Predis\Network\StreamConnection',
  189. );
  190. }
  191. }
  192. public static function defineConnection($scheme, $connectionClass) {
  193. self::ensureDefaultSchemes();
  194. $connectionReflection = new \ReflectionClass($connectionClass);
  195. if (!$connectionReflection->isSubclassOf('\Predis\Network\IConnectionSingle')) {
  196. throw new ClientException(
  197. "Cannot register '$connectionClass' as it is not a valid connection class"
  198. );
  199. }
  200. self::$_connectionSchemes[$scheme] = $connectionClass;
  201. }
  202. public static function getConnectionClass($scheme) {
  203. self::ensureDefaultSchemes();
  204. if (!isset(self::$_connectionSchemes[$scheme])) {
  205. throw new ClientException("Unknown connection scheme: $scheme");
  206. }
  207. return self::$_connectionSchemes[$scheme];
  208. }
  209. private static function newConnectionInternal(ConnectionParameters $parameters) {
  210. $connection = self::getConnectionClass($parameters->scheme);
  211. return new $connection($parameters);
  212. }
  213. public static function newConnection($parameters) {
  214. if (!$parameters instanceof ConnectionParameters) {
  215. $parameters = new ConnectionParameters($parameters);
  216. }
  217. return self::newConnectionInternal($parameters);
  218. }
  219. public static function newConnectionByScheme($scheme, $parameters = array()) {
  220. if ($parameters instanceof ConnectionParameters) {
  221. $parameters = $parameters->toArray();
  222. }
  223. if (is_array($parameters)) {
  224. $parameters['scheme'] = $scheme;
  225. return self::newConnection($parameters);
  226. }
  227. throw new \InvalidArgumentException("Invalid type for connection parameters");
  228. }
  229. }