Client.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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::newConnectionInternal($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 quit() {
  113. $this->disconnect();
  114. }
  115. public function isConnected() {
  116. return $this->_connection->isConnected();
  117. }
  118. public function getConnection($id = null) {
  119. $connection = $this->_connection;
  120. if (!isset($id)) {
  121. return $connection;
  122. }
  123. $isCluster = Utils::isCluster($connection);
  124. return $isCluster ? $connection->getConnectionById($id) : $connection;
  125. }
  126. public function __call($method, $arguments) {
  127. $command = $this->_profile->createCommand($method, $arguments);
  128. return $this->_connection->executeCommand($command);
  129. }
  130. public function createCommand($method, $arguments = array()) {
  131. return $this->_profile->createCommand($method, $arguments);
  132. }
  133. public function executeCommand(ICommand $command) {
  134. return $this->_connection->executeCommand($command);
  135. }
  136. public function executeCommandOnShards(ICommand $command) {
  137. if (Utils::isCluster($this->_connection)) {
  138. $replies = array();
  139. foreach ($this->_connection as $connection) {
  140. $replies[] = $connection->executeCommand($command);
  141. }
  142. return $replies;
  143. }
  144. return array($this->_connection->executeCommand($command));
  145. }
  146. private function sharedInitializer($argv, $initializer) {
  147. $argc = count($argv);
  148. if ($argc === 0) {
  149. return $this->$initializer();
  150. }
  151. else if ($argc === 1) {
  152. list($arg0) = $argv;
  153. return is_array($arg0) ? $this->$initializer($arg0) : $this->$initializer(null, $arg0);
  154. }
  155. else if ($argc === 2) {
  156. list($arg0, $arg1) = $argv;
  157. return $this->$initializer($arg0, $arg1);
  158. }
  159. return $this->$initializer($this, $arguments);
  160. }
  161. public function pipeline(/* arguments */) {
  162. return $this->sharedInitializer(func_get_args(), 'initPipeline');
  163. }
  164. private function initPipeline(Array $options = null, $pipelineBlock = null) {
  165. $pipeline = null;
  166. if (isset($options)) {
  167. if (isset($options['safe']) && $options['safe'] == true) {
  168. $connection = $this->_connection;
  169. $pipeline = new CommandPipeline($this,
  170. Utils::isCluster($connection)
  171. ? new Pipeline\SafeClusterExecutor($connection)
  172. : new Pipeline\SafeExecutor($connection)
  173. );
  174. }
  175. else {
  176. $pipeline = new CommandPipeline($this);
  177. }
  178. }
  179. return $this->pipelineExecute(
  180. $pipeline ?: new CommandPipeline($this), $pipelineBlock
  181. );
  182. }
  183. private function pipelineExecute(CommandPipeline $pipeline, $block) {
  184. return $block !== null ? $pipeline->execute($block) : $pipeline;
  185. }
  186. public function multiExec(/* arguments */) {
  187. return $this->sharedInitializer(func_get_args(), 'initMultiExec');
  188. }
  189. private function initMultiExec(Array $options = null, $transBlock = null) {
  190. $multi = isset($options) ? new MultiExecContext($this, $options) : new MultiExecContext($this);
  191. return $transBlock !== null ? $multi->execute($transBlock) : $multi;
  192. }
  193. public function pubSubContext(Array $options = null) {
  194. return new PubSubContext($this, $options);
  195. }
  196. private static function ensureDefaultSchemes() {
  197. if (!isset(self::$_connectionSchemes)) {
  198. self::$_connectionSchemes = array(
  199. 'tcp' => '\Predis\Network\StreamConnection',
  200. 'unix' => '\Predis\Network\StreamConnection',
  201. );
  202. }
  203. }
  204. public static function defineConnection($scheme, $connectionClass) {
  205. self::ensureDefaultSchemes();
  206. $connectionReflection = new \ReflectionClass($connectionClass);
  207. if (!$connectionReflection->isSubclassOf('\Predis\Network\IConnectionSingle')) {
  208. throw new ClientException(
  209. "Cannot register '$connectionClass' as it is not a valid connection class"
  210. );
  211. }
  212. self::$_connectionSchemes[$scheme] = $connectionClass;
  213. }
  214. public static function getConnectionClass($scheme) {
  215. self::ensureDefaultSchemes();
  216. if (!isset(self::$_connectionSchemes[$scheme])) {
  217. throw new ClientException("Unknown connection scheme: $scheme");
  218. }
  219. return self::$_connectionSchemes[$scheme];
  220. }
  221. private static function newConnectionInternal(ConnectionParameters $parameters) {
  222. $connection = self::getConnectionClass($parameters->scheme);
  223. return new $connection($parameters);
  224. }
  225. public static function newConnection($parameters) {
  226. if (!$parameters instanceof ConnectionParameters) {
  227. $parameters = new ConnectionParameters($parameters);
  228. }
  229. return self::newConnectionInternal($parameters);
  230. }
  231. public static function newConnectionByScheme($scheme, $parameters = array()) {
  232. $connection = self::getConnectionClass($scheme);
  233. if (!$parameters instanceof ConnectionParameters) {
  234. $parameters = new ConnectionParameters($parameters);
  235. }
  236. return self::newConnection($parameters);
  237. }
  238. }