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 323 additions and 14 deletions
Showing only changes of commit 506a0e8dec - Show all commits

2
.env
View File

@ -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 ###

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230419134540 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
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('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`');
}
}

View File

@ -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()]);
}

View File

@ -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');

View File

@ -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<int, SnipContent>
*/
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;
}
}

107
src/Entity/SnipContent.php Normal file
View File

@ -0,0 +1,107 @@
<?php
namespace App\Entity;
use App\Repository\SnipContentRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SnipContentRepository::class)]
class SnipContent
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'snipContents')]
#[ORM\JoinColumn(nullable: false)]
private ?Snip $snip = null;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'child')]
private ?self $parent = null;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
private Collection $child;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $text = null;
public function __construct()
{
$this->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<int, self>
*/
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;
}
}

View File

@ -8,7 +8,7 @@ use DateTime;
class CustomGitRepository extends GitRepository
{
/**
* @return SimpleCommit[]
* @return array<SimpleCommit>
* @throws \CzProject\GitPhp\GitException
*/
public function getAllCommits(): array

View File

@ -0,0 +1,66 @@
<?php
namespace App\Repository;
use App\Entity\SnipContent;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<SnipContent>
*
* @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()
// ;
// }
}

View File

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

View File

@ -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();

View File

@ -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);
}
}