Change snipConent entitie property to public

This commit is contained in:
Tim
2025-07-24 15:03:20 +02:00
parent 074c1d8570
commit a1ecaf0189
8 changed files with 40 additions and 136 deletions

View File

@ -26,17 +26,16 @@ class SnipUpdateContentCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$io = new SymfonyStyle($input, $output); // $io = new SymfonyStyle($input, $output);
$qb = $this->snipContentRepository->createQueryBuilder('s'); $qb = $this->snipContentRepository->createQueryBuilder('s');
$qb->where('s.text IS NOT NULL'); $qb->where('s.text IS NOT NULL');
$c = 0;
/** @var SnipContent $snipContent */ /** @var SnipContent $snipContent */
foreach ($qb->getQuery()->getResult() as $snipContent) { foreach ($qb->getQuery()->getResult() as $snipContent) {
$text = $snipContent->getText(); $text = $snipContent->text;
$text = Lexer::reconstruct(Lexer::tokenize($text)); $text = Lexer::reconstruct(Lexer::tokenize($text));
$snipContent->setText($text); $snipContent->text = $text;
$this->snipContentRepository->save($snipContent); $this->snipContentRepository->save($snipContent);
} }

View File

@ -18,10 +18,10 @@ class SnipContentController extends AbstractController
#[Route('/compare/{to}/{from}', name: '_compare')] #[Route('/compare/{to}/{from}', name: '_compare')]
public function compare(SnipContent $to, ?SnipContent $from = null): Response public function compare(SnipContent $to, ?SnipContent $from = null): Response
{ {
$this->denyAccessUnlessGranted(SnipVoter::VIEW, $to->getSnip()); $this->denyAccessUnlessGranted(SnipVoter::VIEW, $to->snip);
if ($from === null) { if ($from === null) {
$from = $to->getParent(); $from = $to->parent;
} }
$diff = MyersDiff::buildDiffLines( $diff = MyersDiff::buildDiffLines(
@ -30,8 +30,8 @@ class SnipContentController extends AbstractController
); );
return $this->render('content/compare.html.twig', [ return $this->render('content/compare.html.twig', [
'snip' => $to->getSnip(), 'snip' => $to->snip,
'diff' => $diff, 'diff' => $diff,
]); ]);
} }
} }

View File

@ -33,7 +33,7 @@ class VersionController extends AbstractController
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
$this->contentService->setVersion($snip, $version); $this->contentService->setVersion($snip, $version);
$this->addFlash('success', 'Snip version set to ' . $version->getId()); $this->addFlash('success', 'Snip version set to ' . $version->id);
return $this->redirectToRoute('snip_single', ['snip' => $snip->id]); return $this->redirectToRoute('snip_single', ['snip' => $snip->id]);
} }
} }

View File

