| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- <?php
- namespace Elastica;
- use Elastica\Exception\InvalidException;
- use Elastica\Transport\AbstractTransport;
- /**
- * Elastica connection instance to an elasticasearch node.
- *
- * @author Nicolas Ruflin <spam@ruflin.com>
- */
- class Connection extends Param
- {
- /**
- * Default elastic search port.
- */
- const DEFAULT_PORT = 9200;
- /**
- * Default host.
- */
- const DEFAULT_HOST = 'localhost';
- /**
- * Default transport.
- *
- * @var string
- */
- const DEFAULT_TRANSPORT = 'Http';
- /**
- * Default compression.
- *
- * @var string
- */
- const DEFAULT_COMPRESSION = false;
- /**
- * Number of seconds after a timeout occurs for every request
- * If using indexing of file large value necessary.
- */
- const TIMEOUT = 300;
- /**
- * Number of seconds after a connection timeout occurs for every request during the connection phase.
- *
- * @see Connection::setConnectTimeout();
- */
- const CONNECT_TIMEOUT = 0;
- /**
- * Creates a new connection object. A connection is enabled by default.
- *
- * @param array $params OPTIONAL Connection params: host, port, transport, timeout. All are optional
- */
- public function __construct(array $params = [])
- {
- $this->setParams($params);
- $this->setEnabled(true);
- // Set empty config param if not exists
- if (!$this->hasParam('config')) {
- $this->setParam('config', []);
- }
- }
- /**
- * @return int Server port
- */
- public function getPort()
- {
- return $this->hasParam('port') ? $this->getParam('port') : self::DEFAULT_PORT;
- }
- /**
- * @param int $port
- *
- * @return $this
- */
- public function setPort($port)
- {
- return $this->setParam('port', (int) $port);
- }
- /**
- * @return string Host
- */
- public function getHost()
- {
- return $this->hasParam('host') ? $this->getParam('host') : self::DEFAULT_HOST;
- }
- /**
- * @param string $host
- *
- * @return $this
- */
- public function setHost($host)
- {
- return $this->setParam('host', $host);
- }
- /**
- * @return string|null Host
- */
- public function getProxy()
- {
- return $this->hasParam('proxy') ? $this->getParam('proxy') : null;
- }
- /**
- * Set proxy for http connections. Null is for environmental proxy,
- * empty string to disable proxy and proxy string to set actual http proxy.
- *
- * @see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTPROXY
- *
- * @param string|null $proxy
- *
- * @return $this
- */
- public function setProxy($proxy)
- {
- return $this->setParam('proxy', $proxy);
- }
- /**
- * @return string|array
- */
- public function getTransport()
- {
- return $this->hasParam('transport') ? $this->getParam('transport') : self::DEFAULT_TRANSPORT;
- }
- /**
- * @param string|array $transport
- *
- * @return $this
- */
- public function setTransport($transport)
- {
- return $this->setParam('transport', $transport);
- }
- /**
- * @return bool
- */
- public function hasCompression()
- {
- return (bool) $this->hasParam('compression') ? $this->getParam('compression') : self::DEFAULT_COMPRESSION;
- }
- /**
- * @param bool $compression
- *
- * @return $this
- */
- public function setCompression($compression = null)
- {
- return $this->setParam('compression', $compression);
- }
- /**
- * @return string
- */
- public function getPath()
- {
- return $this->hasParam('path') ? $this->getParam('path') : '';
- }
- /**
- * @param string $path
- *
- * @return $this
- */
- public function setPath($path)
- {
- return $this->setParam('path', $path);
- }
- /**
- * @param int $timeout Timeout in seconds
- *
- * @return $this
- */
- public function setTimeout($timeout)
- {
- return $this->setParam('timeout', $timeout);
- }
- /**
- * @return int Connection timeout in seconds
- */
- public function getTimeout()
- {
- return (int) $this->hasParam('timeout') ? $this->getParam('timeout') : self::TIMEOUT;
- }
- /**
- * Number of seconds after a connection timeout occurs for every request during the connection phase.
- * Use a small value if you need a fast fail in case of dead, unresponsive or unreachable servers (~5 sec).
- *
- * Set to zero to switch to the default built-in connection timeout (300 seconds in curl).
- *
- * @see http://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html
- *
- * @param int $timeout Connect timeout in seconds
- *
- * @return $this
- */
- public function setConnectTimeout($timeout)
- {
- return $this->setParam('connectTimeout', $timeout);
- }
- /**
- * @return int Connection timeout in seconds
- */
- public function getConnectTimeout()
- {
- return (int) $this->hasParam('connectTimeout') ? $this->getParam('connectTimeout') : self::CONNECT_TIMEOUT;
- }
- /**
- * Enables a connection.
- *
- * @param bool $enabled OPTIONAL (default = true)
- *
- * @return $this
- */
- public function setEnabled($enabled = true)
- {
- return $this->setParam('enabled', $enabled);
- }
- /**
- * @return bool True if enabled
- */
- public function isEnabled()
- {
- return (bool) $this->getParam('enabled');
- }
- /**
- * Returns an instance of the transport type.
- *
- * @throws \Elastica\Exception\InvalidException If invalid transport type
- *
- * @return \Elastica\Transport\AbstractTransport Transport object
- */
- public function getTransportObject()
- {
- $transport = $this->getTransport();
- return AbstractTransport::create($transport, $this);
- }
- /**
- * @return bool Returns true if connection is persistent. True by default
- */
- public function isPersistent()
- {
- return (bool) $this->hasParam('persistent') ? $this->getParam('persistent') : true;
- }
- /**
- * @param array $config
- *
- * @return $this
- */
- public function setConfig(array $config)
- {
- return $this->setParam('config', $config);
- }
- /**
- * @param string $key
- * @param mixed $value
- *
- * @return $this
- */
- public function addConfig($key, $value)
- {
- $this->_params['config'][$key] = $value;
- return $this;
- }
- /**
- * @param string $key
- *
- * @return bool
- */
- public function hasConfig($key)
- {
- $config = $this->getConfig();
- return isset($config[$key]);
- }
- /**
- * Returns a specific config key or the whole
- * config array if not set.
- *
- * @param string $key Config key
- *
- * @throws \Elastica\Exception\InvalidException
- *
- * @return array|string Config value
- */
- public function getConfig($key = '')
- {
- $config = $this->getParam('config');
- if (empty($key)) {
- return $config;
- }
- if (!array_key_exists($key, $config)) {
- throw new InvalidException('Config key is not set: '.$key);
- }
- return $config[$key];
- }
- /**
- * @param \Elastica\Connection|array $params Params to create a connection
- *
- * @throws Exception\InvalidException
- *
- * @return self
- */
- public static function create($params = [])
- {
- if (is_array($params)) {
- return new self($params);
- }
- if ($params instanceof self) {
- return $params;
- }
- throw new InvalidException('Invalid data type');
- }
- /**
- * @return string User
- */
- public function getUsername()
- {
- return $this->hasParam('username') ? $this->getParam('username') : null;
- }
- /**
- * @return string Password
- */
- public function getPassword()
- {
- return $this->hasParam('password') ? $this->getParam('password') : null;
- }
- }
|