ExportEntityWriter.php in Content Synchronizer 3.x
File
src/Processors/ExportEntityWriter.php
View source
<?php
namespace Drupal\content_synchronizer\Processors;
use Drupal\content_synchronizer\Base\JsonWriterTrait;
use Drupal\Core\Archiver\ArchiveTar;
use Drupal\Core\Entity\EntityInterface;
class ExportEntityWriter {
use JsonWriterTrait;
const TYPE_EXTENSION = '.json';
const EXPORT_EXTENSION = '.tar.gz';
const ROOT_FILE_NAME = 'root';
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 static $generatorDir = NULL;
protected $id;
public static function getEntityLabel(EntityInterface $entity) {
return $entity
->label();
}
public static function getGeneratorDir() {
if (!static::$generatorDir) {
static::$generatorDir = 'temporary://content_synchronizer';
try {
static::$generatorDir .= '_' . exec('whoami') . '/';
} catch (\Exception $e) {
static::$generatorDir .= '/';
}
}
return static::$generatorDir;
}
public function initFromId($id) {
$this->id = $id . '.' . time() . rand();
}
public function getDirPath() {
return static::getGeneratorDir() . 'export/' . $this->id;
}
public function write(EntityInterface $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(EntityInterface $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 function getExpotedDataTypeFilePath(EntityInterface $entity) {
return $this
->getDirPath() . '/' . $entity
->getEntityTypeId() . self::TYPE_EXTENSION;
}
protected function getArchivePath() {
return $this
->getDirPath() . self::EXPORT_EXTENSION;
}
public function archiveFiles() {
$fileSystem = \Drupal::service('file_system');
$path = $fileSystem
->realpath($this
->getDirPath());
$archivePath = $path . static::EXPORT_EXTENSION;
if (file_exists($archivePath)) {
$fileSystem
->delete($archivePath);
}
$archiver = new ArchiveTar($archivePath, 'gz');
$this
->addRepToArchive($path, '', $archiver);
}
public function addRepToArchive($repPath, $parent, ArchiveTar $archiver) {
$files = \scandir($repPath);
foreach ($files as $file) {
if (!in_array($file, [
'.',
'..',
])) {
if (is_dir($repPath . '/' . $file)) {
$this
->addRepToArchive($repPath . '/' . $file, $parent . '/' . $file, $archiver);
}
else {
$archiver
->addString($parent . '/' . $file, file_get_contents($repPath . '/' . $file));
}
}
}
}
public function getArchiveUri() {
$archivePath = $this
->getArchivePath();
if (file_exists($archivePath)) {
return $archivePath;
}
return FALSE;
}
public function getId() {
return $this->id;
}
}