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 4 commits
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
25 changes: 21 additions & 4 deletions models/classes/resources/Service/ClassCopier.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,18 @@ public function copy(
private function doCopy(
core_kernel_classes_Class $class,
core_kernel_classes_Class $destinationClass,
bool $keepOriginalPermission = true
bool $keepOriginalPermission = true,
string $copyIdentifier = '',
tikhanovichA marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use null as default instead of empty string 🙂

Suggested change
string $copyIdentifier = '',
?string $copyIdentifier = null,

): core_kernel_classes_Class {
if (in_array($class->getUri(), $this->copiedClasses, true)) {
// Generate a unique identifier for the copy operation
if (!$copyIdentifier) {
$copyIdentifier = $this->generateCopyIdentifier($class->getUri(), $destinationClass->getUri());
}
// Prevent infinite recursion
if (
isset($this->copiedClasses[$copyIdentifier])
&& in_array($class->getUri(), $this->copiedClasses[$copyIdentifier], true)
) {
return $class;
}

Expand All @@ -107,7 +116,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 +143,7 @@ private function doCopy(
}

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

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

$this->assertionCompleted = true;
}

/**
* Generates a unique identifier for a copy operation
*/
private function generateCopyIdentifier(string $from, string $to): string
{
return hash('sha256', $from . '-' . $to);
}
}
Loading