Fully implement snip db storage

This commit is contained in:
Tim 2023-12-17 01:55:26 +01:00
parent 0fce8ee4fe
commit 6107f560e2
11 changed files with 89 additions and 57 deletions

View File

@ -4,6 +4,8 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
snipStorageType: 'db' # 'db' or 'git
gitStoragePath: '%kernel.project_dir%/var/snips'
services:
# default configuration for services in *this* file
@ -26,4 +28,5 @@ services:
App\Service\SnipServiceFactory:
arguments:
- '%kernel.project_dir%/var/snips'
$gitStoragePath: '%gitStoragePath%'
$storageType: '%snipStorageType%'

View File

@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230419134540 extends AbstractMigration
final class Version20231217002445 extends AbstractMigration
{
public function getDescription(): string
{
@ -20,10 +20,10 @@ final class Version20230419134540 extends AbstractMigration
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE snip_content (id INT AUTO_INCREMENT NOT NULL, snip_id INT NOT NULL, parent_id INT DEFAULT NULL, text LONGTEXT DEFAULT NULL, INDEX IDX_185DCA87140FD260 (snip_id), INDEX IDX_185DCA87727ACA70 (parent_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE snip_content (id BINARY(16) NOT NULL COMMENT \'(DC2Type:ulid)\', snip_id INT NOT NULL, parent_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:ulid)\', text LONGTEXT DEFAULT NULL, INDEX IDX_185DCA87140FD260 (snip_id), INDEX IDX_185DCA87727ACA70 (parent_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE snip_content ADD CONSTRAINT FK_185DCA87140FD260 FOREIGN KEY (snip_id) REFERENCES snip (id)');
$this->addSql('ALTER TABLE snip_content ADD CONSTRAINT FK_185DCA87727ACA70 FOREIGN KEY (parent_id) REFERENCES snip_content (id)');
$this->addSql('ALTER TABLE user CHANGE roles roles LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\'');
$this->addSql('ALTER TABLE user CHANGE roles roles JSON NOT NULL COMMENT \'(DC2Type:json)\'');
}
public function down(Schema $schema): void

View File

@ -14,28 +14,28 @@ class HistoryController extends AbstractController
{
public function __construct(
private readonly SnipServiceFactory $snipServiceFactory,
)
{
}
) {}
#[Route('/', name: '_index')]
public function index(Snip $snip): Response
{
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
$snipService = $this->snipServiceFactory->create($snip);
return $this->render('history/index.html.twig', [
'snip' => $snip,
'commits' => $this->snipServiceFactory->createGit($snip)->getHistory(),
'versions' => $snipService->getVersions(),
'latestVersion' => $snipService->getLatestVersion(),
]);
}
#[Route('/set/{commit}', name: '_set')]
public function set(Snip $snip, string $commit): Response
#[Route('/set/{version}', name: '_set')]
public function set(Snip $snip, string $version): Response
{
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
$this->snipServiceFactory->createGit($snip)->setCommit($commit);
$this->addFlash('success', 'Snip version set to ' . $commit);
$this->snipServiceFactory->create($snip)->setVersion($version);
$this->addFlash('success', 'Snip version set to ' . $version);
return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]);
}
}

View File

