acquia_search-3.0.1/src/EventSubscriber/PossibleCores/LocalOverride.php
src/EventSubscriber/PossibleCores/LocalOverride.php
<?php
namespace Drupal\acquia_search\EventSubscriber\PossibleCores;
use Drupal\acquia_connector\Subscription;
use Drupal\acquia_search\AcquiaSearchEvents;
use Drupal\acquia_search\Event\AcquiaPossibleCoresEvent;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Site\Settings as CoreSettings;
use Drupal\search_api\ServerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Gets the Acquia Search Server settings from Drupal's settings.
*/
final class LocalOverride implements EventSubscriberInterface {
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Acquia subscription service.
*
* @var \Drupal\acquia_connector\Subscription
*/
protected $subscription;
/**
* Get Possible Cores from local settings file.
*
* @param \Drupal\acquia_connector\Subscription $subscription
* Acquia Subscription Service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Config Factory.
*/
public function __construct(Subscription $subscription, ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
$this->subscription = $subscription;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Overridden fires first and supersedes all other possible cores.
// phpcs:ignore
$events[AcquiaSearchEvents::GET_POSSIBLE_CORES][] = ['onGetPossibleCores', 999];
return $events;
}
/**
* Gets a preset possible core from settings.
*
* @param \Drupal\acquia_search\Event\AcquiaPossibleCoresEvent $event
* The dispatched event.
*
* @see \Drupal\acquia_connector\Settings
*/
public function onGetPossibleCores(AcquiaPossibleCoresEvent $event) {
// Check for new settings first, ignore old settings if new are set.
$search_settings = CoreSettings::get('acquia_search');
if (is_array($search_settings) && !empty($search_settings)) {
// New core override format that allows overriding the Solr index used by
// specific Search API servers.
if (isset($search_settings['server_overrides']) && is_array($search_settings['server_overrides'])) {
$server_id = ($event->getServer() instanceof ServerInterface) ? $event->getServer()->id() : $event->getServer();
if (isset($search_settings['server_overrides'][$server_id])) {
if (is_string($search_settings['server_overrides'][$server_id])) {
@trigger_error('Setting the core as a string value on settings.php acquia_search.server_overrides.SERVER_ID setting is deprecated in acquia_search:3.2.0 and will be removed from acquia_search:3.3.0. See https://www.drupal.org/project/acquia_search/issues/3432307', E_USER_DEPRECATED);
$server_name = $search_settings['server_overrides'][$server_id];
$server_readonly = $search_settings['read_only'] ?? TRUE;
}
elseif (isset($search_settings['server_overrides'][$server_id]['core_id'])) {
$server_name = $search_settings['server_overrides'][$server_id]['core_id'];
$server_readonly = $search_settings['server_overrides'][$server_id]['read_only'] ?? TRUE;
}
// If the core_id key is missing, don't do anything.
else {
return;
}
$event->addPossibleCore($server_name);
$event->setReadOnly($server_readonly);
// @phpstan-ignore-next-line
$event->stopPropagation();
return;
}
}
elseif (isset($search_settings['override_search_core'])) {
@trigger_error('Settings variable acquia_search.override_search_core is deprecated in acquia_search:3.2.0 and will be removed from acquia_search:3.3.0. To override the default Acquia Search server, add the values to acquia_search.server_overrides similar to any other Acquia Search server. See https://www.drupal.org/project/acquia_search/issues/3432307', E_USER_DEPRECATED);
$event->addPossibleCore($search_settings['override_search_core']);
// @phpstan-ignore-next-line
$event->stopPropagation();
}
// Lastly, set the global read_only variable.
if (isset($search_settings['read_only'])) {
@trigger_error('The global settings.php acquia_search.read_only setting is deprecated in acquia_search:3.2.0 and will be removed from acquia_search:3.3.0. There is no replacement. Set each servers read-only status individually. See https://www.drupal.org/project/acquia_search/issues/3432307', E_USER_DEPRECATED);
$readonly = $search_settings['read_only'] ?? TRUE;
$event->setReadOnly($readonly);
}
return;
}
$acquia_search_core = $this->configFactory->get('acquia_search.settings')->get('override_search_core') ?? '';
if (is_string($acquia_search_core) && $acquia_search_core !== '') {
@trigger_error('Config variable acquia_search.settings.override_search_core is deprecated in acquia_search:3.2.0 and will be removed from acquia_search:3.3.0. It is replaced with settings variable: "acquia_search.server_overrides.acquia_search_server". See https://www.drupal.org/project/acquia_search/issues/3432307', E_USER_DEPRECATED);
$event->addPossibleCore($acquia_search_core);
}
$acquia_search_solr_core = $this->configFactory->get('acquia_search_solr.settings')->get('override_search_core') ?? '';
if (is_string($acquia_search_solr_core) && $acquia_search_solr_core !== '') {
@trigger_error('Config variable acquia_search_solr.settings.override_search_core is deprecated in acquia_search:3.2.0 and will be removed from acquia_search:3.3.0. It is replaced with settings variable: "acquia_search.server_overrides.acquia_search_server". See https://www.drupal.org/project/acquia_search/issues/3432307', E_USER_DEPRECATED);
$event->addPossibleCore($acquia_search_solr_core);
}
}
}
