Client.php 9.1 KB

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