Integrate snipContent more tightly into snip and cleanup old code

This commit is contained in:
Tim 2023-12-20 22:37:06 +01:00
parent 64bd7e3642
commit 5624fc3a74
9 changed files with 67 additions and 45 deletions

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231220204107 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}

View File

@ -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);

View File

@ -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()]);
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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();
}
}

View File

@ -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) {

View File

@ -31,8 +31,8 @@
</div>
<div class="card-footer">
<p class="card-text text-muted">
Current version: {{ activeVersion }}
{% if activeVersion == latestVersion %}(latest){% endif %}
Current version: {{ snip.activeVersion.id }}
{% if snip.activeVersion == snip.latestVersion %}(latest){% endif %}
</p>
</div>
</div>

View File

@ -6,14 +6,14 @@
<a href="{{ path('snip_single', {snip: snip.id}) }}" class="btn btn-primary">
<i class="fa fa-arrow-left"></i> Back
</a>
<a href="{{ path('version_set', {version: latestVersion, snip: snip.id}) }}" class="btn btn-warning">
<a href="{{ path('version_set', {version: snip.latestVersion.id, snip: snip.id}) }}" class="btn btn-warning">
<i class="fa fa-refresh"></i> Latest
</a>
<br><br>
<div class="list-group">
{% for version in versions %}
{% for version in snip.snipContents %}
<a class="list-group-item" href="{{ path('version_set', {version: version.id, snip: snip.id}) }}">
{{ version.name }} - {{ version.id }}
{{ version.id.dateTime|date('Y-m-d H:i:s') }} - {{ version.id }}
</a>
{% endfor %}
</div>