View source
<?php
namespace Drupal\content_synchronizer\Commands;
use Drush\Commands\DrushCommands;
use Drupal\content_synchronizer\Entity\ImportEntity;
use Drupal\content_synchronizer\Processors\ExportEntityWriter;
use Drupal\content_synchronizer\Entity\ExportEntity;
use Drupal\content_synchronizer\Processors\ExportProcessor;
use Drupal\content_synchronizer\Processors\ImportProcessor;
use Drupal\content_synchronizer\Form\LaunchImportForm;
class ContentSynchronizerCommands extends DrushCommands {
public function synchronizerCreateImport($path, $options = [
'as_function' => FALSE,
]) {
if (file_exists($path)) {
$extensionData = explode('.', $path);
if (end($extensionData) == 'zip') {
if ($file = file_save_data(file_get_contents($path))) {
$name = strip_tags(t('Drush import - %date', [
'%date' => \Drupal::service('date.formatter')
->format(time()),
]));
$ie = ImportEntity::create([
'name' => $name,
ImportEntity::FIELD_ARCHIVE => $file,
]);
$ie
->save();
$this->logger
->notice(t('The import has been created')
->__toString());
}
}
else {
$this->logger
->error(t('The file is not a .zip archive')
->__toString());
}
}
else {
$this->logger
->error(t('No file found')
->__toString());
}
if ($options['as_function'] == TRUE) {
return $ie
->id();
}
}
public function synchronizerCleanTemporaryFiles() {
$path = \Drupal::service('file_system')
->realpath(ExportEntityWriter::GENERATOR_DIR);
foreach (glob($path . '/*') as $file) {
if (is_dir($file)) {
file_unmanaged_delete_recursive($file);
}
}
}
public function synchronizerLaunchExport($exportId, $destination = '') {
if ($export = ExportEntity::load($exportId)) {
$entitiesToExport = $export
->getEntitiesList();
$writer = new ExportEntityWriter();
$writer
->initFromId($export
->label());
$processor = new ExportProcessor($writer);
$count = count($entitiesToExport);
foreach (array_values($entitiesToExport) as $key => $entity) {
try {
$processor
->exportEntity($entity);
$status = t('Exported');
} catch (\Exception $error) {
$this->logger
->error($error
->getMessage());
$status = t('Error');
}
$this->logger
->notice(t('[@key/@count] - "@label" - @status', [
'@key' => $key + 1,
'@count' => $count,
'@label' => ExportEntityWriter::getEntityLabel($entity),
'@status' => $status,
])
->__toString());
}
$tempArchive = $path = \Drupal::service('file_system')
->realpath($processor
->closeProcess());
if ($destination == '') {
$destination = './' . basename($tempArchive);
}
rename($tempArchive, $destination);
$this->logger
->notice(t('Archive file : @destination', [
'@destination' => $destination,
])
->__toString());
}
}
public function synchronizerLaunchImport($importId, $options = [
'publish' => '',
'update' => '',
]) {
if ($import = ImportEntity::load($importId)) {
if (!in_array('publication_' . $options['publish'], array_keys(LaunchImportForm::getCreateOptions()))) {
$message = "Publish option must be in : publish|unpublish";
throw new \Exception($message);
}
if (!in_array('update_' . $options['update'], array_keys(LaunchImportForm::getUpdateOptions()))) {
$message = "Update option must be in : systematic|if_recent|no_update";
throw new \Exception($message);
}
$createType = 'publication_' . $options['publish'];
$updateType = 'update_' . $options['update'];
$importProcessor = new ImportProcessor($import);
$importProcessor
->setCreationType($createType);
$importProcessor
->setUpdateType($updateType);
$rootEntities = $import
->getRootsEntities();
$count = count($rootEntities);
foreach ($rootEntities as $key => $rootEntityData) {
try {
$importProcessor
->importEntityFromRootData($rootEntityData);
$status = array_key_exists('edit_url', $rootEntityData) ? t('Updated') : t('Created');
} catch (\Exception $error) {
$this->logger
->error($error
->getMessage());
$status = t('Error');
}
$this->logger
->notice(t('[@key/@count] - "@label" - @status', [
'@key' => $key + 1,
'@count' => $count,
'@status' => $status,
'@label' => $rootEntityData['label'],
])
->__toString());
}
$import
->removeArchive();
}
}
public function synchronizerAllExport($destination = '') {
$exportEntity = ExportEntity::create([
'name' => 'export-all',
]);
$exportEntity
->save();
$exportId = $exportEntity
->id();
foreach ([
'node',
'taxonomy_term',
] as $entity_type) {
$ids = \Drupal::entityQuery($entity_type)
->execute();
$entities = \Drupal::entityTypeManager()
->getStorage($entity_type)
->loadMultiple($ids);
foreach ($entities as $entity) {
$exportEntity
->addEntity($entity);
}
}
$this
->synchronizerLaunchExport($exportId, $destination);
}
public function synchronizerImportZip($file_path) {
$importId = $this
->synchronizerCreateImport($file_path, [
'as_function' => TRUE,
]);
$this
->synchronizerLaunchImport($importId, [
'publish' => 'publish',
'update' => 'systematic',
]);
}
}