Client.php 9.3 KB

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