ExportEntityWriter.php in Content Synchronizer 8
File
src/Processors/ExportEntityWriter.php
View source
<?php
namespace Drupal\content_synchronizer\Processors;
use Cocur\Slugify\Slugify;
use Drupal\content_synchronizer\Base\JsonWriterTrait;
use Drupal\Core\Entity\Entity;
use ZipArchive;
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
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;
protected $slugifier;
public function __construct() {
$this->slugifier = new Slugify();
}
public function initFromId($id) {
$this->id = $id . '.' . time() . rand();
}
public function getDirPath() {
return self::GENERATOR_DIR . 'export/' . $this->id;
}
public function write(Entity $entityToExport, array $dataToExport) {
if (array_key_exists('gid', $dataToExport) && ($gid = $dataToExport['gid'])) {
if ($allExportedData = $this
->getExportedData($entityToExport)) {
if (array_key_exists($gid, $allExportedData)) {
return;
}
}
else {
$allExportedData = [];
}
$allExportedData[$gid] = $dataToExport;
$this
->writeJson($allExportedData, $this
->getExpotedDataTypeFilePath($entityToExport));
}
}
public function getExportedData($entityType) {
$path = $this
->getExpotedDataTypeFilePath($entityType);
return $this
->getDataFromFile($path);
}
public function addRootEntity(Entity $entity) {
$rootEntitiesFilePath = $this
->getDirPath() . '/' . self::ROOT_FILE_NAME . self::TYPE_EXTENSION;
$rootEntities = $this
->getDataFromFile($rootEntitiesFilePath);
if (!$rootEntities) {
$rootEntities = [];
}
$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);
}
public static function getEntityLabel(Entity $entity) {
return $entity
->label();
}
public function getExpotedDataTypeFilePath(Entity $entity) {
return $this
->getDirPath() . '/' . $entity
->getEntityTypeId() . self::TYPE_EXTENSION;
}
protected function getArchivePath() {
return $this
->getDirPath() . '/' . $this->slugifier
->slugify($this->id) . self::EXPORT_EXTENSION;
}
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();
}
public function getArchiveUri() {
$archivePath = $this
->getArchivePath();
if (file_exists($archivePath)) {
return $archivePath;
}
return FALSE;
}
public function getId() {
return $this->id;
}
}