You are here

class DESConnector82 in Elasticsearch Connector 8

Drupal Elasticsearch Interface.

@package Drupal\elasticsearch_connector

Hierarchy

Expanded class hierarchy of DESConnector82

File

src/DESConnector/DESConnector82.php, line 20
Provides Elasticsearch Client for Drupal's Elasticsearch Connector module.

Namespace

Drupal\elasticsearch_connector\DESConnector
View source
class DESConnector82 extends DESConnector implements DESConnectorInterface {
  const CLUSTER_STATUS_GREEN = 'green';
  const CLUSTER_STATUS_YELLOW = 'yellow';
  const CLUSTER_STATUS_RED = 'red';
  protected static $instances;
  protected $client;

  /**
   * Singleton constructor.
   */
  private function __construct($client) {

    // TODO: Validate if we have a valid client.
    $this->client = $client;
  }

  /**
   * Singleton clone.
   */
  private function __clone() {
  }

  /**
   * Singleton wakeup.
   */
  private function __wakeup() {
  }

  /**
   * Singleton sleep.
   */
  private function __sleep() {
  }

  /**
   * Initializes the needed client.
   *
   * TODO: We need to check the available options for the ClientBuilder
   *       and set them after the alter hook.
   *
   * @param array $hosts
   *   The URLs of the Elasticsearch hosts.
   *
   * @return Client
   */
  public static function getInstance(array $hosts) {
    $hash_hosts = array();
    foreach ($hosts as $host) {
      $hash_hosts[] = $host['url'];
    }
    $hash = md5(implode(':', $hash_hosts));
    if (!isset($instances[$hash])) {
      foreach ($hosts as $host) {
        $cluster_url = self::buildClusterUrl($host['url'], $host['options']);
        $options['hosts'][] = $cluster_url;
      }

      // TODO: Remove this from the abstraction!
      // It should be passed via parameter.
      \Drupal::moduleHandler()
        ->alter('elasticsearch_connector_load_library_options', $options);
      $builder = ClientBuilder::create();
      $builder
        ->setHosts($options['hosts']);
      $instances[$hash] = new DESConnector82($builder
        ->build());
    }
    return $instances[$hash];
  }

  /**
   * Builds the proper cluster URL based on the provided options.
   *
   * @param $cluster
   *
   * @return string
   */
  public static function buildClusterUrl($cluster_url, $options) {
    if (isset($options['use_authentication'])) {
      if (isset($options['username']) && isset($options['password'])) {
        $schema = file_uri_scheme($cluster_url);
        $host = file_uri_target($cluster_url);
        $user = $options['username'];
        $pass = $options['password'];
        return $schema . '://' . $user . ':' . $pass . '@' . $host;
      }
    }
    return $cluster_url;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DESConnector82::$client protected property Overrides DESConnector::$client
DESConnector82::$instances protected static property Overrides DESConnector::$instances
DESConnector82::buildClusterUrl public static function Builds the proper cluster URL based on the provided options.
DESConnector82::CLUSTER_STATUS_GREEN constant Overrides DESConnector::CLUSTER_STATUS_GREEN
DESConnector82::CLUSTER_STATUS_RED constant Overrides DESConnector::CLUSTER_STATUS_RED
DESConnector82::CLUSTER_STATUS_YELLOW constant Overrides DESConnector::CLUSTER_STATUS_YELLOW
DESConnector82::getInstance public static function Initializes the needed client. Overrides DESConnector::getInstance
DESConnector82::__clone private function Singleton clone. Overrides DESConnector::__clone
DESConnector82::__construct private function Singleton constructor. Overrides DESConnector::__construct
DESConnector82::__sleep private function Singleton sleep. Overrides DESConnector::__sleep
DESConnector82::__wakeup private function Singleton wakeup. Overrides DESConnector::__wakeup
DESConnector::bulk public function
DESConnector::clusterIsOk public function Check if we have a connection the cluster.
DESConnector::getClient protected function
DESConnector::getCluster public function
DESConnector::getClusterHealth public function Get cluster health.
DESConnector::getClusterInfo public function Return cluster info.
DESConnector::getClusterState public function Get cluster state.
DESConnector::getClusterStats public function Get cluster stats.
DESConnector::getClusterStatus public function Check if we have a connection the cluster.
DESConnector::getIndices public function
DESConnector::getNodes public function
DESConnector::getNodesInfo public function Get nodes info.
DESConnector::getNodesProperties public function Return nodes info.
DESConnector::getNodesStats public function Get nodes stats.
DESConnector::info public function Elasticsearch info function.
DESConnector::ping public function Elasticsearch ping function.
DESConnector::__call public function Magic method call.