From a405578f93e8eab65fb3eaa324fabb828a066df7 Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 15 Apr 2023 13:20:07 +0200 Subject: [PATCH 1/4] Start with snips content abstraction --- src/Controller/HistoryController.php | 4 ++-- src/Controller/SnipController.php | 4 ++-- src/Service/SnipContent/SnipContentDB.php | 13 ++++++++++++ .../SnipContentGit.php} | 20 ++++++++++++++----- .../SnipContent/SnipContentInterface.php | 18 +++++++++++++++++ src/Service/SnipServiceFactory.php | 5 +++-- 6 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 src/Service/SnipContent/SnipContentDB.php rename src/Service/{SnipService.php => SnipContent/SnipContentGit.php} (77%) create mode 100644 src/Service/SnipContent/SnipContentInterface.php diff --git a/src/Controller/HistoryController.php b/src/Controller/HistoryController.php index c14be55..7afa6e0 100644 --- a/src/Controller/HistoryController.php +++ b/src/Controller/HistoryController.php @@ -25,7 +25,7 @@ class HistoryController extends AbstractController return $this->render('history/index.html.twig', [ 'snip' => $snip, - 'commits' => $this->snipServiceFactory->create($snip)->getRepo()->getAllCommits(), + 'commits' => $this->snipServiceFactory->create($snip)->getHistory(), ]); } @@ -34,7 +34,7 @@ class HistoryController extends AbstractController { $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); - $this->snipServiceFactory->create($snip)->getRepo()->checkout($commit); + $this->snipServiceFactory->create($snip)->setCommit($commit); $this->addFlash('success', 'Snip version set to ' . $commit); return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]); } diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index c74326c..896907f 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -52,7 +52,7 @@ class SnipController extends AbstractController return $this->render('snip/single.html.twig', [ 'snip' => $snip, 'content' => (new Pipeline())->parse($snipService->get()), - 'branch' => $snipService->getRepo()->getCurrentBranchName(), + 'branch' => $snipService->getCommit(), ]); } @@ -130,7 +130,7 @@ class SnipController extends AbstractController $form = $this->createForm(ConfirmationType::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $this->snipServiceFactory->create($snip)->deleteRepo(); + $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/Service/SnipContent/SnipContentDB.php b/src/Service/SnipContent/SnipContentDB.php new file mode 100644 index 0000000..5c69c5e --- /dev/null +++ b/src/Service/SnipContent/SnipContentDB.php @@ -0,0 +1,13 @@ +getSnipPath()); } - public function getRepo(): CustomGitRepository + public function getHistory(): array { - return $this->repo; + return $this->repo->getAllCommits(); } - public function deleteRepo(): void + public function setCommit(string $commit): void + { + $this->repo->checkout($commit); + } + + public function getCommit(): string + { + return $this->repo->getCurrentBranchName(); + } + + public function delete(): void { system("rm -rf " . escapeshellarg($this->repo->getRepositoryPath())); } diff --git a/src/Service/SnipContent/SnipContentInterface.php b/src/Service/SnipContent/SnipContentInterface.php new file mode 100644 index 0000000..67c9ec4 --- /dev/null +++ b/src/Service/SnipContent/SnipContentInterface.php @@ -0,0 +1,18 @@ +snipBasePath, $snip->getId()); @@ -28,6 +29,6 @@ class SnipServiceFactory } else { $repo = $git->open($repoPath); } - return new SnipService($repo, $this->security->getUser()); + return new SnipContentGit($repo, $this->security->getUser()); } } \ No newline at end of file -- 2.34.1 From 506a0e8deca544061add06c176175b5b6e1843f3 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 20 Apr 2023 23:23:04 +0200 Subject: [PATCH 2/4] Start on creating snipContent entities for the db content --- .env | 2 +- migrations/Version20230419134540.php | 37 +++++++ src/Controller/HistoryController.php | 4 +- src/Controller/SnipController.php | 10 +- src/Entity/Snip.php | 40 ++++++++ src/Entity/SnipContent.php | 107 +++++++++++++++++++++ src/Git/CustomGitRepository.php | 2 +- src/Repository/SnipContentRepository.php | 66 +++++++++++++ src/Service/SnipContent/SnipContentDB.php | 52 +++++++++- src/Service/SnipContent/SnipContentGit.php | 3 + src/Service/SnipServiceFactory.php | 14 ++- 11 files changed, 323 insertions(+), 14 deletions(-) create mode 100644 migrations/Version20230419134540.php create mode 100644 src/Entity/SnipContent.php create mode 100644 src/Repository/SnipContentRepository.php diff --git a/.env b/.env index affd816..e3feaae 100644 --- a/.env +++ b/.env @@ -24,6 +24,6 @@ APP_SECRET=a617c2ab616c5688ff5b0e95ad646641 # IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml # # DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db" -# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=8&charset=utf8mb4" +# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=mariadb-10.9.5&charset=utf8mb4" # DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=15&charset=utf8" ###< doctrine/doctrine-bundle ### diff --git a/migrations/Version20230419134540.php b/migrations/Version20230419134540.php new file mode 100644 index 0000000..1fc86f5 --- /dev/null +++ b/migrations/Version20230419134540.php @@ -0,0 +1,37 @@ +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('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)\''); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE snip_content DROP FOREIGN KEY FK_185DCA87140FD260'); + $this->addSql('ALTER TABLE snip_content DROP FOREIGN KEY FK_185DCA87727ACA70'); + $this->addSql('DROP TABLE snip_content'); + $this->addSql('ALTER TABLE `user` CHANGE roles roles LONGTEXT NOT NULL COLLATE `utf8mb4_bin`'); + } +} diff --git a/src/Controller/HistoryController.php b/src/Controller/HistoryController.php index 7afa6e0..59d10dd 100644 --- a/src/Controller/HistoryController.php +++ b/src/Controller/HistoryController.php @@ -25,7 +25,7 @@ class HistoryController extends AbstractController return $this->render('history/index.html.twig', [ 'snip' => $snip, - 'commits' => $this->snipServiceFactory->create($snip)->getHistory(), + 'commits' => $this->snipServiceFactory->createGit($snip)->getHistory(), ]); } @@ -34,7 +34,7 @@ class HistoryController extends AbstractController { $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); - $this->snipServiceFactory->create($snip)->setCommit($commit); + $this->snipServiceFactory->createGit($snip)->setCommit($commit); $this->addFlash('success', 'Snip version set to ' . $commit); return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]); } diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index 896907f..2588066 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -48,7 +48,7 @@ class SnipController extends AbstractController { $this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip); - $snipService = $this->snipServiceFactory->create($snip); + $snipService = $this->snipServiceFactory->createGit($snip); return $this->render('snip/single.html.twig', [ 'snip' => $snip, 'content' => (new Pipeline())->parse($snipService->get()), @@ -62,7 +62,7 @@ class SnipController extends AbstractController $this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip); $response = new Response( - (new Pipeline())->clean($this->snipServiceFactory->create($snip)->get()), + (new Pipeline())->clean($this->snipServiceFactory->createGit($snip)->get()), Response::HTTP_OK, ['Content-Type' => 'text/html'] ); @@ -91,13 +91,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->create($snip)->get()); + $form->get('content')->setData($this->snipServiceFactory->createGit($snip)->get()); } $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->repository->save($snip); - $this->snipServiceFactory->create($snip)->update($form->get('content')->getData()); + $this->snipServiceFactory->createGit($snip)->update($form->get('content')->getData()); $this->addFlash('success', sprintf('Snip "%s" saved', $snip)); @@ -130,7 +130,7 @@ class SnipController extends AbstractController $form = $this->createForm(ConfirmationType::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $this->snipServiceFactory->create($snip)->delete(); + $this->snipServiceFactory->createGit($snip)->delete(); $this->repository->remove($snip); $this->addFlash('success', sprintf('Snip "%s" deleted', $snip)); return $this->redirectToRoute('snip_index'); diff --git a/src/Entity/Snip.php b/src/Entity/Snip.php index 405ff3d..6a5f484 100644 --- a/src/Entity/Snip.php +++ b/src/Entity/Snip.php @@ -4,6 +4,8 @@ namespace App\Entity; use App\Entity\Helpers\TrackedTrait; use App\Repository\SnipRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: SnipRepository::class)] @@ -22,6 +24,14 @@ class Snip #[ORM\Column] private ?bool $public = null; + #[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)] + private Collection $snipContents; + + public function __construct() + { + $this->snipContents = new ArrayCollection(); + } + public function __toString(): string { return $this->name ?? ''; @@ -55,4 +65,34 @@ class Snip return $this; } + + /** + * @return Collection + */ + public function getSnipContents(): Collection + { + return $this->snipContents; + } + + public function addSnipContent(SnipContent $snipContent): self + { + if (!$this->snipContents->contains($snipContent)) { + $this->snipContents->add($snipContent); + $snipContent->setSnip($this); + } + + return $this; + } + + public function removeSnipContent(SnipContent $snipContent): self + { + if ($this->snipContents->removeElement($snipContent)) { + // set the owning side to null (unless already changed) + if ($snipContent->getSnip() === $this) { + $snipContent->setSnip(null); + } + } + + return $this; + } } diff --git a/src/Entity/SnipContent.php b/src/Entity/SnipContent.php new file mode 100644 index 0000000..dcddcb2 --- /dev/null +++ b/src/Entity/SnipContent.php @@ -0,0 +1,107 @@ +child = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getSnip(): ?Snip + { + return $this->snip; + } + + public function setSnip(?Snip $snip): self + { + $this->snip = $snip; + + return $this; + } + + public function getParent(): ?self + { + return $this->parent; + } + + public function setParent(?self $parent): self + { + $this->parent = $parent; + + return $this; + } + + /** + * @return Collection + */ + public function getChild(): Collection + { + return $this->child; + } + + public function addChild(self $child): self + { + if (!$this->child->contains($child)) { + $this->child->add($child); + $child->setParent($this); + } + + return $this; + } + + public function removeChild(self $child): self + { + if ($this->child->removeElement($child)) { + // set the owning side to null (unless already changed) + if ($child->getParent() === $this) { + $child->setParent(null); + } + } + + return $this; + } + + public function getText(): ?string + { + return $this->text; + } + + public function setText(?string $text): self + { + $this->text = $text; + + return $this; + } +} diff --git a/src/Git/CustomGitRepository.php b/src/Git/CustomGitRepository.php index 99c3eb7..6b5585c 100644 --- a/src/Git/CustomGitRepository.php +++ b/src/Git/CustomGitRepository.php @@ -8,7 +8,7 @@ use DateTime; class CustomGitRepository extends GitRepository { /** - * @return SimpleCommit[] + * @return array * @throws \CzProject\GitPhp\GitException */ public function getAllCommits(): array diff --git a/src/Repository/SnipContentRepository.php b/src/Repository/SnipContentRepository.php new file mode 100644 index 0000000..b661732 --- /dev/null +++ b/src/Repository/SnipContentRepository.php @@ -0,0 +1,66 @@ + + * + * @method SnipContent|null find($id, $lockMode = null, $lockVersion = null) + * @method SnipContent|null findOneBy(array $criteria, array $orderBy = null) + * @method SnipContent[] findAll() + * @method SnipContent[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class SnipContentRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, SnipContent::class); + } + + public function save(SnipContent $entity, bool $flush = true): void + { + $this->getEntityManager()->persist($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + public function remove(SnipContent $entity, bool $flush = true): void + { + $this->getEntityManager()->remove($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + +// /** +// * @return SnipContent[] Returns an array of SnipContent objects +// */ +// public function findByExampleField($value): array +// { +// return $this->createQueryBuilder('s') +// ->andWhere('s.exampleField = :val') +// ->setParameter('val', $value) +// ->orderBy('s.id', 'ASC') +// ->setMaxResults(10) +// ->getQuery() +// ->getResult() +// ; +// } + +// public function findOneBySomeField($value): ?SnipContent +// { +// return $this->createQueryBuilder('s') +// ->andWhere('s.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +} diff --git a/src/Service/SnipContent/SnipContentDB.php b/src/Service/SnipContent/SnipContentDB.php index 5c69c5e..cfa90fb 100644 --- a/src/Service/SnipContent/SnipContentDB.php +++ b/src/Service/SnipContent/SnipContentDB.php @@ -2,12 +2,60 @@ namespace App\Service\SnipContent; +use App\Entity\Snip; +use App\Entity\SnipContent; +use App\Entity\User; +use App\Repository\SnipContentRepository; + class SnipContentDB implements SnipContentInterface { - - public function __construct() + public function __construct( + private readonly Snip $snip, + private readonly User $user, + private readonly SnipContentRepository $repo, + ) { } + public function update(string $snipContents): void + { + // Create new snipContent entity with previous one as parent + $content = new SnipContent(); + $content->setText($snipContents); + $content->setSnip($this->snip); + if ($this->snip->getSnipContents()->count() > 0) { + $content->setParent($this->snip->getSnipContents()->last()); + } + $this->repo->save($content); + } + + public function get(): string + { + // Return the content of the latest snipContent entity + return $this->snip->getSnipContents()->last()->getText(); + } + + public function getHistory(): array + { + // Return all snipContent entities (by parent) + return array_map(fn(SnipContent $content) => $content->getId(), $this->snip->getSnipContents()->toArray()); + } + + public function setCommit(string $commit): void + { + // return to previous history commit + // maybe store the current commit in the snip content + } + + public function getCommit(): string + { + // return the current commit + return ''; + } + + public function delete(): void + { + // Cleanup the history + } } \ No newline at end of file diff --git a/src/Service/SnipContent/SnipContentGit.php b/src/Service/SnipContent/SnipContentGit.php index 4638d72..6411d82 100644 --- a/src/Service/SnipContent/SnipContentGit.php +++ b/src/Service/SnipContent/SnipContentGit.php @@ -51,6 +51,9 @@ class SnipContentGit implements SnipContentInterface return file_get_contents($this->getSnipPath()); } + /** + * @return array<\App\Git\SimpleCommit> + */ public function getHistory(): array { return $this->repo->getAllCommits(); diff --git a/src/Service/SnipServiceFactory.php b/src/Service/SnipServiceFactory.php index 76f4719..19f3e41 100644 --- a/src/Service/SnipServiceFactory.php +++ b/src/Service/SnipServiceFactory.php @@ -4,6 +4,8 @@ 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 Symfony\Bundle\SecurityBundle\Security; @@ -11,13 +13,14 @@ class SnipServiceFactory { public function __construct( - private readonly string $snipBasePath, - private readonly Security $security, + private readonly string $snipBasePath, + private readonly Security $security, + private readonly SnipContentRepository $snipContentRepository, ) { } - public function create(Snip $snip): SnipContentGit + public function createGit(Snip $snip): SnipContentGit { $git = new CustomGit(); $repoPath = sprintf('%s/%s', $this->snipBasePath, $snip->getId()); @@ -31,4 +34,9 @@ class SnipServiceFactory } return new SnipContentGit($repo, $this->security->getUser()); } + + public function createDB(Snip $snip): SnipContentDB + { + return new SnipContentDB($snip, $this->security->getUser(), $this->snipContentRepository); + } } \ No newline at end of file -- 2.34.1 From 0fce8ee4feb1bf9e974133a0ea70ec0a8ffe8187 Mon Sep 17 00:00:00 2001 From: tim Date: Sat, 16 Dec 2023 01:26:47 +0100 Subject: [PATCH 3/4] Start on property keeping track of git db content --- composer.json | 3 +- composer.lock | 162 +++++++++++++++++++++- config/packages/uid.yaml | 4 + src/Entity/Snip.php | 15 ++ src/Entity/SnipContent.php | 21 +-- src/Service/SnipContent/SnipContentDB.php | 29 ++-- symfony.lock | 12 ++ 7 files changed, 219 insertions(+), 27 deletions(-) create mode 100644 config/packages/uid.yaml diff --git a/composer.json b/composer.json index 538038a..f7fbeb6 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "minimum-stability": "stable", "prefer-stable": true, "require": { - "php": ">=8.2", + "php": ">=8.3", "ext-ctype": "*", "ext-iconv": "*", "czproject/git-php": "^4.1", @@ -21,6 +21,7 @@ "symfony/runtime": "7.0.*", "symfony/security-bundle": "7.0.*", "symfony/twig-bundle": "7.0.*", + "symfony/uid": "7.0.*", "symfony/validator": "7.0.*", "symfony/yaml": "7.0.*", "twig/extra-bundle": "^2.12|^3.0", diff --git a/composer.lock b/composer.lock index 4df7aee..d15a48c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "81c31aacf558fbbb42aaa93138c76e40", + "content-hash": "0d9cbf5f6f95b13006484d60ccb4d67c", "packages": [ { "name": "czproject/git-php", @@ -4097,6 +4097,88 @@ ], "time": "2023-08-16T06:22:46+00:00" }, + { + "name": "symfony/polyfill-uuid", + "version": "v1.28.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-uuid.git", + "reference": "9c44518a5aff8da565c8a55dbe85d2769e6f630e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-uuid/zipball/9c44518a5aff8da565c8a55dbe85d2769e6f630e", + "reference": "9c44518a5aff8da565c8a55dbe85d2769e6f630e", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-uuid": "*" + }, + "suggest": { + "ext-uuid": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.28-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Uuid\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Grégoire Pineau", + "email": "lyrixx@lyrixx.info" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for uuid functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "uuid" + ], + "support": { + "source": "https://github.com/symfony/polyfill-uuid/tree/v1.28.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2023-01-26T09:26:14+00:00" + }, { "name": "symfony/property-access", "version": "v7.0.0", @@ -5265,6 +5347,80 @@ ], "time": "2023-11-26T15:16:53+00:00" }, + { + "name": "symfony/uid", + "version": "v7.0.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/uid.git", + "reference": "9472fe6a4a2adcc9150106ebb9fde328828d312f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/uid/zipball/9472fe6a4a2adcc9150106ebb9fde328828d312f", + "reference": "9472fe6a4a2adcc9150106ebb9fde328828d312f", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "symfony/polyfill-uuid": "^1.15" + }, + "require-dev": { + "symfony/console": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Uid\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Grégoire Pineau", + "email": "lyrixx@lyrixx.info" + }, + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides an object-oriented API to generate and represent UIDs", + "homepage": "https://symfony.com", + "keywords": [ + "UID", + "ulid", + "uuid" + ], + "support": { + "source": "https://github.com/symfony/uid/tree/v7.0.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2023-10-31T08:22:02+00:00" + }, { "name": "symfony/validator", "version": "v7.0.0", @@ -6149,10 +6305,10 @@ "prefer-stable": true, "prefer-lowest": false, "platform": { - "php": ">=8.2", + "php": ">=8.3", "ext-ctype": "*", "ext-iconv": "*" }, "platform-dev": [], - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/config/packages/uid.yaml b/config/packages/uid.yaml new file mode 100644 index 0000000..0152094 --- /dev/null +++ b/config/packages/uid.yaml @@ -0,0 +1,4 @@ +framework: + uid: + default_uuid_version: 7 + time_based_uuid_version: 7 diff --git a/src/Entity/Snip.php b/src/Entity/Snip.php index 6a5f484..19824aa 100644 --- a/src/Entity/Snip.php +++ b/src/Entity/Snip.php @@ -27,6 +27,9 @@ class Snip #[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)] private Collection $snipContents; + #[ORM\Column(length: 255, nullable: true)] + private ?string $activeCommit = null; + public function __construct() { $this->snipContents = new ArrayCollection(); @@ -95,4 +98,16 @@ class Snip return $this; } + + public function getActiveCommit(): ?string + { + return $this->activeCommit; + } + + public function setActiveCommit(?string $activeCommit): static + { + $this->activeCommit = $activeCommit; + + return $this; + } } diff --git a/src/Entity/SnipContent.php b/src/Entity/SnipContent.php index dcddcb2..6668a74 100644 --- a/src/Entity/SnipContent.php +++ b/src/Entity/SnipContent.php @@ -7,6 +7,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Uid\Ulid; #[ORM\Entity(repositoryClass: SnipContentRepository::class)] class SnipContent @@ -14,27 +15,27 @@ class SnipContent #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] - private ?int $id = null; + private ?Ulid $id = null; #[ORM\ManyToOne(inversedBy: 'snipContents')] #[ORM\JoinColumn(nullable: false)] private ?Snip $snip = null; - #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'child')] + #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] private ?self $parent = null; #[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)] - private Collection $child; + private Collection $children; #[ORM\Column(type: Types::TEXT, nullable: true)] private ?string $text = null; public function __construct() { - $this->child = new ArrayCollection(); + $this->children = new ArrayCollection(); } - public function getId(): ?int + public function getId(): ?Ulid { return $this->id; } @@ -66,15 +67,15 @@ class SnipContent /** * @return Collection */ - public function getChild(): Collection + public function getChildren(): Collection { - return $this->child; + return $this->children; } public function addChild(self $child): self { - if (!$this->child->contains($child)) { - $this->child->add($child); + if (!$this->children->contains($child)) { + $this->children->add($child); $child->setParent($this); } @@ -83,7 +84,7 @@ class SnipContent public function removeChild(self $child): self { - if ($this->child->removeElement($child)) { + if ($this->children->removeElement($child)) { // set the owning side to null (unless already changed) if ($child->getParent() === $this) { $child->setParent(null); diff --git a/src/Service/SnipContent/SnipContentDB.php b/src/Service/SnipContent/SnipContentDB.php index cfa90fb..a07156d 100644 --- a/src/Service/SnipContent/SnipContentDB.php +++ b/src/Service/SnipContent/SnipContentDB.php @@ -5,17 +5,15 @@ namespace App\Service\SnipContent; use App\Entity\Snip; use App\Entity\SnipContent; use App\Entity\User; -use App\Repository\SnipContentRepository; +use Doctrine\ORM\EntityManagerInterface; -class SnipContentDB implements SnipContentInterface +readonly class SnipContentDB implements SnipContentInterface { public function __construct( - private readonly Snip $snip, - private readonly User $user, - private readonly SnipContentRepository $repo, - ) - { - } + private Snip $snip, + private User $user, + private EntityManagerInterface $em, + ) {} public function update(string $snipContents): void { @@ -27,7 +25,12 @@ class SnipContentDB implements SnipContentInterface $content->setParent($this->snip->getSnipContents()->last()); } - $this->repo->save($content); + $this->em->persist($content); + $this->em->flush(); + + $this->snip->setActiveCommit($content->getId()); + $this->em->persist($this->snip); + $this->em->flush(); } public function get(): string @@ -44,14 +47,14 @@ class SnipContentDB implements SnipContentInterface public function setCommit(string $commit): void { - // return to previous history commit - // maybe store the current commit in the snip content + $this->snip->setActiveCommit($commit); + $this->em->persist($this->snip); + $this->em->flush(); } public function getCommit(): string { - // return the current commit - return ''; + return $this->snip->getActiveCommit(); } public function delete(): void diff --git a/symfony.lock b/symfony.lock index 0ae6795..2ff4109 100644 --- a/symfony.lock +++ b/symfony.lock @@ -140,6 +140,18 @@ "templates/base.html.twig" ] }, + "symfony/uid": { + "version": "7.0", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "main", + "version": "6.2", + "ref": "d294ad4add3e15d7eb1bae0221588ca89b38e558" + }, + "files": [ + "config/packages/uid.yaml" + ] + }, "symfony/validator": { "version": "6.2", "recipe": { -- 2.34.1 From 6107f560e247b90c5fa339ce757ac0b3e0f70219 Mon Sep 17 00:00:00 2001 From: tim Date: Sun, 17 Dec 2023 01:55:26 +0100 Subject: [PATCH 4/4] Fully implement snip db storage --- config/services.yaml | 5 +++- ...19134540.php => Version20231217002445.php} | 6 ++-- src/Controller/HistoryController.php | 16 +++++------ src/Controller/SnipController.php | 11 ++++---- src/Entity/SnipContent.php | 6 ++-- src/Service/SnipContent/SnipContentDB.php | 28 +++++++++++++------ src/Service/SnipContent/SnipContentGit.php | 28 +++++++++++-------- .../SnipContent/SnipContentInterface.php | 7 +++-- src/Service/SnipServiceFactory.php | 27 ++++++++++++------ templates/history/index.html.twig | 10 +++---- templates/snip/single.html.twig | 2 +- 11 files changed, 89 insertions(+), 57 deletions(-) rename migrations/{Version20230419134540.php => Version20231217002445.php} (68%) 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

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 }} -- 2.34.1