@ -48,7 +48,8 @@ class SnipController extends AbstractController
{
$this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip);
$snipService = $this->snipServiceFactory->createGit($snip);
$snipService = $this->snipServiceFactory->create($snip);
dump($snipService);
return $this->render('snip/single.html.twig', [
'snip' => $snip,
'content' => $pl->parse($snipService->get()),
@ -62,7 +63,7 @@ class SnipController extends AbstractController
$this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip);
$response = new Response(
$pl->clean($this->snipServiceFactory->createGit($snip)->get()),
$pl->clean($this->snipServiceFactory->create($snip)->get()),
Response::HTTP_OK,
['Content-Type' => 'text/html']
);
@ -87,13 +88,13 @@ class SnipController extends AbstractController
$form = $this->createForm(SnipType::class, $snip);
$form->add('Save', SubmitType::class);
if ($snip->getId()) {
$form->get('content')->setData($this->snipServiceFactory->createGit($snip)->get());
$form->get('content')->setData($this->snipServiceFactory->create($snip)->get());
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->repository->save($snip);
$this->snipServiceFactory->createGit($snip)->update($form->get('content')->getData());
$this->snipServiceFactory->create($snip)->update($form->get('content')->getData());
$this->addFlash('success', sprintf('Snip "%s" saved', $snip));
@ -126,7 +127,7 @@ class SnipController extends AbstractController
$form = $this->createForm(ConfirmationType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->snipServiceFactory->createGit($snip)->delete();
$this->snipServiceFactory->create($snip)->delete();
$this->repository->remove($snip);
$this->addFlash('success', sprintf('Snip "%s" deleted', $snip));
return $this->redirectToRoute('snip_index');

View File

@ -7,14 +7,16 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Component\Uid\Ulid;
#[ORM\Entity(repositoryClass: SnipContentRepository::class)]
class SnipContent
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[ORM\Column(type: UlidType::NAME, unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'doctrine.ulid_generator')]
private ?Ulid $id = null;
#[ORM\ManyToOne(inversedBy: 'snipContents')]

View File

@ -19,8 +19,10 @@ readonly class SnipContentDB implements SnipContentInterface
{
// Create new snipContent entity with previous one as parent
$content = new SnipContent();
$content->setText($snipContents);
$content->setSnip($this->snip);
$content
->setText($snipContents)
->setSnip($this->snip)
;
if ($this->snip->getSnipContents()->count() > 0) {
$content->setParent($this->snip->getSnipContents()->last());
}
@ -35,19 +37,22 @@ readonly class SnipContentDB implements SnipContentInterface
public function get(): string
{
// Return the content of the latest snipContent entity
return $this->snip->getSnipContents()->last()->getText();
$contentRepo = $this->em->getRepository(SnipContent::class);
return $contentRepo->find($this->snip->getActiveCommit())->getText();
}
public function getHistory(): array
public function getVersions(): array
{
// Return all snipContent entities (by parent)
return array_map(fn(SnipContent $content) => $content->getId(), $this->snip->getSnipContents()->toArray());
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 setCommit(string $commit): void
public function setVersion(string $version): void
{
$this->snip->setActiveCommit($commit);
$this->snip->setActiveCommit($version);
$this->em->persist($this->snip);
$this->em->flush();
}
@ -61,4 +66,9 @@ readonly class SnipContentDB implements SnipContentInterface
{
// Cleanup the history
}
public function getLatestVersion(): string
{
return $this->snip->getSnipContents()->last()->getId();
}
}

View File

@ -4,19 +4,18 @@ namespace App\Service\SnipContent;
use App\Entity\User;
use App\Git\CustomGitRepository;
use App\Git\SimpleCommit;
use Symfony\Component\Security\Core\User\UserInterface;
class SnipContentGit implements SnipContentInterface
{
private const SNIP_FILE_NAME = 'snip.txt';
private const MASTER_BRANCH_NAME = 'master';
private const string SNIP_FILE_NAME = 'snip.txt';
private const string MASTER_BRANCH_NAME = 'master';
public function __construct(
private readonly CustomGitRepository $repo,
private readonly ?User $user,
)
{
}
) {}
private function snipExists(): bool
{
@ -51,17 +50,17 @@ class SnipContentGit implements SnipContentInterface
return file_get_contents($this->getSnipPath());
}
/**
* @return array<\App\Git\SimpleCommit>
*/
public function getHistory(): array
public function getVersions(): array
{
return $this->repo->getAllCommits();
return array_map(fn(SimpleCommit $c) => [
'id' => $c->getHash(),
'name' => $c->getDate()->format('Y-m-d H:i:s'),
], $this->repo->getAllCommits());
}
public function setCommit(string $commit): void
public function setVersion(string $version): void
{
$this->repo->checkout($commit);
$this->repo->checkout($version);
}
public function getCommit(): string
@ -73,4 +72,9 @@ class SnipContentGit implements SnipContentInterface
{
system("rm -rf " . escapeshellarg($this->repo->getRepositoryPath()));
}
public function getLatestVersion(): string
{
return self::MASTER_BRANCH_NAME;
}
}

View File

@ -8,11 +8,14 @@ interface SnipContentInterface
public function get(): string;
public function getHistory(): array;
/** @return array{id: string, name: string} */
public function getVersions(): array;
public function setCommit(string $commit): void;
public function setVersion(string $version): void;
public function getCommit(): string;
public function getLatestVersion(): string;
public function delete(): void;
}

View File

@ -4,25 +4,34 @@ namespace App\Service;
use App\Entity\Snip;
use App\Git\CustomGit;
use App\Repository\SnipContentRepository;
use App\Service\SnipContent\SnipContentDB;
use App\Service\SnipContent\SnipContentGit;
use App\Service\SnipContent\SnipContentInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;
class SnipServiceFactory
{
public function __construct(
private readonly string $snipBasePath,
private readonly Security $security,
private readonly SnipContentRepository $snipContentRepository,
)
private readonly string $gitStoragePath,
private readonly string $storageType,
private readonly Security $security,
private readonly EntityManagerInterface $em,
) {}
public function create(Snip $snip): SnipContentInterface
{
return match ($this->storageType) {
'git' => $this->createGit($snip),
'db' => $this->createDB($snip),
default => throw new \Exception('Unknown storage type'),
};
}
public function createGit(Snip $snip): SnipContentGit
private function createGit(Snip $snip): SnipContentGit
{
$git = new CustomGit();
$repoPath = sprintf('%s/%s', $this->snipBasePath, $snip->getId());
$repoPath = sprintf('%s/%s', $this->gitStoragePath, $snip->getId());
if (!is_dir($repoPath)) {
$repo = $git->init($repoPath);
touch(sprintf('%s/.gitignore', $repoPath));
@ -34,8 +43,8 @@ class SnipServiceFactory
return new SnipContentGit($repo, $this->security->getUser());
}
public function createDB(Snip $snip): SnipContentDB
private function createDB(Snip $snip): SnipContentDB
{
return new SnipContentDB($snip, $this->security->getUser(), $this->snipContentRepository);
return new SnipContentDB($snip, $this->security->getUser(), $this->em);
}
}

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('history_set', {commit: 'master', snip: snip.id}) }}" class="btn btn-warning">
<i class="fa fa-refresh"></i> Master
<a href="{{ path('history_set', {version: latestVersion, snip: snip.id}) }}" class="btn btn-warning">
<i class="fa fa-refresh"></i> Latest
</a>
<br><br>
<div class="list-group">
{% for commit in commits %}
<a class="list-group-item" href="{{ path('history_set', {commit: commit.hash, snip: snip.id}) }}">
{{ commit.date|date }} - {{ commit.hash }}
{% for version in versions %}
<a class="list-group-item" href="{{ path('history_set', {version: version.id, snip: snip.id}) }}">
{{ version.name }} - {{ version.id }}
</a>
{% endfor %}
</div>

View File

@ -27,7 +27,7 @@
{{ snip }} <small class="text-muted">#{{ snip.id }}</small>
</h4>
<div class="card-header">
<p class="card-text">Current branch: {{ branch }}</p>
<p class="card-text">Current version: {{ branch }}</p>
</div>
<div class="card-body">
{{ content|raw }}