You are here

class VariablesController in Acquia Connector 3.x

Same name and namespace in other branches
  1. 8.2 src/Controller/VariablesController.php \Drupal\acquia_connector\Controller\VariablesController
  2. 8 src/Controller/VariablesController.php \Drupal\acquia_connector\Controller\VariablesController

Class MappingController.

Hierarchy

Expanded class hierarchy of VariablesController

1 file declares its use of VariablesController
AcquiaConnectorSpiTest.php in tests/src/Functional/AcquiaConnectorSpiTest.php

File

src/Controller/VariablesController.php, line 14

Namespace

Drupal\acquia_connector\Controller
View source
class VariablesController extends ControllerBase {

  /**
   * Mapping array. Loaded from configuration.
   *
   * @var array
   */
  protected $mapping = [];

  /**
   * All config for the site.
   *
   * @var null|array
   */
  protected $configs = NULL;

  /**
   * Construction method.
   */
  public function __construct() {
    $this->mapping = $this
      ->config('acquia_connector.settings')
      ->get('mapping');
  }

  /**
   * Load configs for all enabled modules.
   *
   * @return array
   *   Array of Drupal configs.
   */
  public function getAllConfigs() {
    if (!is_null($this->configs)) {
      return $this->configs;
    }
    $this->configs = [];
    $names = \Drupal::configFactory()
      ->listAll();
    foreach ($names as $config_name) {
      $this->configs[$config_name] = $this
        ->config($config_name)
        ->get();
    }
    return $this->configs;
  }

  /**
   * Get a variable value by the variable name.
   *
   * @param string $var
   *   Variable name.
   *
   * @return mixed
   *   Variable value.
   *
   * @throws \UnexpectedValueException
   */
  public function getVariableValue($var) {

    // We have no mapping for the variable.
    if (empty($this->mapping[$var])) {
      throw new \UnexpectedValueException($var);
    }

    // Variable type (for state, setting and container parameter only).
    // Holds Config name for the configuration object variables.
    $var_type = $this->mapping[$var][0];

    // Variable machine name (for state, settings and container parameter only).
    $var_name = !empty($this->mapping[$var][1]) ? $this->mapping[$var][1] : NULL;

    // Variable is Drupal state.
    if ($var_type == 'state') {
      return \Drupal::state()
        ->get($var_name);
    }

    // Variable is Drupal setting.
    if ($var_type == 'settings') {
      return Settings::get($var_name);
    }

    // Variable is Container Parameter.
    if ($var_type == 'container_parameter') {
      if (\Drupal::hasContainer()) {
        try {
          return \Drupal::getContainer()
            ->getParameter($var_name);
        } catch (ParameterNotFoundException $e) {

          // Parameter not found.
        }
      }
      throw new \UnexpectedValueException($var);
    }

    // Variable is data from Configuration object (D7 Variable).
    // We can not detect this variable type so we're processing it in last turn.
    $key_exists = NULL;
    $config = self::getAllConfigs();
    $value = NestedArray::getValue($config, $this->mapping[$var], $key_exists);
    if ($key_exists) {
      return $value;
    }
    throw new \UnexpectedValueException($var);
  }

  /**
   * Get all system variables.
   *
   * @return string
   *   Variables values keyed by the variable name.
   */
  public function getVariablesData() {

    // Send SPI definition timestamp to see if the site needs updates.
    $data = [
      'acquia_spi_def_timestamp' => $this
        ->state()
        ->get('acquia_spi_data.def_timestamp', 0),
    ];
    $variables = [
      'acquia_spi_send_node_user',
      'acquia_spi_admin_priv',
      'acquia_spi_send_watchdog',
      'acquia_spi_use_cron',
      'cache_backends',
      'cache_default_class',
      'cache_inc',
      'cron_safe_threshold',
      'googleanalytics_cache',
      'error_level',
      'preprocess_js',
      'page_cache_maximum_age',
      'block_cache',
      'preprocess_css',
      'page_compression',
      'cron_last',
      'clean_url',
      'redirect_global_clean',
      'theme_zen_settings',
      'site_offline',
      'site_name',
      'user_register',
      'user_signatures',
      'user_admin_role',
      'user_email_verification',
      'user_cancel_method',
      'filter_fallback_format',
      'dblog_row_limit',
      'date_default_timezone',
      'file_default_scheme',
      'install_profile',
      'maintenance_mode',
      'update_last_check',
      'site_default_country',
      'acquia_spi_saved_variables',
      'acquia_spi_set_variables_automatic',
      'acquia_spi_ignored_set_variables',
      'acquia_spi_set_variables_override',
      'http_response_debug_cacheability_headers',
    ];
    $spi_def_vars = $this
      ->state()
      ->get('acquia_spi_data.def_vars', []);
    $waived_spi_def_vars = $this
      ->state()
      ->get('acquia_spi_data.def_waived_vars', []);

    // Merge hard coded $variables with vars from SPI definition.
    foreach ($spi_def_vars as $var_name => $var) {
      if (!in_array($var_name, $waived_spi_def_vars) && !in_array($var_name, $variables)) {
        $variables[] = $var_name;
      }
    }
    foreach ($variables as $name) {
      try {
        $data[$name] = $this
          ->getVariableValue($name);
      } catch (\UnexpectedValueException $e) {

        // Variable does not exist.
      }
    }

    // Unset waived vars so they won't be sent to NSPI.
    foreach ($data as $var_name => $var) {
      if (in_array($var_name, $waived_spi_def_vars)) {
        unset($data[$var_name]);
      }
    }

    // Collapse to JSON string to simplify transport.
    return Json::encode($data);
  }

