You are here

class ConfigImporterService in Config Importer and Tools 8

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

Class ConfigImporterService.

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 28

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;

  /**
   * Path to directory where configs located.
   *
   * @var string
   */
  protected $directory = '';

  /**
   * Configuration of FileCacheFactory.
   *
   * @var array
   */
  private $fileCacheConfig = [];

  /**
   * 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;

    // Save current configuration to disable file cache for a while
    // and restore afterwards.
    $this->fileCacheConfig = FileCacheFactory::getConfiguration();

    // @see https://www.drupal.org/node/2758325
    FileCacheFactory::setConfiguration([
      FileCacheFactory::DISABLE_CACHE => TRUE,
    ]);

    // Sync directory must be configured.
    $this
      ->setDirectory(\Drupal\Core\Site\Settings::get('config_sync_directory'));
  }

  /**
   * Restore file cache configuration.
   */
  public function __destruct() {
    FileCacheFactory::setConfiguration($this->fileCacheConfig);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('uuid'), $container
      ->get('config.storage'), $container
      ->get('config.manager'), $container
      ->get('event_dispatcher'), $container
      ->get('lock'), $container
      ->get('config.typed'), $container
      ->get('module_handler'), $container
      ->get('module_installer'), $container
      ->get('theme_handler'), $container
      ->get('string_translation'), $container
      ->get('file_system'));
  }

  /**
   * {@inheritdoc}
   */
  public function setDirectory($directory) {
    if (!is_dir($directory)) {
      throw new \InvalidArgumentException($directory . ' - is not valid path or type of directory with configurations.');
    }
    $this->directory = $directory;
  }

  /**
   * {@inheritdoc}
   */
  public function getDirectory() {
    return $this->directory;
  }

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

    // Stream wrappers are not available during installation.
    $tmp_dir = (defined('MAINTENANCE_MODE') ? '/tmp' : 'temporary:/') . '/confi_' . $this->uuid
      ->generate();
    if (!$this->fileSystem
      ->mkdir($tmp_dir)) {
      throw new ConfigImporterException('Failed to create temporary directory: ' . $tmp_dir);
    }

    // Define temporary storage for our shenanigans.
    $tmp_storage = new FileStorage($tmp_dir);

    // Dump all configurations into temporary directory.
    $this
      ->export($tmp_storage);

    // Overwrite exported configurations by our custom ones.
    foreach ($configs as $config) {
      $file = "{$this->directory}/{$config}.yml";
      if (file_exists($file)) {
        file_unmanaged_copy($file, $tmp_dir, FileSystemInterface::EXISTS_REPLACE);
      }
      else {

        // Possibly, config has been exported a little bit above. This could
        // happen if you removed it from disc, but not from database. Export
        // operation will generate it inside of temporary storage and we should
        // take care about this.
        $tmp_storage
          ->delete($config);

        // Remove config if it was specified, but file does not exists.
        $this->configStorage
          ->delete($config);
      }
    }

    // Remove configurations from storage which are not allowed for import.
    $this
      ->filter($tmp_storage);

    // Import changed, just overwritten items, into config storage.
    $this
      ->import($tmp_storage);
  }

  /**
   * {@inheritdoc}
   */
  public function exportConfigs(array $configs) {
    $storage = new FileStorage($this->directory);
    foreach ($configs as $config) {
      $storage
        ->write($config, $this->configStorage
        ->read($config));
    }
  }

  /**
   * Clean storage.
   *
   * @param FileStorage $storage
   *   A storage to prepare.
   *
   * @see hook_config_import_configs_alter()
   */
  protected function filter(FileStorage $storage) {
    $configs = [];

    // Collect config names which are not allowed for import.
    $this->moduleHandler
      ->alter('config_import_configs', $configs);
    foreach ($configs as $config) {
      $storage
        ->delete($config);
    }
  }

  /**
   * Dump configurations to files storage.
   *
   * @param FileStorage $storage
   *   A storage to dump to.
   */
  protected function export(FileStorage $storage) {
    foreach ($this->configStorage
      ->listAll() as $config) {
      $storage
        ->write($config, $this->configStorage
        ->read($config));
    }
  }

  /**
   * Import configurations from files storage.
   *
   * @param FileStorage $storage
   *   A storage to import from.
   */
  protected function import(FileStorage $storage) {
    $storage_comparer = new StorageComparer($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()
        ->import();
    } catch (ConfigException $e) {
      throw new ConfigImporterException(implode("\n", $config_importer
        ->getErrors()));
    }
  }

}

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::$directory protected property Path to directory where configs located.
ConfigImporterService::$eventDispatcher protected property ContainerAwareEventDispatcher definition.
ConfigImporterService::$fileCacheConfig private property Configuration of FileCacheFactory.
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::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ConfigImporterService::export protected function Dump configurations to files storage.
ConfigImporterService::exportConfigs public function Export configurations. Overrides ConfigImporterServiceInterface::exportConfigs
ConfigImporterService::filter protected function Clean storage.
ConfigImporterService::getDirectory public function Get path to directory where configs stored. Overrides ConfigImporterServiceInterface::getDirectory
ConfigImporterService::import protected function Import configurations from files storage.
ConfigImporterService::importConfigs public function Import configurations. Overrides ConfigImporterServiceInterface::importConfigs
ConfigImporterService::setDirectory public function Set path to directory where configs stored. Overrides ConfigImporterServiceInterface::setDirectory
ConfigImporterService::__construct public function ConfigImporterService constructor.
ConfigImporterService::__destruct public function Restore file cache configuration.