diff --git a/config/services.yaml b/config/services.yaml index 39cf726..bf61fd9 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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' \ No newline at end of file + $gitStoragePath: '%gitStoragePath%' + $storageType: '%snipStorageType%' \ No newline at end of file diff --git a/migrations/Version20230419134540.php b/migrations/Version20231217002445.php similarity index 68% rename from migrations/Version20230419134540.php rename to migrations/Version20231217002445.php index 1fc86f5..ef4153b 100644 --- a/migrations/Version20230419134540.php +++ b/migrations/Version20231217002445.php @@ -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 diff --git a/src/Controller/HistoryController.php b/src/Controller/HistoryController.php index 59d10dd..1a08bd3 100644 --- a/src/Controller/HistoryController.php +++ b/src/Controller/HistoryController.php @@ -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()]); } } \ No newline at end of file diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index 216f160..4c2ad59 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -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'); diff --git a/src/Entity/SnipContent.php b/src/Entity/SnipContent.php index 6668a74..ee98d9b 100644 --- a/src/Entity/SnipContent.php +++ b/src/Entity/SnipContent.php @@ -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')] diff --git a/src/Service/SnipContent/SnipContentDB.php b/src/Service/SnipContent/SnipContentDB.php index a07156d..6fa2e0b 100644 --- a/src/Service/SnipContent/SnipContentDB.php +++ b/src/Service/SnipContent/SnipContentDB.php @@ -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(); + } } \ No newline at end of file diff --git a/src/Service/SnipContent/SnipContentGit.php b/src/Service/SnipContent/SnipContentGit.php index 6411d82..005ff3f 100644 --- a/src/Service/SnipContent/SnipContentGit.php +++ b/src/Service/SnipContent/SnipContentGit.php @@ -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; + } } \ No newline at end of file diff --git a/src/Service/SnipContent/SnipContentInterface.php b/src/Service/SnipContent/SnipContentInterface.php index 67c9ec4..f687eb5 100644 --- a/src/Service/SnipContent/SnipContentInterface.php +++ b/src/Service/SnipContent/SnipContentInterface.php @@ -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; } \ No newline at end of file diff --git a/src/Service/SnipServiceFactory.php b/src/Service/SnipServiceFactory.php index 0590f83..1c85c18 100644 --- a/src/Service/SnipServiceFactory.php +++ b/src/Service/SnipServiceFactory.php @@ -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); } } \ No newline at end of file diff --git a/templates/history/index.html.twig b/templates/history/index.html.twig index c21d130..79714ec 100644 --- a/templates/history/index.html.twig +++ b/templates/history/index.html.twig @@ -6,14 +6,14 @@ Back - - Master + + Latest

- {% for commit in commits %} - - {{ commit.date|date }} - {{ commit.hash }} + {% for version in versions %} + + {{ version.name }} - {{ version.id }} {% endfor %}
diff --git a/templates/snip/single.html.twig b/templates/snip/single.html.twig index 20d7780..b1d0c7a 100644 --- a/templates/snip/single.html.twig +++ b/templates/snip/single.html.twig @@ -27,7 +27,7 @@ {{ snip }} #{{ snip.id }}
-

Current branch: {{ branch }}

+

Current version: {{ branch }}

{{ content|raw }}