Client.php 9.5 KB

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