degov-8.x-2.0/modules/degov_common/src/DegovConfigManagerBase.php
modules/degov_common/src/DegovConfigManagerBase.php
<?php
namespace Drupal\degov_common;
use Drupal\Core\Config\ConfigException;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ModuleInstallerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Class DegovConfigManagerBase. Base class for deGov configuration updaters.
*
* @package Drupal\degov_common
*/
class DegovConfigManagerBase implements DegovConfigManager {
/**
* @var \Drupal\Core\Config\StorageInterface
*/
protected $activeStorage;
/**
* The event dispatcher used to notify subscribers.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The configuration manager.
*
* @var \Drupal\Core\Config\ConfigManagerInterface
*/
protected $configManager;
/**
* The used lock backend instance.
*
* @var \Drupal\Core\Lock\LockBackendInterface
*/
protected $lock;
/**
* The typed config manager.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface
*/
protected $typedConfigManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* The module installer.
*
* @var \Drupal\Core\Extension\ModuleInstallerInterface
*/
protected $moduleInstaller;
/**
* Translation Interface service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $stringTranslation;
/**
* Constructs a configuration import object.
*
* @param \Drupal\Core\Config\StorageInterface $active_storage
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher used to notify subscribers of config import events.
* @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
* The configuration manager.
* @param \Drupal\Core\Lock\LockBackendInterface $lock
* The lock backend to ensure multiple imports do not occur at the same
* time.
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
* The typed configuration manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler
* @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
* The module installer.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
*/
public function __construct(StorageInterface $active_storage, EventDispatcherInterface $event_dispatcher, ConfigManagerInterface $config_manager, LockBackendInterface $lock, TypedConfigManagerInterface $typed_config, ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, ThemeHandlerInterface $theme_handler, TranslationInterface $string_translation) {
$this->activeStorage = $active_storage;
$this->eventDispatcher = $event_dispatcher;
$this->configManager = $config_manager;
$this->lock = $lock;
$this->typedConfigManager = $typed_config;
$this->moduleHandler = $module_handler;
$this->moduleInstaller = $module_installer;
$this->themeHandler = $theme_handler;
$this->stringTranslation = $string_translation;
}
/**
* Imports all the changes for the configuration with batch.
*
* @param \Drupal\Core\Config\ConfigImporter $config_importer
*/
public function configImport($config_importer) {
if ($config_importer->alreadyImporting()) {
drupal_set_message(t('Another request may be importing configuration already.'), 'error');
}
else {
try {
// This is the contents of \Drupal\Core\Config\ConfigImporter::import.
// Copied here so we can log progress.
if ($config_importer->hasUnprocessedConfigurationChanges()) {
$sync_steps = $config_importer->initialize();
foreach ($sync_steps as $step) {
$context = [];
do {
$config_importer->doSyncStep($step, $context);
if (isset($context['message'])) {
drupal_set_message(str_replace('Synchronizing', 'Synchronized', (string) $context['message']), 'status');
}
} while ($context['finished'] < 1);
}
}
if ($config_importer->getErrors()) {
throw new ConfigException('Errors occurred during import');
}
else {
drupal_set_message('The configuration was imported successfully.', 'status');
}
} catch (ConfigException $e) {
// Return a negative result for UI purposes. We do not differentiate
// between an actual synchronization error and a failed lock, because
// concurrent synchronizations are an edge-case happening only when
// multiple developers or site builders attempt to do it without
// coordinating.
$message = 'The import failed due for the following reasons:' . "\n";
$message .= implode("\n", $config_importer->getErrors());
watchdog_exception('config_import', $e);
drupal_set_message($message, 'error');
}
}
}
}