class ExportEntityWriter in Content Synchronizer 3.x
Same name and namespace in other branches
- 8.2 src/Processors/ExportEntityWriter.php \Drupal\content_synchronizer\Processors\ExportEntityWriter
- 8 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
10 files declare their use of ExportEntityWriter
- ArchiveDownloader.php in src/
Service/ ArchiveDownloader.php - ArchiveDownloaderController.php in src/
Controller/ ArchiveDownloaderController.php - ContentSynchronizerManager.php in src/
Service/ ContentSynchronizerManager.php - EntityProcessorBase.php in src/
Processors/ Entity/ EntityProcessorBase.php - ExportConfirmForm.php in src/
Form/ ExportConfirmForm.php
File
- src/
Processors/ ExportEntityWriter.php, line 12
Namespace
Drupal\content_synchronizer\ProcessorsView source
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';
/**
* Generator dir.
*
* @var string
*/
protected static $generatorDir = NULL;
/**
* The id of the entity.
*
* @var string
*/
protected $id;
/**
* Return the entity label.
*/
public static function getEntityLabel(EntityInterface $entity) {
return $entity
->label();
}
/**
* Return the generator dir.
*
* @return string
* The generator dir.
*/
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;
}
/**
* THe id of the directory.
*/
public function initFromId($id) {
$this->id = $id . '.' . time() . rand();
}
/**
* Return the directory path.
*/
public function getDirPath() {
return static::getGeneratorDir() . 'export/' . $this->id;
}
/**
* Write the document to export.
*
* @param \Drupal\Core\Entity\EntityInterface $entityToExport
* The entity to export.
* @param array $dataToExport
* The data to export.
*/
public function write(EntityInterface $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\EntityInterface $entity
* The root entity to add.
*/
public function addRootEntity(EntityInterface $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 file path of the type of the entity.
*/
public function getExpotedDataTypeFilePath(EntityInterface $entity) {
return $this
->getDirPath() . '/' . $entity
->getEntityTypeId() . self::TYPE_EXTENSION;
}
/**
* Return the archive path.
*/
protected function getArchivePath() {
return $this
->getDirPath() . self::EXPORT_EXTENSION;
}
/**
* Zip the generated files.
*/
public function archiveFiles() {
/** @var \Drupal\Core\File\FileSystem $fileSystem */
$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);
}
/**
* Add files to archive recursively.
*
* @param string $repPath
* The repertory path.
* @param string $parent
* The parent.
* @param \Drupal\Core\Archiver\ArchiveTar $archiver
* The 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));
}
}
}
}
/**
* 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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExportEntityWriter:: |
protected static | property | Generator dir. | |
ExportEntityWriter:: |
protected | property | The id of the entity. | |
ExportEntityWriter:: |
public | function | Add files to archive recursively. | |
ExportEntityWriter:: |
public | function | Add the entity to the exported root entities list. | |
ExportEntityWriter:: |
public | function | Zip the generated files. | |
ExportEntityWriter:: |
constant | |||
ExportEntityWriter:: |
constant | |||
ExportEntityWriter:: |
constant | |||
ExportEntityWriter:: |
constant | |||
ExportEntityWriter:: |
constant | |||
ExportEntityWriter:: |
constant | |||
ExportEntityWriter:: |
constant | |||
ExportEntityWriter:: |
protected | function | Return the archive path. | |
ExportEntityWriter:: |
public | function | Return the archive uri. | |
ExportEntityWriter:: |
public | function | Return the directory path. | |
ExportEntityWriter:: |
public static | function | Return the entity label. | |
ExportEntityWriter:: |
public | function | Get the entity Type data already exported. | |
ExportEntityWriter:: |
public | function | Return the file path of the type of the entity. | |
ExportEntityWriter:: |
public static | function | Return the generator dir. | |
ExportEntityWriter:: |
public | function | The writer id. | |
ExportEntityWriter:: |
public | function | THe id of the directory. | |
ExportEntityWriter:: |
constant | |||
ExportEntityWriter:: |
constant | |||
ExportEntityWriter:: |
public | function | Write the document to export. | |
JsonWriterTrait:: |
protected | property | File System. | |
JsonWriterTrait:: |
protected | function | Create a directory if not exists. | |
JsonWriterTrait:: |
protected | function | Create a directory tree. | |
JsonWriterTrait:: |
public | function | Return file system. | |
JsonWriterTrait:: |
protected | function | Get json decode data from a file. | |
JsonWriterTrait:: |
protected | function | Save json in the destination file. |