feature/3-git-content-database #8

Merged
ardent merged 5 commits from feature/3-git-content-database into main 2023-12-17 22:03:00 +01:00
11 changed files with 89 additions and 57 deletions
Showing only changes of commit 6107f560e2 - Show all commits

View File

@ -4,6 +4,8 @@
# Put parameters here that don't need to change on each machine where the app is deployed # 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 # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters: parameters:
snipStorageType: 'db' # 'db' or 'git
gitStoragePath: '%kernel.project_dir%/var/snips'
services: services:
# default configuration for services in *this* file # default configuration for services in *this* file
@ -26,4 +28,5 @@ services:
App\Service\SnipServiceFactory: App\Service\SnipServiceFactory:
arguments: 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! * Auto-generated Migration: Please modify to your needs!
*/ */
final class Version20230419134540 extends AbstractMigration final class Version20231217002445 extends AbstractMigration
{ {
public function getDescription(): string public function getDescription(): string
{ {
@ -20,10 +20,10 @@ final class Version20230419134540 extends AbstractMigration
public function up(Schema $schema): void public function up(Schema $schema): void
{ {
// this up() migration is auto-generated, please modify it to your needs // 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_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 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 public function down(Schema $schema): void

View File

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

View File

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

View File

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

View File

@ -19,8 +19,10 @@ readonly class SnipContentDB implements SnipContentInterface
{ {
// Create new snipContent entity with previous one as parent // Create new snipContent entity with previous one as parent
$content = new SnipContent(); $content = new SnipContent();
$content->setText($snipContents); $content
$content->setSnip($this->snip); ->setText($snipContents)
->setSnip($this->snip)
;
if ($this->snip->getSnipContents()->count() > 0) { if ($this->snip->getSnipContents()->count() > 0) {
$content->setParent($this->snip->getSnipContents()->last()); $content->setParent($this->snip->getSnipContents()->last());
} }
@ -35,19 +37,22 @@ readonly class SnipContentDB implements SnipContentInterface
public function get(): string public function get(): string
{ {
// Return the content of the latest snipContent entity $contentRepo = $this->em->getRepository(SnipContent::class);
return $this->snip->getSnipContents()->last()->getText(); return $contentRepo->find($this->snip->getActiveCommit())->getText();
} }
public function getHistory(): array public function getVersions(): array
{ {
// Return all snipContent entities (by parent) // 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->persist($this->snip);
$this->em->flush(); $this->em->flush();
} }
@ -61,4 +66,9 @@ readonly class SnipContentDB implements SnipContentInterface
{ {
// Cleanup the history // 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\Entity\User;
use App\Git\CustomGitRepository; use App\Git\CustomGitRepository;
use App\Git\SimpleCommit;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
class SnipContentGit implements SnipContentInterface class SnipContentGit implements SnipContentInterface
{ {
private const SNIP_FILE_NAME = 'snip.txt'; private const string SNIP_FILE_NAME = 'snip.txt';
private const MASTER_BRANCH_NAME = 'master'; private const string MASTER_BRANCH_NAME = 'master';
public function __construct( public function __construct(
private readonly CustomGitRepository $repo, private readonly CustomGitRepository $repo,
private readonly ?User $user, private readonly ?User $user,
) ) {}
{
}
private function snipExists(): bool private function snipExists(): bool
{ {
@ -51,17 +50,17 @@ class SnipContentGit implements SnipContentInterface
return file_get_contents($this->getSnipPath()); return file_get_contents($this->getSnipPath());
} }
/** public function getVersions(): array
* @return array<\App\Git\SimpleCommit>
*/
public function getHistory(): 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 public function getCommit(): string
@ -73,4 +72,9 @@ class SnipContentGit implements SnipContentInterface
{ {
system("rm -rf " . escapeshellarg($this->repo->getRepositoryPath())); 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 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 getCommit(): string;
public function getLatestVersion(): string;
public function delete(): void; public function delete(): void;
} }

View File

@ -4,25 +4,34 @@ namespace App\Service;
use App\Entity\Snip; use App\Entity\Snip;
use App\Git\CustomGit; use App\Git\CustomGit;
use App\Repository\SnipContentRepository;
use App\Service\SnipContent\SnipContentDB; use App\Service\SnipContent\SnipContentDB;
use App\Service\SnipContent\SnipContentGit; use App\Service\SnipContent\SnipContentGit;
use App\Service\SnipContent\SnipContentInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security; use Symfony\Bundle\SecurityBundle\Security;
class SnipServiceFactory class SnipServiceFactory
{ {
public function __construct( public function __construct(
private readonly string $snipBasePath, private readonly string $gitStoragePath,
private readonly Security $security, private readonly string $storageType,
private readonly SnipContentRepository $snipContentRepository, 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(); $git = new CustomGit();
$repoPath = sprintf('%s/%s', $this->snipBasePath, $snip->getId()); $repoPath = sprintf('%s/%s', $this->gitStoragePath, $snip->getId());
if (!is_dir($repoPath)) { if (!is_dir($repoPath)) {
$repo = $git->init($repoPath); $repo = $git->init($repoPath);
touch(sprintf('%s/.gitignore', $repoPath)); touch(sprintf('%s/.gitignore', $repoPath));
@ -34,8 +43,8 @@ class SnipServiceFactory
return new SnipContentGit($repo, $this->security->getUser()); 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"> <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('history_set', {commit: 'master', snip: snip.id}) }}" class="btn btn-warning"> <a href="{{ path('history_set', {version: latestVersion, snip: snip.id}) }}" class="btn btn-warning">
<i class="fa fa-refresh"></i> Master <i class="fa fa-refresh"></i> Latest
</a> </a>
<br><br> <br><br>
<div class="list-group"> <div class="list-group">
{% for commit in commits %} {% for version in versions %}
<a class="list-group-item" href="{{ path('history_set', {commit: commit.hash, snip: snip.id}) }}"> <a class="list-group-item" href="{{ path('history_set', {version: version.id, snip: snip.id}) }}">
{{ commit.date|date }} - {{ commit.hash }} {{ version.name }} - {{ version.id }}
</a> </a>
{% endfor %} {% endfor %}
</div> </div>

View File

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