Integrate snipContent more tightly into snip and cleanup old code
This commit is contained in:
parent
64bd7e3642
commit
5624fc3a74
35
migrations/Version20231220204107.php
Normal file
35
migrations/Version20231220204107.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
@ -53,8 +53,6 @@ class SnipController extends AbstractController
|
|||||||
return $this->render('snip/single.html.twig', [
|
return $this->render('snip/single.html.twig', [
|
||||||
'snip' => $snip,
|
'snip' => $snip,
|
||||||
'content' => $pl->parse($snipService->getActiveText()),
|
'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
|
public function new(Request $request): Response
|
||||||
{
|
{
|
||||||
$snip = new Snip();
|
$snip = new Snip();
|
||||||
$snip->setCreatedAtTodayNoSeconds()
|
$snip->setCreatedAtNow()
|
||||||
->setCreatedBy($this->getUser());
|
->setCreatedBy($this->getUser());
|
||||||
|
|
||||||
return $this->edit($snip, $request);
|
return $this->edit($snip, $request);
|
||||||
|
@ -21,12 +21,9 @@ class VersionController extends AbstractController
|
|||||||
public function index(Snip $snip): Response
|
public function index(Snip $snip): Response
|
||||||
{
|
{
|
||||||
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
|
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
|
||||||
|
|
||||||
$snipService = $this->snipServiceFactory->create($snip);
|
|
||||||
return $this->render('version/index.html.twig', [
|
return $this->render('version/index.html.twig', [
|
||||||
'snip' => $snip,
|
'snip' => $snip,
|
||||||
'versions' => $snipService->getVersions(),
|
|
||||||
'latestVersion' => $snipService->getLatestVersion(),
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +32,7 @@ class VersionController extends AbstractController
|
|||||||
{
|
{
|
||||||
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
|
$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());
|
$this->addFlash('success', 'Snip version set to ' . $version->getId());
|
||||||
return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]);
|
return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]);
|
||||||
}
|
}
|
||||||
|
@ -40,10 +40,17 @@ trait TrackedTrait
|
|||||||
return $this;
|
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')));
|
$this->setCreatedAt(DateTime::createFromFormat('Y-m-d H:i', date('Y-m-d H:i')));
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setCreatedAtNow(): self
|
||||||
|
{
|
||||||
|
$this->setCreatedAt(new DateTime());
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
@ -27,8 +27,8 @@ class Snip
|
|||||||
#[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)]
|
#[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)]
|
||||||
private Collection $snipContents;
|
private Collection $snipContents;
|
||||||
|
|
||||||
#[ORM\Column(length: 255, nullable: true)]
|
#[ORM\ManyToOne]
|
||||||
private ?string $activeCommit = null;
|
private ?SnipContent $activeVersion = null;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
@ -99,14 +99,19 @@ class Snip
|
|||||||
return $this;
|
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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ readonly class SnipContentService
|
|||||||
$this->em->persist($content);
|
$this->em->persist($content);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
|
|
||||||
$this->snip->setActiveCommit($content->getId());
|
$this->snip->setActiveVersion($content->getId());
|
||||||
$this->em->persist($this->snip);
|
$this->em->persist($this->snip);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
}
|
}
|
||||||
@ -37,38 +37,18 @@ readonly class SnipContentService
|
|||||||
public function getActiveText(): string
|
public function getActiveText(): string
|
||||||
{
|
{
|
||||||
$contentRepo = $this->em->getRepository(SnipContent::class);
|
$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 setVersion(SnipContent $version): void
|
||||||
public function getVersions(): array
|
|
||||||
{
|
{
|
||||||
// Return all snipContent entities (by parent)
|
$this->snip->setActiveVersion($version);
|
||||||
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->em->persist($this->snip);
|
$this->em->persist($this->snip);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getActiveVersion(): string
|
|
||||||
{
|
|
||||||
return $this->snip->getActiveCommit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function delete(): void
|
public function delete(): void
|
||||||
{
|
{
|
||||||
// Cleanup the versions
|
// Cleanup the versions
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLatestVersion(): string
|
|
||||||
{
|
|
||||||
return $this->snip->getSnipContents()->last()->getId();
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -41,7 +41,7 @@ class IncludeReferenceStage implements StageInterface
|
|||||||
} else {
|
} else {
|
||||||
$snip = $this->snipRepository->find($id);
|
$snip = $this->snipRepository->find($id);
|
||||||
if ($snip) {
|
if ($snip) {
|
||||||
$content = $this->snipContentRepository->find($snip->getActiveCommit());
|
$content = $this->snipContentRepository->find($snip->getActiveVersion());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($content === null) {
|
if ($content === null) {
|
||||||
|
@ -31,8 +31,8 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="card-footer">
|
<div class="card-footer">
|
||||||
<p class="card-text text-muted">
|
<p class="card-text text-muted">
|
||||||
Current version: {{ activeVersion }}
|
Current version: {{ snip.activeVersion.id }}
|
||||||
{% if activeVersion == latestVersion %}(latest){% endif %}
|
{% if snip.activeVersion == snip.latestVersion %}(latest){% endif %}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -6,14 +6,14 @@
|
|||||||
<a href="{{ path('snip_single', {snip: snip.id}) }}" class="btn btn-primary">
|
<a href="{{ path('snip_single', {snip: snip.id}) }}" class="btn btn-primary">
|
||||||
<i class="fa fa-arrow-left"></i> Back
|
<i class="fa fa-arrow-left"></i> Back
|
||||||
</a>
|
</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
|
<i class="fa fa-refresh"></i> Latest
|
||||||
</a>
|
</a>
|
||||||
<br><br>
|
<br><br>
|
||||||
<div class="list-group">
|
<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}) }}">
|
<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>
|
</a>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user