  /**
   * Set variables from NSPI response.
   *
   * @param array|bool $set_variables
   *   Variables to be set.
   */
  public function setVariables($set_variables) {
    $this
      ->getLogger('acquia spi')
      ->notice('SPI set variables: @messages', [
      '@messages' => implode(', ', $set_variables),
    ]);
    if (empty($set_variables)) {
      return;
    }
    $saved = [];
    $ignored = $this
      ->config('acquia_connector.settings')
      ->get('spi.ignored_set_variables');
    if (!$this
      ->config('acquia_connector.settings')
      ->get('spi.set_variables_override')) {
      $ignored[] = 'acquia_spi_set_variables_automatic';
    }

    // Some variables can never be set.
    $ignored = array_merge($ignored, [
      'drupal_private_key',
      'site_mail',
      'site_name',
      'maintenance_mode',
      'user_register',
    ]);

    // Variables that can be automatically set.
    $whitelist = $this
      ->config('acquia_connector.settings')
      ->get('spi.set_variables_automatic');
    foreach ($set_variables as $key => $value) {

      // Approved variables get set immediately unless ignored.
      if (in_array($key, $whitelist) && !in_array($key, $ignored)) {
        if (!empty($this->mapping[$key])) {

          // State.
          if ($this->mapping[$key][0] == 'state' and !empty($this->mapping[$key][1])) {
            \Drupal::state()
              ->set($this->mapping[$key][1], $value);
            $saved[] = $key;
          }
          elseif ($this->mapping[$key][0] == 'settings') {

            // No setter for Settings.
          }
          else {
            $mapping_row_copy = $this->mapping[$key];
            $config_name = array_shift($mapping_row_copy);
            $variable_name = implode('.', $mapping_row_copy);
            \Drupal::configFactory()
              ->getEditable($config_name)
              ->set($variable_name, $value);
            \Drupal::configFactory()
              ->getEditable($config_name)
              ->save();
            $saved[] = $key;
          }
        }
        elseif (preg_match('/^([^\\s]+):([^\\s]+)$/ui', $key, $regs)) {
          $config_name = $regs[1];
          $variable_name = $regs[2];
          \Drupal::configFactory()
            ->getEditable($config_name)
            ->set($variable_name, $value);
          \Drupal::configFactory()
            ->getEditable($config_name)
            ->save();
          $saved[] = $key;
        }
        else {
          $this
            ->getLogger('acquia spi')
            ->notice('Variable is not implemented: ' . $key);
        }
      }
    }
    if (!empty($saved)) {
      \Drupal::configFactory()
        ->getEditable('acquia_connector.settings')
        ->set('spi.saved_variables', [
        'variables' => $saved,
        'time' => time(),
      ]);
      \Drupal::configFactory()
        ->getEditable('acquia_connector.settings')
        ->save();
      $this
        ->getLogger('acquia spi')
        ->notice('Saved variables from the Acquia: @variables', [
        '@variables' => implode(', ', $saved),
      ]);
    }
    else {
      $this
        ->getLogger('acquia spi')
        ->notice('Did not save any variables from Acquia.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 46
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
VariablesController::$configs protected property All config for the site.
VariablesController::$mapping protected property Mapping array. Loaded from configuration.
VariablesController::getAllConfigs public function Load configs for all enabled modules.
VariablesController::getVariablesData public function Get all system variables.
VariablesController::getVariableValue public function Get a variable value by the variable name.
VariablesController::setVariables public function Set variables from NSPI response.
VariablesController::__construct public function Construction method.