class ExportEntityWriter in Content Synchronizer 8
Same name and namespace in other branches
- 8.2 src/Processors/ExportEntityWriter.php \Drupal\content_synchronizer\Processors\ExportEntityWriter
- 3.x src/Processors/ExportEntityWriter.php \Drupal\content_synchronizer\Processors\ExportEntityWriter
Export entity writer.
Hierarchy
- class \Drupal\content_synchronizer\Processors\ExportEntityWriter uses JsonWriterTrait
Expanded class hierarchy of ExportEntityWriter
11 files declare their use of ExportEntityWriter
- ArchiveDownloader.php in src/
Service/ ArchiveDownloader.php - ArchiveDownloaderController.php in src/
Controller/ ArchiveDownloaderController.php - ContentSynchronizerCommands.php in src/
Commands/ ContentSynchronizerCommands.php - content_synchronizer.drush.inc in ./
content_synchronizer.drush.inc - Drush commands for content_synchronizer module.
- EntityProcessorBase.php in src/
Processors/ Entity/ EntityProcessorBase.php
File
- src/
Processors/ ExportEntityWriter.php, line 15
Namespace
Drupal\content_synchronizer\ProcessorsView source
class ExportEntityWriter {
use JsonWriterTrait;
const TYPE_EXTENSION = '.json';
const EXPORT_EXTENSION = '.zip';
const ROOT_FILE_NAME = 'root';
const GENERATOR_DIR = 'temporary://content_synchronizer/';
const FIELD_GID = 'gid';
const FIELD_UUID = 'uuid';
const FIELD_CHANGED = 'changed';
const FIELD_ENTITY_TYPE_ID = 'entity_type_id';
const FIELD_ENTITY_ID = 'entity_id';
const FIELD_LABEL = 'label';
protected $id;
/** @var \Cocur\Slugify\Slugify $slugifier */
protected $slugifier;
public function __construct() {
$this->slugifier = new Slugify();
}
/**
* THe id of the directory.
*/
public function initFromId($id) {
$this->id = $id . '.' . time() . rand();
}
/**
* Return the directory path.
*/
public function getDirPath() {
return self::GENERATOR_DIR . 'export/' . $this->id;
}
/**
* Write the document to export.
*
* @param \Drupal\Core\Entity\Entity $entityToExport
* The entity to export.
* @param array $dataToExport
* The data to export.
*/
public function write(Entity $entityToExport, array $dataToExport) {
if (array_key_exists('gid', $dataToExport) && ($gid = $dataToExport['gid'])) {
// Get the previous exported entities :
if ($allExportedData = $this
->getExportedData($entityToExport)) {
// If the current entity has already been exported, the writer don't do anything.
if (array_key_exists($gid, $allExportedData)) {
return;
}
}
else {
$allExportedData = [];
}
// Add the current entity to export to the already exported data.
$allExportedData[$gid] = $dataToExport;
// Write files :
$this
->writeJson($allExportedData, $this
->getExpotedDataTypeFilePath($entityToExport));
}
}
/**
* Get the entity Type data already exported.
*
* @param string $entityType
* The entityType.
*
* @return mixed
* The data.
*/
public function getExportedData($entityType) {
$path = $this
->getExpotedDataTypeFilePath($entityType);
return $this
->getDataFromFile($path);
}
/**
* Add the entity to the exported root entities list.
*
* @param \Drupal\Core\Entity\Entity $entity
* The root entity to add.
*/
public function addRootEntity(Entity $entity) {
$rootEntitiesFilePath = $this
->getDirPath() . '/' . self::ROOT_FILE_NAME . self::TYPE_EXTENSION;
$rootEntities = $this
->getDataFromFile($rootEntitiesFilePath);
if (!$rootEntities) {
$rootEntities = [];
}
// Add entity data.
$rootEntities[] = [
self::FIELD_GID => $entity->contentSynchronizerGid,
self::FIELD_ENTITY_TYPE_ID => $entity
->getEntityTypeId(),
self::FIELD_ENTITY_ID => $entity
->id(),
self::FIELD_LABEL => static::getEntityLabel($entity),
self::FIELD_UUID => $entity
->uuid(),
];
$this
->writeJson($rootEntities, $rootEntitiesFilePath);
}
/**
* Return the entity label.
*/
public static function getEntityLabel(Entity $entity) {
return $entity
->label();
}
/**
* Return the file path of the type of the entity.
*/
public function getExpotedDataTypeFilePath(Entity $entity) {
return $this
->getDirPath() . '/' . $entity
->getEntityTypeId() . self::TYPE_EXTENSION;
}
/**
* Return the archive path.
*/
protected function getArchivePath() {
return $this
->getDirPath() . '/' . $this->slugifier
->slugify($this->id) . self::EXPORT_EXTENSION;
}
/**
* Zip the generated files.
*/
public function archiveFiles() {
$path = \Drupal::service('file_system')
->realpath($this
->getDirPath());
$zip = new ZipArchive();
$zip
->open($path . '/' . $this->slugifier
->slugify($this->id) . self::EXPORT_EXTENSION, ZIPARCHIVE::CREATE);
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::CURRENT_AS_FILEINFO));
foreach ($files as $file) {
if (!in_array($file
->getFilename(), [
'.',
'..',
])) {
$relativePath = str_replace($path . '/', '', $file);
if ($file
->isDir()) {
$zip
->addEmptyDir($relativePath . '/');
}
else {
$zip
->addFromString($relativePath, $file
->getContents());
}
}
}
$zip
->close();
}
/**
* Return the archive uri.
*/
public function getArchiveUri() {
$archivePath = $this
->getArchivePath();
if (file_exists($archivePath)) {
return $archivePath;
}
return FALSE;
}
/**
* The writer id.
*
* @return string
* THe id.
*/
public function getId() {
return $this->id;
}
}