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

@ -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,15 +19,17 @@ 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());
}
$this->em->persist($content);
$this->em->flush();
$this->snip->setActiveCommit($content->getId());
$this->em->persist($this->snip);
$this->em->flush();
@ -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);
}
}