Client.php 9.5 KB

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