View source
<?php
namespace Drupal\acquia_connector\Controller;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Site\Settings;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
class VariablesController extends ControllerBase {
protected $mapping = [];
protected $configs = NULL;
public function __construct() {
$this->mapping = $this
->config('acquia_connector.settings')
->get('mapping');
}
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;
}
public function getVariableValue($var) {
if (empty($this->mapping[$var])) {
throw new \UnexpectedValueException($var);
}
$var_type = $this->mapping[$var][0];
$var_name = !empty($this->mapping[$var][1]) ? $this->mapping[$var][1] : NULL;
if ($var_type == 'state') {
return \Drupal::state()
->get($var_name);
}
if ($var_type == 'settings') {
return Settings::get($var_name);
}
if ($var_type == 'container_parameter') {
if (\Drupal::hasContainer()) {
try {
return \Drupal::getContainer()
->getParameter($var_name);
} catch (ParameterNotFoundException $e) {
}
}
throw new \UnexpectedValueException($var);
}
$key_exists = NULL;
$config = self::getAllConfigs();
$value = NestedArray::getValue($config, $this->mapping[$var], $key_exists);
if ($key_exists) {
return $value;
}
throw new \UnexpectedValueException($var);
}
public function getVariablesData() {
$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', []);
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) {
}
}
foreach ($data as $var_name => $var) {
if (in_array($var_name, $waived_spi_def_vars)) {
unset($data[$var_name]);
}
}
return Json::encode($data);
}
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';
}
$ignored = array_merge($ignored, [
'drupal_private_key',
'site_mail',
'site_name',
'maintenance_mode',
'user_register',
]);
$whitelist = $this
->config('acquia_connector.settings')
->get('spi.set_variables_automatic');
foreach ($set_variables as $key => $value) {
if (in_array($key, $whitelist) && !in_array($key, $ignored)) {
if (!empty($this->mapping[$key])) {
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') {
}
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.');
}
}
}