Skip to content

Instantly share code, notes, and snippets.

@paullinney
Created July 24, 2018 18:12
Show Gist options
  • Save paullinney/3981754dab6745ac53afbe055cc03c63 to your computer and use it in GitHub Desktop.
Save paullinney/3981754dab6745ac53afbe055cc03c63 to your computer and use it in GitHub Desktop.
CMT Solr Connection
<?php
namespace Drupal\cmt_solr_connection;
use Drupal\Core\Config\ConfigFactory;
/**
* Class SolrConnection
*/
class SolrConnection {
/**
* @var $config
*/
private $config;
/**
* Constructor.
*
* @param ConfigFactory $config
*/
public function __construct(ConfigFactory $config) {
$this->config = $config;
}
/**
* Get the connection details for a core.
*
* @param $core
*/
public function getConnectionDetails($core = 'appsearch') {
if (isset($_ENV['PLATFORM_RELATIONSHIPS'])) {
$relationships = json_decode(base64_decode($_ENV['PLATFORM_RELATIONSHIPS']), TRUE);
// If we are running on Platform production environment.
if($_ENV['PLATFORM_PROJECT'] == 'skilldevprod_cmt') {
$core = 'solr_' . $core;
}
$config = $this->buildConfigArrayForSolarium(
$relationships[$core][0]['host'],
'/' . $relationships[$core][0]['path'],
$relationships[$core][0]['port']
);
}
else {
if ($core == 'appsearch') {
$config = $this->config->get('search_api.server.application')
->get('backend_config');
}
else {
$config = $this->config->get('search_api.server.course')
->get('backend_config');
}
$config = $this->buildConfigArrayForSolarium(
$config['connector_config']['host'],
$config['connector_config']['path'] . '/' . $config['connector_config']['core'],
$config['connector_config']['port']
);
}
return $config;
}
/**
* Build up the config array for the Solarium Client.
*
* @param $host
* @param $path
* @param $port
*/
private function buildConfigArrayForSolarium($host, $path, $port) {
return [
'endpoint' => [
'localhost' => [
'host' => $host,
'path' => $path,
'port' => $port,
],
],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment