WorkspaceMerger.php in Drupal 9
File
core/modules/workspaces/src/WorkspaceMerger.php
View source
<?php
namespace Drupal\workspaces;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class WorkspaceMerger implements WorkspaceMergerInterface {
protected $sourceWorkspace;
protected $targetWorkspace;
protected $entityTypeManager;
protected $database;
protected $workspaceAssociation;
protected $cacheTagsInvalidator;
public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, WorkspaceAssociationInterface $workspace_association, CacheTagsInvalidatorInterface $cache_tags_invalidator, WorkspaceInterface $source, WorkspaceInterface $target) {
$this->entityTypeManager = $entity_type_manager;
$this->database = $database;
$this->workspaceAssociation = $workspace_association;
$this->cacheTagsInvalidator = $cache_tags_invalidator;
$this->sourceWorkspace = $source;
$this->targetWorkspace = $target;
}
public function merge() {
if (!$this->sourceWorkspace
->hasParent() || $this->sourceWorkspace->parent->target_id != $this->targetWorkspace
->id()) {
throw new \InvalidArgumentException('The contents of a workspace can only be merged into its parent workspace.');
}
if ($this
->checkConflictsOnTarget()) {
throw new WorkspaceConflictException();
}
$transaction = $this->database
->startTransaction();
try {
foreach ($this
->getDifferringRevisionIdsOnSource() as $entity_type_id => $revision_difference) {
$revisions_on_source = $this->entityTypeManager
->getStorage($entity_type_id)
->loadMultipleRevisions(array_keys($revision_difference));
foreach ($revisions_on_source as $revision) {
$this->workspaceAssociation
->trackEntity($revision, $this->targetWorkspace);
}
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$this->cacheTagsInvalidator
->invalidateTags($entity_type
->getListCacheTags());
}
} catch (\Exception $e) {
$transaction
->rollBack();
watchdog_exception('workspaces', $e);
throw $e;
}
}
public function getSourceLabel() {
return $this->sourceWorkspace
->label();
}
public function getTargetLabel() {
return $this->targetWorkspace
->label();
}
public function checkConflictsOnTarget() {
}
public function getDifferringRevisionIdsOnTarget() {
$target_revision_difference = [];
$tracked_entities_on_source = $this->workspaceAssociation
->getTrackedEntities($this->sourceWorkspace
->id());
$tracked_entities_on_target = $this->workspaceAssociation
->getTrackedEntities($this->targetWorkspace
->id());
foreach ($tracked_entities_on_target as $entity_type_id => $tracked_revisions) {
if (!isset($tracked_entities_on_source[$entity_type_id])) {
$target_revision_difference[$entity_type_id] = $tracked_revisions;
}
elseif ($revision_difference = array_diff_key($tracked_revisions, $tracked_entities_on_source[$entity_type_id])) {
$target_revision_difference[$entity_type_id] = $revision_difference;
}
}
return $target_revision_difference;
}
public function getDifferringRevisionIdsOnSource() {
$source_revision_difference = [];
$tracked_entities_on_source = $this->workspaceAssociation
->getTrackedEntities($this->sourceWorkspace
->id());
$tracked_entities_on_target = $this->workspaceAssociation
->getTrackedEntities($this->targetWorkspace
->id());
foreach ($tracked_entities_on_source as $entity_type_id => $tracked_revisions) {
if (!isset($tracked_entities_on_target[$entity_type_id])) {
$source_revision_difference[$entity_type_id] = $tracked_revisions;
}
elseif ($revision_difference = array_diff_key($tracked_revisions, $tracked_entities_on_target[$entity_type_id])) {
$source_revision_difference[$entity_type_id] = $revision_difference;
}
}
return $source_revision_difference;
}
public function getNumberOfChangesOnTarget() {
$total_changes = $this
->getDifferringRevisionIdsOnTarget();
return count($total_changes, COUNT_RECURSIVE) - count($total_changes);
}
public function getNumberOfChangesOnSource() {
$total_changes = $this
->getDifferringRevisionIdsOnSource();
return count($total_changes, COUNT_RECURSIVE) - count($total_changes);
}
}