diff --git a/migrations/Version20231220204107.php b/migrations/Version20231220204107.php new file mode 100644 index 0000000..cb91e17 --- /dev/null +++ b/migrations/Version20231220204107.php @@ -0,0 +1,35 @@ +addSql('ALTER TABLE snip ADD active_version_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:ulid)\', DROP active_commit'); + $this->addSql('ALTER TABLE snip ADD CONSTRAINT FK_FEBD97966A1E45F3 FOREIGN KEY (active_version_id) REFERENCES snip_content (id)'); + $this->addSql('CREATE INDEX IDX_FEBD97966A1E45F3 ON snip (active_version_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE snip DROP FOREIGN KEY FK_FEBD97966A1E45F3'); + $this->addSql('DROP INDEX IDX_FEBD97966A1E45F3 ON snip'); + $this->addSql('ALTER TABLE snip ADD active_commit VARCHAR(255) DEFAULT NULL, DROP active_version_id'); + } +} diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index 4e07730..0bf9aab 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -53,8 +53,6 @@ class SnipController extends AbstractController return $this->render('snip/single.html.twig', [ 'snip' => $snip, 'content' => $pl->parse($snipService->getActiveText()), - 'activeVersion' => $snipService->getActiveVersion(), - 'latestVersion' => $snipService->getLatestVersion(), ]); } @@ -114,7 +112,7 @@ class SnipController extends AbstractController public function new(Request $request): Response { $snip = new Snip(); - $snip->setCreatedAtTodayNoSeconds() + $snip->setCreatedAtNow() ->setCreatedBy($this->getUser()); return $this->edit($snip, $request); diff --git a/src/Controller/VersionController.php b/src/Controller/VersionController.php index 56ee259..046be9a 100644 --- a/src/Controller/VersionController.php +++ b/src/Controller/VersionController.php @@ -21,12 +21,9 @@ class VersionController extends AbstractController public function index(Snip $snip): Response { $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); - - $snipService = $this->snipServiceFactory->create($snip); + return $this->render('version/index.html.twig', [ 'snip' => $snip, - 'versions' => $snipService->getVersions(), - 'latestVersion' => $snipService->getLatestVersion(), ]); } @@ -35,7 +32,7 @@ class VersionController extends AbstractController { $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); - $this->snipServiceFactory->create($snip)->setVersion($version->getId()); + $this->snipServiceFactory->create($snip)->setVersion($version); $this->addFlash('success', 'Snip version set to ' . $version->getId()); return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]); } diff --git a/src/Entity/Helpers/TrackedTrait.php b/src/Entity/Helpers/TrackedTrait.php index 1fdd5f6..6cdf453 100644 --- a/src/Entity/Helpers/TrackedTrait.php +++ b/src/Entity/Helpers/TrackedTrait.php @@ -40,10 +40,17 @@ trait TrackedTrait return $this; } - public function setCreatedAtTodayNoSeconds(): self + public function setCreatedAtNowNoSeconds(): self { $this->setCreatedAt(DateTime::createFromFormat('Y-m-d H:i', date('Y-m-d H:i'))); return $this; } + + public function setCreatedAtNow(): self + { + $this->setCreatedAt(new DateTime()); + + return $this; + } } \ No newline at end of file diff --git a/src/Entity/Snip.php b/src/Entity/Snip.php index 19824aa..bd4e99f 100644 --- a/src/Entity/Snip.php +++ b/src/Entity/Snip.php @@ -27,8 +27,8 @@ class Snip #[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)] private Collection $snipContents; - #[ORM\Column(length: 255, nullable: true)] - private ?string $activeCommit = null; + #[ORM\ManyToOne] + private ?SnipContent $activeVersion = null; public function __construct() { @@ -99,14 +99,19 @@ class Snip return $this; } - public function getActiveCommit(): ?string + public function getLatestVersion(): ?SnipContent { - return $this->activeCommit; + return $this->snipContents->last(); } - public function setActiveCommit(?string $activeCommit): static + public function getActiveVersion(): ?SnipContent { - $this->activeCommit = $activeCommit; + return $this->activeVersion; + } + + public function setActiveVersion(?SnipContent $activeVersion): static + { + $this->activeVersion = $activeVersion; return $this; } diff --git a/src/Service/SnipContent/SnipContentService.php b/src/Service/SnipContent/SnipContentService.php index f013060..53e73b8 100644 --- a/src/Service/SnipContent/SnipContentService.php +++ b/src/Service/SnipContent/SnipContentService.php @@ -28,7 +28,7 @@ readonly class SnipContentService $this->em->persist($content); $this->em->flush(); - $this->snip->setActiveCommit($content->getId()); + $this->snip->setActiveVersion($content->getId()); $this->em->persist($this->snip); $this->em->flush(); } @@ -37,38 +37,18 @@ readonly class SnipContentService public function getActiveText(): string { $contentRepo = $this->em->getRepository(SnipContent::class); - return $contentRepo->find($this->snip->getActiveCommit())->getText(); + return $contentRepo->find($this->snip->getActiveVersion())->getText(); } - /** @return array{id: string, name: string} */ - public function getVersions(): array + public function setVersion(SnipContent $version): void { - // Return all snipContent entities (by parent) - return array_map(fn(SnipContent $content) => [ - 'id' => (string)$content->getId(), - 'name' => $content->getId()->getDateTime()->format('Y-m-d H:i:s'), - ], $this->snip->getSnipContents()->toArray()); - } - - public function setVersion(string $version): void - { - $this->snip->setActiveCommit($version); + $this->snip->setActiveVersion($version); $this->em->persist($this->snip); $this->em->flush(); } - public function getActiveVersion(): string - { - return $this->snip->getActiveCommit(); - } - public function delete(): void { // Cleanup the versions } - - public function getLatestVersion(): string - { - return $this->snip->getSnipContents()->last()->getId(); - } } \ No newline at end of file diff --git a/src/Service/SnipParser/Stages/IncludeReferenceStage.php b/src/Service/SnipParser/Stages/IncludeReferenceStage.php index 0405c4d..db887f7 100644 --- a/src/Service/SnipParser/Stages/IncludeReferenceStage.php +++ b/src/Service/SnipParser/Stages/IncludeReferenceStage.php @@ -41,7 +41,7 @@ class IncludeReferenceStage implements StageInterface } else { $snip = $this->snipRepository->find($id); if ($snip) { - $content = $this->snipContentRepository->find($snip->getActiveCommit()); + $content = $this->snipContentRepository->find($snip->getActiveVersion()); } } if ($content === null) { diff --git a/templates/snip/single.html.twig b/templates/snip/single.html.twig index eca4897..e86d88d 100644 --- a/templates/snip/single.html.twig +++ b/templates/snip/single.html.twig @@ -31,8 +31,8 @@ diff --git a/templates/version/index.html.twig b/templates/version/index.html.twig index 2b142cc..aeeaced 100644 --- a/templates/version/index.html.twig +++ b/templates/version/index.html.twig @@ -6,14 +6,14 @@ Back - + Latest

- {% for version in versions %} + {% for version in snip.snipContents %} - {{ version.name }} - {{ version.id }} + {{ version.id.dateTime|date('Y-m-d H:i:s') }} - {{ version.id }} {% endfor %}