You are here

class ConfigImporterService in Config Importer and Tools 8.0

Same name and namespace in other branches
  1. 8.2 src/ConfigImporterService.php \Drupal\config_import\ConfigImporterService
  2. 8 src/ConfigImporterService.php \Drupal\config_import\ConfigImporterService

Class ConfigImporterService.

@package Drupal\config_import

Hierarchy

Expanded class hierarchy of ConfigImporterService

1 string reference to 'ConfigImporterService'
config_import.services.yml in ./config_import.services.yml
config_import.services.yml
1 service uses ConfigImporterService
config_import.importer in ./config_import.services.yml
Drupal\config_import\ConfigImporterService

File

src/ConfigImporterService.php, line 29

Namespace

Drupal\config_import
View source
class ConfigImporterService implements ConfigImporterServiceInterface {

  /**
   * Uuid definition.
   *
   * @var UuidInterface
   */
  protected $uuid;

  /**
   * CachedStorage definition.
   *
   * @var CachedStorage
   */
  protected $configStorage;

  /**
   * ConfigManager definition.
   *
   * @var ConfigManagerInterface
   */
  protected $configManager;

  /**
   * ContainerAwareEventDispatcher definition.
   *
   * @var ContainerAwareEventDispatcher
   */
  protected $eventDispatcher;

  /**
   * LockBackend definition.
   *
   * @var LockBackendInterface
   */
  protected $lock;

  /**
   * TypedConfigManager definition.
   *
   * @var TypedConfigManager
   */
  protected $configTyped;

  /**
   * ModuleHandler definition.
   *
   * @var ModuleHandler
   */
  protected $moduleHandler;

  /**
   * ModuleInstaller definition.
   *
   * @var ModuleInstaller
   */
  protected $moduleInstaller;

  /**
   * ThemeHandler definition.
   *
   * @var ThemeHandler
   */
  protected $themeHandler;

  /**
   * TranslationManager definition.
   *
   * @var TranslationManager
   */
  protected $translationManager;

  /**
   * FileSystem definition.
   *
   * @var FileSystem
   */
  protected $fileSystem;

  /**
   * ConfigImporterService constructor.
   *
   * @param UuidInterface $uuid
   *   Uuid.
   * @param CachedStorage $config_storage
   *   CachedStorage.
   * @param ConfigManagerInterface $config_manager
   *   ConfigManager.
   * @param ContainerAwareEventDispatcher $event_dispatcher
   *   ContainerAwareEventDispatcher.
   * @param LockBackendInterface $lock
   *   LockBackend.
   * @param TypedConfigManager $config_typed
   *   TypedConfigManager.
   * @param ModuleHandler $module_handler
   *   ModuleHandler.
   * @param ModuleInstaller $module_installer
   *   ModuleInstaller.
   * @param ThemeHandler $theme_handler
   *   ThemeHandler.
   * @param TranslationManager $translation_manager
   *   TranslationManager.
   * @param FileSystem $file_system
   *   FileSystem.
   */
  public function __construct(UuidInterface $uuid, CachedStorage $config_storage, ConfigManagerInterface $config_manager, ContainerAwareEventDispatcher $event_dispatcher, LockBackendInterface $lock, TypedConfigManager $config_typed, ModuleHandler $module_handler, ModuleInstaller $module_installer, ThemeHandler $theme_handler, TranslationManager $translation_manager, FileSystem $file_system) {
    $this->uuid = $uuid;
    $this->configStorage = $config_storage;
    $this->configManager = $config_manager;
    $this->eventDispatcher = $event_dispatcher;
    $this->lock = $lock;
    $this->configTyped = $config_typed;
    $this->moduleHandler = $module_handler;
    $this->moduleInstaller = $module_installer;
    $this->themeHandler = $theme_handler;
    $this->translationManager = $translation_manager;
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  public function importConfigs(array $files) {

    // @todo The next string doesn't work during installation. Hardcode it.
    // $uri = 'temporary://confi_tmp_' . $this->uuid->->generate();
    $tmp_dir = '/tmp/confi_tmp_' . $this->uuid
      ->generate();

    // Save current configuration and disable file cache for config import.
    // @see https://www.drupal.org/node/2758325
    $file_cache_config = FileCacheFactory::getConfiguration();
    FileCacheFactory::setConfiguration([
      FileCacheFactory::DISABLE_CACHE => TRUE,
    ]);
    try {
      $this
        ->export($tmp_dir);
    } catch (Exception $e) {
      throw new ConfigImporterException($e
        ->getMessage());
    }
    foreach ($files as $source) {
      file_unmanaged_copy($source, $tmp_dir, FILE_EXISTS_REPLACE);
    }
    $this
      ->import($tmp_dir);

    // Restore file cache settings.
    FileCacheFactory::setConfiguration($file_cache_config);
  }

  /**
   * Import config from temporary directory.
   *
   * @param string $dir
   *   Temporary directory.
   */
  protected function import($dir) {
    $source_storage = new FileStorage($dir);
    $storage_comparer = new StorageComparer($source_storage, $this->configStorage, $this->configManager);
    if (!$storage_comparer
      ->createChangelist()
      ->hasChanges()) {
      return;
    }
    $config_importer = new ConfigImporter($storage_comparer, $this->eventDispatcher, $this->configManager, $this->lock, $this->configTyped, $this->moduleHandler, $this->moduleInstaller, $this->themeHandler, $this->translationManager);
    try {
      $config_importer
        ->validate();
      $config_importer
        ->import();
    } catch (ConfigException $e) {
      $message = 'The import failed due for the following reasons:' . "\n";
      $message .= implode("\n", $config_importer
        ->getErrors());
      throw new ConfigImporterException($message);
    }
  }

  /**
   * Export configuration to temporary directory.
   *
   * @param string $dir
   *   Path to directory.
   *
   * @throws \Exception
   *   When fails to create temporary directory.
   */
  protected function export($dir) {
    $result = $this->fileSystem
      ->mkdir($dir);
    if (!$result) {
      throw new \Exception('Failed to create temporary directory: ' . $dir);
    }
    $source_storage = $this->configStorage;
    $destination_storage = new FileStorage($dir);
    $filters = [];
    $this->moduleHandler
      ->alter('config_import_configs', $filters);
    foreach ($source_storage
      ->listAll() as $name) {
      if (in_array($name, $filters)) {
        continue;
      }
      $destination_storage
        ->write($name, $source_storage
        ->read($name));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigImporterService::$configManager protected property ConfigManager definition.
ConfigImporterService::$configStorage protected property CachedStorage definition.
ConfigImporterService::$configTyped protected property TypedConfigManager definition.
ConfigImporterService::$eventDispatcher protected property ContainerAwareEventDispatcher definition.
ConfigImporterService::$fileSystem protected property FileSystem definition.
ConfigImporterService::$lock protected property LockBackend definition.
ConfigImporterService::$moduleHandler protected property ModuleHandler definition.
ConfigImporterService::$moduleInstaller protected property ModuleInstaller definition.
ConfigImporterService::$themeHandler protected property ThemeHandler definition.
ConfigImporterService::$translationManager protected property TranslationManager definition.
ConfigImporterService::$uuid protected property Uuid definition.
ConfigImporterService::export protected function Export configuration to temporary directory.
ConfigImporterService::import protected function Import config from temporary directory.
ConfigImporterService::importConfigs public function Import config files. Overrides ConfigImporterServiceInterface::importConfigs
ConfigImporterService::__construct public function ConfigImporterService constructor.