acquia_search-3.0.1/src/EventSubscriber/PossibleCores/FromConfig.php
src/EventSubscriber/PossibleCores/FromConfig.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\acquia_search\Plugin\SolrConnector\SearchApiSolrAcquiaConnector;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Gets the Acquia Search Server settings from Drupal's settings.
*/
final class FromConfig 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', 950];
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) {
$server = $event->getServer();
$backend_config = $server->getBackendConfig();
$config_core = $backend_config['connector_config']['acquia_search_core'] ?? '';
$config_readonly = $backend_config['connector_config']['acquia_search_override_readonly'] ?? SearchApiSolrAcquiaConnector::OVERRIDE_AUTO_SET;
if ($config_core !== '') {
$acquiaIdentifier = $this->subscription->getSettings()->getIdentifier();
// Always set as read only.
if ($config_readonly == SearchApiSolrAcquiaConnector::READ_ONLY) {
$event->setReadOnly(TRUE);
}
// Force set to read/write.
if ($config_readonly == SearchApiSolrAcquiaConnector::READ_WRITE) {
$event->setReadOnly(FALSE);
}
// By default, cores defined in config are read only. Enable read/write on
// Acquia Cloud when the environment matches the config.
if ($this->subscription->getProvider() === 'acquia_cloud') {
$ahEnv = $this->subscription->getSettings()
->getMetadata('AH_SITE_ENVIRONMENT');
$ahEnv = preg_replace('/[^a-zA-Z0-9]+/', '', $ahEnv);
// ACSF Sites should use the pre-configured env and db roles instead.
//phpcs:disable
if (isset($GLOBALS['gardens_site_settings'])) {
$ahEnv = $GLOBALS['gardens_site_settings']['env'];
}
//phpcs:enable
// If we only have the core name and not full ID, build the core name.
if (!str_contains($config_core, '.')) {
$event->addPossibleCore($acquiaIdentifier . '.' . $ahEnv . '.' . $config_core);
$config_environment = $ahEnv;
}
else {
$event->addPossibleCore($config_core);
$pieces = explode('.', $config_core);
$config_environment = $pieces[1];
}
// Change read_only status only when automatic detection is turned on.
if ($config_readonly == SearchApiSolrAcquiaConnector::OVERRIDE_AUTO_SET) {
// If the config environment matches the Acquia environment, set r/w.
if ($config_environment === $ahEnv) {
$event->setReadOnly(FALSE);
}
}
return;
}
// Set the core for non-acquia environments.
// Determine if the core is a full ID or not.
if (!str_contains($config_core, '.')) {
$event->addPossibleCore($acquiaIdentifier . '.dev.' . $config_core);
if ($config_readonly == SearchApiSolrAcquiaConnector::OVERRIDE_AUTO_SET) {
$event->setReadOnly(FALSE);
}
}
else {
$event->addPossibleCore($config_core);
}
}
// @phpstan-ignore-next-line
$event->stopPropagation();
}
}
