View source
<?php
namespace Drupal\config_import;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Config\CachedStorage;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Drupal\Core\Config\ConfigImporterException;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Config\TypedConfigManager;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\ProxyClass\Extension\ModuleInstaller;
use Drupal\Core\Extension\ThemeHandler;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Config\ConfigException;
use Exception;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\StorageComparer;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Component\FileCache\FileCacheFactory;
class ConfigImporterService implements ConfigImporterServiceInterface {
protected $uuid;
protected $configStorage;
protected $configManager;
protected $eventDispatcher;
protected $lock;
protected $configTyped;
protected $moduleHandler;
protected $moduleInstaller;
protected $themeHandler;
protected $translationManager;
protected $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;
}
public function importConfigs(array $files) {
$tmp_dir = '/tmp/confi_tmp_' . $this->uuid
->generate();
$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);
FileCacheFactory::setConfiguration($file_cache_config);
}
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);
}
}
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));
}
}
}