AllDocs.php in Replication 8
File
src/AllDocs/AllDocs.php
View source
<?php
namespace Drupal\replication\AllDocs;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\multiversion\Entity\Index\EntityIndexInterface;
use Drupal\multiversion\Entity\WorkspaceInterface;
use Drupal\multiversion\MultiversionManagerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
class AllDocs implements AllDocsInterface {
use DependencySerializationTrait;
protected $entityTypeManager;
protected $multiversionManager;
protected $workspace;
protected $entityIndex;
protected $serializer;
protected $includeDocs = FALSE;
protected $limit = NULL;
protected $skip = 0;
protected $descending = FALSE;
protected $startKey;
protected $endKey;
protected $inclusiveEnd = TRUE;
public function __construct(EntityTypeManagerInterface $entity_type_manager, MultiversionManagerInterface $multiversion_manager, WorkspaceInterface $workspace, EntityIndexInterface $entity_index, SerializerInterface $serializer) {
$this->entityTypeManager = $entity_type_manager;
$this->multiversionManager = $multiversion_manager;
$this->workspace = $workspace;
$this->entityIndex = $entity_index;
$this->serializer = $serializer;
}
public function includeDocs($include_docs) {
$this->includeDocs = (bool) $include_docs;
}
public function limit($limit) {
$this->limit = $limit;
}
public function skip($skip) {
$this->skip = $skip;
}
public function descending($descending) {
$this->descending = $descending;
}
public function startKey($key) {
$this->startKey = $key;
}
public function endKey($key) {
$this->endKey = $key;
}
public function inclusiveEnd($inclusive_end) {
$this->inclusiveEnd = $inclusive_end;
}
public function execute() {
$rows = [];
$entity_types = $this->entityTypeManager
->getDefinitions();
foreach ($entity_types as $entity_type_id => $entity_type) {
if ($this->multiversionManager
->isEnabledEntityType($entity_type) && !$entity_type
->get('local')) {
try {
$query = $this->entityTypeManager
->getStorage($entity_type_id)
->getQuery();
if ($entity_type
->get('workspace') !== FALSE) {
$query
->condition('workspace', $this->workspace
->id());
}
$ids = $query
->execute();
} catch (\Exception $e) {
watchdog_exception('replication', $e);
continue;
}
$keys = [];
foreach ($ids as $id) {
$keys[] = $entity_type_id . ':' . $id;
}
$items = $this->entityIndex
->getMultiple($keys);
foreach ($items as $item) {
if ($item['is_stub'] == TRUE) {
continue;
}
$rows[$item['uuid']] = [
'rev' => $item['rev'],
];
}
if ($this->includeDocs) {
$entities = $this->entityTypeManager
->getStorage($entity_type_id)
->loadMultiple($ids);
foreach ($entities as $entity) {
if ($entity->_rev->is_stub) {
continue;
}
$rows[$entity
->uuid()]['doc'] = $this->serializer
->normalize($entity, 'json');
}
}
}
}
return $rows;
}
}