class Exporter in Tome 8
Handles exporting of content and file entities.
@internal
Hierarchy
- class \Drupal\tome_sync\Exporter implements ExporterInterface uses PathTrait, AccountSwitcherTrait, ContentIndexerTrait
Expanded class hierarchy of Exporter
1 string reference to 'Exporter'
- tome_sync.services.yml in modules/
tome_sync/ tome_sync.services.yml - modules/tome_sync/tome_sync.services.yml
1 service uses Exporter
- tome_sync.exporter in modules/
tome_sync/ tome_sync.services.yml - Drupal\tome_sync\Exporter
File
- modules/
tome_sync/ src/ Exporter.php, line 22
Namespace
Drupal\tome_syncView source
class Exporter implements ExporterInterface {
use PathTrait;
use ContentIndexerTrait;
use AccountSwitcherTrait;
/**
* The target content storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $contentStorage;
/**
* The serializer.
*
* @var \Symfony\Component\Serializer\Serializer
*/
protected $serializer;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The file system.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The file sync service.
*
* @var \Drupal\tome_sync\FileSyncInterface
*/
protected $fileSync;
/**
* An array of excluded entity types.
*
* @var string[]
*/
protected static $excludedTypes = [
'content_moderation_state',
];
/**
* Creates an Exporter object.
*
* @param \Drupal\Core\Config\StorageInterface $content_storage
* The target content storage.
* @param \Symfony\Component\Serializer\Serializer $serializer
* The serializer.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\Core\Session\AccountSwitcherInterface $account_switcher
* The account switcher.
* @param \Drupal\tome_sync\FileSyncInterface $file_sync
* The file sync service.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system.
*/
public function __construct(StorageInterface $content_storage, Serializer $serializer, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, AccountSwitcherInterface $account_switcher, FileSyncInterface $file_sync, FileSystemInterface $file_system) {
$this->contentStorage = $content_storage;
$this->serializer = $serializer;
$this->entityTypeManager = $entity_type_manager;
$this->eventDispatcher = $event_dispatcher;
$this->accountSwitcher = $account_switcher;
$this->fileSync = $file_sync;
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*/
public function getContentToExport() {
$entities = [];
$definitions = array_diff_key($this->entityTypeManager
->getDefinitions(), array_flip(static::$excludedTypes));
foreach ($definitions as $entity_type) {
if (is_a($entity_type
->getClass(), '\\Drupal\\Core\\Entity\\ContentEntityInterface', TRUE)) {
$storage = $this->entityTypeManager
->getStorage($entity_type
->id());
$entities[$entity_type
->id()] = $storage
->getQuery()
->execute();
}
}
return $entities;
}
/**
* {@inheritdoc}
*/
public function deleteExportDirectories() {
$this->contentStorage
->deleteAll();
$this
->deleteContentIndex();
if (!$this->fileSync
->deleteExportDirectory()) {
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function exportContent(ContentEntityInterface $entity) {
if (in_array($entity
->getEntityTypeId(), static::$excludedTypes, TRUE)) {
return;
}
$this
->switchToAdmin();
$data = $this->serializer
->normalize($entity, 'json');
$this->contentStorage
->write(TomeSyncHelper::getContentName($entity), $data);
$this
->indexContent($entity);
if ($entity instanceof FileInterface) {
$this->fileSync
->exportFile($entity);
}
$event = new ContentCrudEvent($entity);
$this->eventDispatcher
->dispatch(TomeSyncEvents::EXPORT_CONTENT, $event);
$this
->switchBack();
}
/**
* {@inheritdoc}
*/
public function deleteContentExport(ContentEntityInterface $entity) {
// It would be cool if hook_entity_translation_delete() is invoked for
// every translation of an entity when it's deleted. But it isn't. :-(.
foreach (array_keys($entity
->getTranslationLanguages()) as $langcode) {
$this->contentStorage
->delete(TomeSyncHelper::getContentName($entity
->getTranslation($langcode)));
$this
->unIndexContent($entity);
}
if ($entity instanceof FileInterface) {
$this->fileSync
->deleteFileExport($entity);
}
$event = new ContentCrudEvent($entity);
$this->eventDispatcher
->dispatch(TomeSyncEvents::DELETE_CONTENT, $event);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AccountSwitcherTrait:: |
protected | property | The account switcher. | |
AccountSwitcherTrait:: |
protected | function | Switches the current user back. | |
AccountSwitcherTrait:: |
protected | function | Switches the current user to the admin. | |
ContentIndexerTrait:: |
protected | function | Acquires a lock for writing to the index. | |
ContentIndexerTrait:: |
protected | function | Deletes the index file. | |
ContentIndexerTrait:: |
protected | function | Gets the contents of the index. | |
ContentIndexerTrait:: |
protected | function | Gets the index file path. | |
ContentIndexerTrait:: |
protected | function | Writes content to the index. | |
ContentIndexerTrait:: |
protected | function | Removes content from the index. | |
ContentIndexerTrait:: |
protected | function | Removes content from the index. | |
Exporter:: |
protected | property | The target content storage. | |
Exporter:: |
protected | property | The entity type manager. | |
Exporter:: |
protected | property | The event dispatcher. | |
Exporter:: |
protected static | property | An array of excluded entity types. | |
Exporter:: |
protected | property | The file sync service. | |
Exporter:: |
protected | property | The file system. | |
Exporter:: |
protected | property | The serializer. | |
Exporter:: |
public | function |
Deletes an exported content entity. Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Deletes all content and files from the export directories. Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Exports a content entity to the target storage. Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Grabs a list of content to export. Overrides ExporterInterface:: |
|
Exporter:: |
public | function | Creates an Exporter object. | |
PathTrait:: |
protected | function | Joins multiple paths. |