View source
<?php
namespace Drupal\default_content;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\InfoParserInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\default_content\Event\DefaultContentEvents;
use Drupal\default_content\Event\ExportEvent;
use Drupal\hal\LinkManager\LinkManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Serializer;
class Exporter implements ExporterInterface {
protected $linkDomain;
protected $serializer;
protected $entityTypeManager;
protected $entityRepository;
protected $moduleHandler;
protected $infoParser;
protected $linkManager;
protected $eventDispatcher;
protected $accountSwitcher;
protected $fileSystem;
public function __construct(Serializer $serializer, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, LinkManagerInterface $link_manager, EventDispatcherInterface $event_dispatcher, ModuleHandlerInterface $module_handler, InfoParserInterface $info_parser, $link_domain, AccountSwitcherInterface $account_switcher, FileSystemInterface $file_system) {
$this->serializer = $serializer;
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
$this->linkManager = $link_manager;
$this->eventDispatcher = $event_dispatcher;
$this->moduleHandler = $module_handler;
$this->infoParser = $info_parser;
$this->linkDomain = $link_domain;
$this->accountSwitcher = $account_switcher;
$this->fileSystem = $file_system;
}
public function exportContent($entity_type_id, $entity_id) {
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$entity = $storage
->load($entity_id);
if (!$entity) {
throw new \InvalidArgumentException(sprintf('Entity "%s" with ID "%s" does not exist', $entity_type_id, $entity_id));
}
if (!$entity instanceof ContentEntityInterface) {
throw new \InvalidArgumentException(sprintf('Entity "%s" with ID "%s" is not a content entity', $entity_type_id, $entity_id));
}
if ($this
->isCli()) {
$root_user = $this->entityTypeManager
->getStorage('user')
->load(1);
$this->accountSwitcher
->switchTo($root_user);
}
$this->linkManager
->setLinkDomain($this->linkDomain);
$return = $this->serializer
->serialize($entity, 'hal_json', [
'json_encode_options' => JSON_PRETTY_PRINT,
]);
$this->eventDispatcher
->dispatch(DefaultContentEvents::EXPORT, new ExportEvent($entity));
$this->linkManager
->setLinkDomain(FALSE);
if ($this
->isCli()) {
$this->accountSwitcher
->switchBack();
}
return $return;
}
public function exportContentWithReferences($entity_type_id, $entity_id) {
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$entity = $storage
->load($entity_id);
if (!$entity) {
throw new \InvalidArgumentException(sprintf('Entity "%s" with ID "%s" does not exist', $entity_type_id, $entity_id));
}
if (!$entity instanceof ContentEntityInterface) {
throw new \InvalidArgumentException(sprintf('Entity "%s" with ID "%s" is not a content entity', $entity_type_id, $entity_id));
}
$entities = [
$entity
->uuid() => $entity,
];
$entities = $this
->getEntityReferencesRecursive($entity, 0, $entities);
if ($this
->isCli()) {
$root_user = $this->entityTypeManager
->getStorage('user')
->load(1);
$this->accountSwitcher
->switchTo($root_user);
}
$this->linkManager
->setLinkDomain($this->linkDomain);
$serialized_entities_per_type = [];
foreach ($entities as $entity) {
$serialized_entities_per_type[$entity
->getEntityTypeId()][$entity
->uuid()] = $this->serializer
->serialize($entity, 'hal_json', [
'json_encode_options' => JSON_PRETTY_PRINT,
]);
}
$this->linkManager
->setLinkDomain(FALSE);
if ($this
->isCli()) {
$this->accountSwitcher
->switchBack();
}
return $serialized_entities_per_type;
}
public function exportModuleContent($module_name) {
$info_file = $this->moduleHandler
->getModule($module_name)
->getPathname();
$info = $this->infoParser
->parse($info_file);
$exported_content = [];
if (empty($info['default_content'])) {
return $exported_content;
}
foreach ($info['default_content'] as $entity_type => $uuids) {
foreach ($uuids as $uuid) {
$entity = $this->entityRepository
->loadEntityByUuid($entity_type, $uuid);
if (!$entity) {
throw new \InvalidArgumentException(sprintf('Entity "%s" with UUID "%s" does not exist', $entity_type, $uuid));
}
$exported_content[$entity_type][$uuid] = $this
->exportContent($entity_type, $entity
->id());
}
}
return $exported_content;
}
public function writeDefaultContent(array $serialized_by_type, $folder) {
foreach ($serialized_by_type as $entity_type => $serialized_entities) {
$entity_type_folder = "{$folder}/{$entity_type}";
$this
->prepareDirectory($entity_type_folder);
foreach ($serialized_entities as $uuid => $serialized_entity) {
$this
->putFile($entity_type_folder, $uuid, $serialized_entity);
}
}
}
protected function prepareDirectory($path) {
$this->fileSystem
->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
}
protected function putFile($path, $uuid, $serialized_entity) {
file_put_contents($path . '/' . $uuid . '.json', $serialized_entity);
}
protected function getEntityReferencesRecursive(ContentEntityInterface $entity, $depth = 0, array &$indexed_dependencies = []) {
$entity_dependencies = $entity
->referencedEntities();
foreach ($entity_dependencies as $dependent_entity) {
if (!$dependent_entity instanceof ContentEntityInterface) {
continue;
}
$key = $dependent_entity
->uuid();
if (isset($indexed_dependencies[$key])) {
continue;
}
$indexed_dependencies[$key] = $dependent_entity;
if ($depth < 6) {
$indexed_dependencies += $this
->getEntityReferencesRecursive($dependent_entity, $depth + 1, $indexed_dependencies);
}
}
return $indexed_dependencies;
}
protected function isCli() {
return PHP_SAPI === 'cli';
}
}