@ -66,7 +66,7 @@ class Snip
{ {
if (!$this->snipContents->contains($snipContent)) { if (!$this->snipContents->contains($snipContent)) {
$this->snipContents->add($snipContent); $this->snipContents->add($snipContent);
$snipContent->setSnip($this); $snipContent->snip = $this;
} }
return $this; return $this;
@ -76,8 +76,8 @@ class Snip
{ {
if ($this->snipContents->removeElement($snipContent)) { if ($this->snipContents->removeElement($snipContent)) {
// set the owning side to null (unless already changed) // set the owning side to null (unless already changed)
if ($snipContent->getSnip() === $this) { if ($snipContent->snip === $this) {
$snipContent->setSnip(null); $snipContent->snip = null;
} }
} }

View File

@ -17,124 +17,30 @@ class SnipContent
#[ORM\Column(type: UlidType::NAME, unique: true)] #[ORM\Column(type: UlidType::NAME, unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')] #[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'doctrine.ulid_generator')] #[ORM\CustomIdGenerator(class: 'doctrine.ulid_generator')]
private ?Ulid $id = null; public ?Ulid $id = null;
#[ORM\ManyToOne(inversedBy: 'snipContents')] #[ORM\ManyToOne(inversedBy: 'snipContents')]
#[ORM\JoinColumn(nullable: false)] #[ORM\JoinColumn(nullable: false)]
private ?Snip $snip = null; public ?Snip $snip = null;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
private ?self $parent = null; public ?self $parent = null;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)] /** @var Collection<int, self> */
private Collection $children; #[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')]
public Collection $children;
#[ORM\Column(type: Types::TEXT, nullable: true)] #[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $text = null; public ?string $text = null;
#[ORM\Column(nullable: true)] #[ORM\Column(nullable: true)]
private ?array $diff = null; public ?array $diff = null;
#[ORM\Column(length: 255, nullable: true)] #[ORM\Column(length: 255, nullable: true)]
private ?string $name = null; public ?string $name = null;
public function __construct() public function __construct()
{ {
$this->children = new ArrayCollection(); $this->children = new ArrayCollection();
} }
public function getId(): ?Ulid
{
return $this->id;
}
public function getSnip(): ?Snip
{
return $this->snip;
}
public function setSnip(?Snip $snip): self
{
$this->snip = $snip;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(self $child): self
{
if (!$this->children->contains($child)) {
$this->children->add($child);
$child->setParent($this);
}
return $this;
}
public function removeChild(self $child): self
{
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(?string $text): self
{
$this->text = $text;
return $this;
}
public function getDiff(): ?array
{
return $this->diff;
}
public function setDiff(?array $diff): static
{
$this->diff = $diff;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): static
{
$this->name = $name;
return $this;
}
} }

View File

@ -22,13 +22,12 @@ readonly class SnipContentService
// Create new snipContent entity with previous one as parent // Create new snipContent entity with previous one as parent
$content = new SnipContent(); $content = new SnipContent();
$content $content->text = $contents;
->setText($contents) $content->snip = $snip;
->setSnip($snip) $content->name = $contentName;
->setName($contentName)
;
if ($parentContent !== null) { if ($parentContent !== null) {
$content->setParent($parentContent); $content->parent = $parentContent;
$this->contentToRelative($parentContent); $this->contentToRelative($parentContent);
} }
@ -45,17 +44,17 @@ readonly class SnipContentService
if ($snipContent === null) { if ($snipContent === null) {
return ''; return '';
} }
if ($snipContent->getText()) { if ($snipContent->text) {
return $snipContent->getText(); return $snipContent->text;
} }
$parentContent = $snipContent->getParent(); $parentContent = $snipContent->parent;
if ($parentContent === null && $snipContent->getDiff() === null) { if ($parentContent === null && $snipContent->diff === null) {
return '---Something went very wrong, cant rebuild the text---'; return '---Something went very wrong, cant rebuild the text---';
} }
return MyersDiff::rebuildBFromCompact( return MyersDiff::rebuildBFromCompact(
self::rebuildText($parentContent), $snipContent->getDiff() self::rebuildText($parentContent), $snipContent->diff
); );
} }
@ -72,25 +71,25 @@ readonly class SnipContentService
public function contentToRelative(SnipContent $content): void public function contentToRelative(SnipContent $content): void
{ {
if ($content->getText() === null || $content->getParent() === null) { if ($content->text === null || $content->parent === null) {
return; return;
} }
$contentText = $content->getText(); $contentText = $content->text;
$parentText = self::rebuildText($content->getParent()); $parentText = self::rebuildText($content->parent);
$diff = MyersDiff::calculate($parentText, $contentText); $diff = MyersDiff::calculate($parentText, $contentText);
$content->setDiff($diff); $content->diff = $diff;
$content->setText(null); $content->text = null;
$this->em->persist($content); $this->em->persist($content);
$this->em->flush(); $this->em->flush();
} }
public function contentToAbsolute(SnipContent $content): void public function contentToAbsolute(SnipContent $content): void
{ {
if ($content->getDiff() === null) { if ($content->diff === null) {
return; return;
} }
$content->setText(self::rebuildText($content)); $content->text = self::rebuildText($content);
$content->setDiff(null); $content->diff = null;
$this->em->persist($content); $this->em->persist($content);
$this->em->flush(); $this->em->flush();
} }

View File

@ -37,7 +37,7 @@ class IncludeReferenceStage implements StageInterface
$content = null; $content = null;
} }
if ($content) { if ($content) {
$snip = $content->getSnip(); $snip = $content->snip;
} else { } else {
$snip = $this->snipRepository->find($id); $snip = $this->snipRepository->find($id);
if ($snip) { if ($snip) {

View File

@ -25,7 +25,7 @@ class SnipLoader implements LoaderInterface
public function getCacheKey(string $name): string public function getCacheKey(string $name): string
{ {
return $this->getFromKey($name)->activeVersion->getId(); return $this->getFromKey($name)->activeVersion->id;
} }
public function isFresh(string $name, int $time): bool public function isFresh(string $name, int $time): bool