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