Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix copy of copied classes #3857

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions models/classes/resources/Service/ClassCopier.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public function transfer(ResourceTransferCommand $command): ResourceTransferResu
{
$class = $this->ontology->getClass($command->getFrom());
$destinationClass = $this->ontology->getClass($command->getTo());
$newClass = $this->doCopy($class, $destinationClass, $command->keepOriginalAcl());
$copyIdentifier = $this->generateCopyIdentifier($class->getUri(), $destinationClass->getUri());
$newClass = $this->doCopy($class, $destinationClass, $copyIdentifier, $command->keepOriginalAcl());

return new ResourceTransferResult($newClass->getUri());
}
Expand All @@ -90,15 +91,21 @@ public function copy(
core_kernel_classes_Class $class,
core_kernel_classes_Class $destinationClass
): core_kernel_classes_Class {
return $this->doCopy($class, $destinationClass);
$copyIdentifier = $this->generateCopyIdentifier($class->getUri(), $destinationClass->getUri());
return $this->doCopy($class, $destinationClass, $copyIdentifier);
tikhanovichA marked this conversation as resolved.
Show resolved Hide resolved
}

private function doCopy(
core_kernel_classes_Class $class,
core_kernel_classes_Class $destinationClass,
string $copyIdentifier,
bool $keepOriginalPermission = true
): core_kernel_classes_Class {
if (in_array($class->getUri(), $this->copiedClasses, true)) {
// Prevent infinite recursion
if (
isset($this->copiedClasses[$copyIdentifier])
&& in_array($class->getUri(), $this->copiedClasses[$copyIdentifier], true)
) {
return $class;
}

Expand All @@ -107,7 +114,7 @@ private function doCopy(
$newClass = $destinationClass->createSubClass($class->getLabel());
$newClassUri = $newClass->getUri();

$this->copiedClasses[] = $newClassUri;
$this->copiedClasses[$copyIdentifier][] = $newClassUri;

$this->classMetadataCopier->copy($class, $newClass);

Expand All @@ -134,7 +141,7 @@ private function doCopy(
}

foreach ($class->getSubClasses() as $subClass) {
$this->doCopy($subClass, $newClass, $keepOriginalPermission);
$this->doCopy($subClass, $newClass, $copyIdentifier, $keepOriginalPermission);
}

$this->classMetadataMapper->remove($newClass->getProperties());
Expand Down Expand Up @@ -169,4 +176,15 @@ private function assertInSameRootClass(

$this->assertionCompleted = true;
}

/**
* Generates a unique identifier for a copy operation.
* @param string $from
* @param string $to
* @return string
tikhanovichA marked this conversation as resolved.
Show resolved Hide resolved
*/
private function generateCopyIdentifier(string $from, string $to): string
{
return hash('sha256', $from . '-' . $to);
}
}
Loading