Connection.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Exception\InvalidException;
  4. use Elastica\Transport\AbstractTransport;
  5. /**
  6. * Elastica connection instance to an elasticasearch node.
  7. *
  8. * @author Nicolas Ruflin <spam@ruflin.com>
  9. */
  10. class Connection extends Param
  11. {
  12. /**
  13. * Default elastic search port.
  14. */
  15. const DEFAULT_PORT = 9200;
  16. /**
  17. * Default host.
  18. */
  19. const DEFAULT_HOST = 'localhost';
  20. /**
  21. * Default transport.
  22. *
  23. * @var string
  24. */
  25. const DEFAULT_TRANSPORT = 'Http';
  26. /**
  27. * Default compression.
  28. *
  29. * @var string
  30. */
  31. const DEFAULT_COMPRESSION = false;
  32. /**
  33. * Number of seconds after a timeout occurs for every request
  34. * If using indexing of file large value necessary.
  35. */
  36. const TIMEOUT = 300;
  37. /**
  38. * Number of seconds after a connection timeout occurs for every request during the connection phase.
  39. *
  40. * @see Connection::setConnectTimeout();
  41. */
  42. const CONNECT_TIMEOUT = 0;
  43. /**
  44. * Creates a new connection object. A connection is enabled by default.
  45. *
  46. * @param array $params OPTIONAL Connection params: host, port, transport, timeout. All are optional
  47. */
  48. public function __construct(array $params = [])
  49. {
  50. $this->setParams($params);
  51. $this->setEnabled(true);
  52. // Set empty config param if not exists
  53. if (!$this->hasParam('config')) {
  54. $this->setParam('config', []);
  55. }
  56. }
  57. /**
  58. * @return int Server port
  59. */
  60. public function getPort()
  61. {
  62. return $this->hasParam('port') ? $this->getParam('port') : self::DEFAULT_PORT;
  63. }
  64. /**
  65. * @param int $port
  66. *
  67. * @return $this
  68. */
  69. public function setPort($port)
  70. {
  71. return $this->setParam('port', (int) $port);
  72. }
  73. /**
  74. * @return string Host
  75. */
  76. public function getHost()
  77. {
  78. return $this->hasParam('host') ? $this->getParam('host') : self::DEFAULT_HOST;
  79. }
  80. /**
  81. * @param string $host
  82. *
  83. * @return $this
  84. */
  85. public function setHost($host)
  86. {
  87. return $this->setParam('host', $host);
  88. }
  89. /**
  90. * @return string|null Host
  91. */
  92. public function getProxy()
  93. {
  94. return $this->hasParam('proxy') ? $this->getParam('proxy') : null;
  95. }
  96. /**
  97. * Set proxy for http connections. Null is for environmental proxy,
  98. * empty string to disable proxy and proxy string to set actual http proxy.
  99. *
  100. * @see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTPROXY
  101. *
  102. * @param string|null $proxy
  103. *
  104. * @return $this
  105. */
  106. public function setProxy($proxy)
  107. {
  108. return $this->setParam('proxy', $proxy);
  109. }
  110. /**
  111. * @return string|array
  112. */
  113. public function getTransport()
  114. {
  115. return $this->hasParam('transport') ? $this->getParam('transport') : self::DEFAULT_TRANSPORT;
  116. }
  117. /**
  118. * @param string|array $transport
  119. *
  120. * @return $this
  121. */
  122. public function setTransport($transport)
  123. {
  124. return $this->setParam('transport', $transport);
  125. }
  126. /**
  127. * @return bool
  128. */
  129. public function hasCompression()
  130. {
  131. return (bool) $this->hasParam('compression') ? $this->getParam('compression') : self::DEFAULT_COMPRESSION;
  132. }
  133. /**
  134. * @param bool $compression
  135. *
  136. * @return $this
  137. */
  138. public function setCompression($compression = null)
  139. {
  140. return $this->setParam('compression', $compression);
  141. }
  142. /**
  143. * @return string
  144. */
  145. public function getPath()
  146. {
  147. return $this->hasParam('path') ? $this->getParam('path') : '';
  148. }
  149. /**
  150. * @param string $path
  151. *
  152. * @return $this
  153. */
  154. public function setPath($path)
  155. {
  156. return $this->setParam('path', $path);
  157. }
  158. /**
  159. * @param int $timeout Timeout in seconds
  160. *
  161. * @return $this
  162. */
  163. public function setTimeout($timeout)
  164. {
  165. return $this->setParam('timeout', $timeout);
  166. }
  167. /**
  168. * @return int Connection timeout in seconds
  169. */
  170. public function getTimeout()
  171. {
  172. return (int) $this->hasParam('timeout') ? $this->getParam('timeout') : self::TIMEOUT;
  173. }
  174. /**
  175. * Number of seconds after a connection timeout occurs for every request during the connection phase.
  176. * Use a small value if you need a fast fail in case of dead, unresponsive or unreachable servers (~5 sec).
  177. *
  178. * Set to zero to switch to the default built-in connection timeout (300 seconds in curl).
  179. *
  180. * @see http://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html
  181. *
  182. * @param int $timeout Connect timeout in seconds
  183. *
  184. * @return $this
  185. */
  186. public function setConnectTimeout($timeout)
  187. {
  188. return $this->setParam('connectTimeout', $timeout);
  189. }
  190. /**
  191. * @return int Connection timeout in seconds
  192. */
  193. public function getConnectTimeout()
  194. {
  195. return (int) $this->hasParam('connectTimeout') ? $this->getParam('connectTimeout') : self::CONNECT_TIMEOUT;
  196. }
  197. /**
  198. * Enables a connection.
  199. *
  200. * @param bool $enabled OPTIONAL (default = true)
  201. *
  202. * @return $this
  203. */
  204. public function setEnabled($enabled = true)
  205. {
  206. return $this->setParam('enabled', $enabled);
  207. }
  208. /**
  209. * @return bool True if enabled
  210. */
  211. public function isEnabled()
  212. {
  213. return (bool) $this->getParam('enabled');
  214. }
  215. /**
  216. * Returns an instance of the transport type.
  217. *
  218. * @throws \Elastica\Exception\InvalidException If invalid transport type
  219. *
  220. * @return \Elastica\Transport\AbstractTransport Transport object
  221. */
  222. public function getTransportObject()
  223. {
  224. $transport = $this->getTransport();
  225. return AbstractTransport::create($transport, $this);
  226. }
  227. /**
  228. * @return bool Returns true if connection is persistent. True by default
  229. */
  230. public function isPersistent()
  231. {
  232. return (bool) $this->hasParam('persistent') ? $this->getParam('persistent') : true;
  233. }
  234. /**
  235. * @param array $config
  236. *
  237. * @return $this
  238. */
  239. public function setConfig(array $config)
  240. {
  241. return $this->setParam('config', $config);
  242. }
  243. /**
  244. * @param string $key
  245. * @param mixed $value
  246. *
  247. * @return $this
  248. */
  249. public function addConfig($key, $value)
  250. {
  251. $this->_params['config'][$key] = $value;
  252. return $this;
  253. }
  254. /**
  255. * @param string $key
  256. *
  257. * @return bool
  258. */
  259. public function hasConfig($key)
  260. {
  261. $config = $this->getConfig();
  262. return isset($config[$key]);
  263. }
  264. /**
  265. * Returns a specific config key or the whole
  266. * config array if not set.
  267. *
  268. * @param string $key Config key
  269. *
  270. * @throws \Elastica\Exception\InvalidException
  271. *
  272. * @return array|string Config value
  273. */
  274. public function getConfig($key = '')
  275. {
  276. $config = $this->getParam('config');
  277. if (empty($key)) {
  278. return $config;
  279. }
  280. if (!array_key_exists($key, $config)) {
  281. throw new InvalidException('Config key is not set: '.$key);
  282. }
  283. return $config[$key];
  284. }
  285. /**
  286. * @param \Elastica\Connection|array $params Params to create a connection
  287. *
  288. * @throws Exception\InvalidException
  289. *
  290. * @return self
  291. */
  292. public static function create($params = [])
  293. {
  294. if (is_array($params)) {
  295. return new self($params);
  296. }
  297. if ($params instanceof self) {
  298. return $params;
  299. }
  300. throw new InvalidException('Invalid data type');
  301. }
  302. /**
  303. * @return string User
  304. */
  305. public function getUsername()
  306. {
  307. return $this->hasParam('username') ? $this->getParam('username') : null;
  308. }
  309. /**
  310. * @return string Password
  311. */
  312. public function getPassword()
  313. {
  314. return $this->hasParam('password') ? $this->getParam('password') : null;
  315. }
  316. }