Runtime.php in Acquia Search 3.x
File
src/Helper/Runtime.php
View source
<?php
namespace Drupal\acquia_search\Helper;
use Drupal\acquia_search\AcquiaSearchApiClient;
use Drupal\acquia_search\PreferredSearchCore;
use Drupal\Core\Database\Database;
use Drupal\search_api\Entity\Server;
class Runtime {
protected static $preferredSearchCoreService;
public static function getAhDatabaseRole(array $options, array $connection_info) : string {
$ah_db_name = $options['database'];
foreach ($connection_info as $db_role => $db_array) {
if ($db_role == 'default') {
continue;
}
if ($db_array['default']['database'] == $ah_db_name) {
$pattern = '/[^a-zA-Z0-9_]+/';
$db_role = preg_replace($pattern, '', $db_role);
return $db_role;
}
}
return '';
}
public static function getPreferredSearchCoreService() : PreferredSearchCore {
if (self::$preferredSearchCoreService) {
return self::$preferredSearchCoreService;
}
$ah_env = $_ENV['AH_SITE_ENVIRONMENT'] ?? '';
$ah_site_name = $_ENV['AH_SITE_NAME'] ?? '';
$ah_site_group = $_ENV['AH_SITE_GROUP'] ?? '';
$conf_path = \Drupal::service('site.path');
$sites_folder_name = substr($conf_path, strrpos($conf_path, '/') + 1);
$ah_db_role = '';
if ($ah_env && $ah_site_name && $ah_site_group) {
$options = Database::getConnection()
->getConnectionOptions();
$connection_info = Database::getAllConnectionInfo();
$ah_db_role = Runtime::getAhDatabaseRole($options, $connection_info);
}
if (!($available_cores = Runtime::getAcquiaSearchApiClient(Storage::getUuid())
->getSearchIndexes(Storage::getIdentifier()))) {
$available_cores = [];
}
return new PreferredSearchCore(Storage::getIdentifier(), $ah_env, $sites_folder_name, $ah_db_role, $available_cores);
}
public static function shouldEnforceReadOnlyMode() : bool {
$read_only = FALSE;
if (Storage::isReadOnly()) {
$read_only = TRUE;
}
\Drupal::moduleHandler()
->alter('acquia_search_should_enforce_read_only', $read_only);
return $read_only;
}
public static function getAcquiaSearchApiClient(string $application_uuid = NULL) : AcquiaSearchApiClient {
if (!$application_uuid) {
$application_uuid = Storage::getUuid();
}
$drupal_http_client = \Drupal::service('http_client');
$cache = \Drupal::cache();
$auth_info = [
'host' => Storage::getApiHost(),
'app_uuid' => $application_uuid,
'key' => Storage::getApiKey(),
];
return new AcquiaSearchApiClient($auth_info, $drupal_http_client, $cache);
}
public static function isAcquiaServer(Server $server) : bool {
$backend_config = $server
->getBackendConfig();
return !empty($backend_config['connector']) && $backend_config['connector'] === 'solr_acquia_connector';
}
}