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