From 842c936d8c31ddcdff5da8025ed201cb232c4e61 Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 4 Apr 2023 23:23:53 +0200 Subject: [PATCH] First fully working version of saving snips content with git control --- composer.json | 1 + composer.lock | 54 +++++++++++++++++++++++++++- config/services.yaml | 4 +++ src/Controller/SnipController.php | 11 ++++-- src/Entity/Helpers/TrackedTrait.php | 5 --- src/Repository/SnipRepository.php | 11 ++++++ src/Service/SnipService.php | 55 +++++++++++++++++++++++++++++ src/Service/SnipServiceFactory.php | 22 ++++++++++++ templates/base/base.html.twig | 8 ++--- templates/snip/edit.html.twig | 2 +- templates/snip/index.html.twig | 2 +- templates/snip/single.html.twig | 12 ++++++- 12 files changed, 170 insertions(+), 17 deletions(-) create mode 100644 src/Service/SnipService.php create mode 100644 src/Service/SnipServiceFactory.php diff --git a/composer.json b/composer.json index 92ddee6..aaa01f8 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "php": ">=8.1", "ext-ctype": "*", "ext-iconv": "*", + "czproject/git-php": "^4.1", "doctrine/doctrine-bundle": "^2.9", "doctrine/doctrine-migrations-bundle": "^3.2", "doctrine/orm": "^2.14", diff --git a/composer.lock b/composer.lock index efb15dc..f34c252 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,60 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5cd09d345b4ecefaece6f03c919d953e", + "content-hash": "975714eae301e2eb9cd72cfce038b337", "packages": [ + { + "name": "czproject/git-php", + "version": "v4.1.0", + "source": { + "type": "git", + "url": "https://github.com/czproject/git-php.git", + "reference": "bb195e30442bc5206b30fa9a304e20ea8e96458f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/czproject/git-php/zipball/bb195e30442bc5206b30fa9a304e20ea8e96458f", + "reference": "bb195e30442bc5206b30fa9a304e20ea8e96458f", + "shasum": "" + }, + "require": { + "php": ">=5.6.0" + }, + "require-dev": { + "nette/tester": "^2.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jan Pecha", + "email": "janpecha@email.cz" + } + ], + "description": "Library for work with Git repository in PHP.", + "keywords": [ + "git" + ], + "support": { + "issues": "https://github.com/czproject/git-php/issues", + "source": "https://github.com/czproject/git-php/tree/v4.1.0" + }, + "funding": [ + { + "url": "https://www.paypal.com/donate?hosted_button_id=EPSKR7MGVV7KC", + "type": "custom" + } + ], + "time": "2022-12-10T18:23:32+00:00" + }, { "name": "doctrine/cache", "version": "2.2.0", diff --git a/config/services.yaml b/config/services.yaml index 5268342..39cf726 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -23,3 +23,7 @@ services: App\Service\LastRelease: arguments: - '%kernel.project_dir%/release.json' + + App\Service\SnipServiceFactory: + arguments: + - '%kernel.project_dir%/var/snips' \ No newline at end of file diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index 8471cdd..e1cb66e 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -5,6 +5,7 @@ namespace App\Controller; use App\Entity\Snip; use App\Form\SnipType; use App\Repository\SnipRepository; +use App\Service\SnipServiceFactory; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; @@ -16,6 +17,7 @@ class SnipController extends AbstractController { public function __construct( private readonly SnipRepository $repository, + private readonly SnipServiceFactory $snipServiceFactory, ) { } @@ -24,22 +26,26 @@ class SnipController extends AbstractController public function index(): Response { return $this->render('snip/index.html.twig', [ - 'snips' => $this->repository->findAll(), + 'snips' => $this->repository->findByUser($this->getUser()), ]); } - #[Route('/singe/{snip}', name: '_single')] + #[Route('/single/{snip}', name: '_single')] public function single(Snip $snip): Response { return $this->render('snip/single.html.twig', [ 'snip' => $snip, + 'content' => $this->snipServiceFactory->create($snip)->get(), ]); } #[Route('/edit/{snip}', name: '_edit')] public function edit(Snip $snip, Request $request): Response { + $snipService = $this->snipServiceFactory->create($snip); + $form = $this->createForm(SnipType::class, $snip); + $form->get('content')->setData($snipService->get()); $form->add('Save', SubmitType::class); $form->handleRequest($request); @@ -47,6 +53,7 @@ class SnipController extends AbstractController $snip->setCreatedAtTodayNoSeconds() ->setCreatedBy($this->getUser()); $this->repository->save($snip); + $snipService->update($form->get('content')->getData()); $this->addFlash('success', sprintf('Snip "%s" saved successfully', $snip)); diff --git a/src/Entity/Helpers/TrackedTrait.php b/src/Entity/Helpers/TrackedTrait.php index 88d6ce6..1fdd5f6 100644 --- a/src/Entity/Helpers/TrackedTrait.php +++ b/src/Entity/Helpers/TrackedTrait.php @@ -40,11 +40,6 @@ trait TrackedTrait return $this; } - public function getCode(): string - { - return date_format($this->createdAt, "ymd"); - } - public function setCreatedAtTodayNoSeconds(): self { $this->setCreatedAt(DateTime::createFromFormat('Y-m-d H:i', date('Y-m-d H:i'))); diff --git a/src/Repository/SnipRepository.php b/src/Repository/SnipRepository.php index 988b6e2..ccdd8dd 100644 --- a/src/Repository/SnipRepository.php +++ b/src/Repository/SnipRepository.php @@ -3,6 +3,7 @@ namespace App\Repository; use App\Entity\Snip; +use App\Entity\User; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; @@ -38,4 +39,14 @@ class SnipRepository extends ServiceEntityRepository $this->getEntityManager()->flush(); } } + + public function findByUser(User $user): array + { + $qb = $this->createQueryBuilder('s'); + $qb->where('s.createdBy = :user') + ->setParameter('user', $user) + ->orderBy('s.createdAt', 'DESC'); + + return $qb->getQuery()->getResult(); + } } diff --git a/src/Service/SnipService.php b/src/Service/SnipService.php new file mode 100644 index 0000000..9250d09 --- /dev/null +++ b/src/Service/SnipService.php @@ -0,0 +1,55 @@ +getId()); + if (!is_dir($repoPath)) { + $this->repo = $git->init($repoPath); + } else { + $this->repo = $git->open($repoPath); + } + } + + private function snipExists(): bool + { + return file_exists($this->getSnipPath()); + } + + private function getSnipPath(): string + { + return sprintf('%s/snip.txt', $this->repo->getRepositoryPath()); + } + + public function update(string $snipContents): void + { + file_put_contents($this->getSnipPath(), $snipContents); + $this->repo->addFile('snip.txt'); + if ($this->repo->hasChanges()) { + $this->repo->commit(sprintf('Updated snip at %s by %s', date('Y-m-d H:i:s'), $this->user)); + } + } + + public function get(): string + { + if (!$this->snipExists()) { + return ''; + } + return file_get_contents($this->getSnipPath()); + } +} \ No newline at end of file diff --git a/src/Service/SnipServiceFactory.php b/src/Service/SnipServiceFactory.php new file mode 100644 index 0000000..04c2f86 --- /dev/null +++ b/src/Service/SnipServiceFactory.php @@ -0,0 +1,22 @@ +snipBasePath, $this->security->getUser()); + } +} \ No newline at end of file diff --git a/templates/base/base.html.twig b/templates/base/base.html.twig index 3bff21d..043db66 100644 --- a/templates/base/base.html.twig +++ b/templates/base/base.html.twig @@ -3,13 +3,8 @@ - {% block title %}BLES{% endblock %} + {% block title %}SNIPS{% endblock %} - - {% if chartjs|default(false) %} - - - {% endif %} {% block css %} diff --git a/templates/snip/edit.html.twig b/templates/snip/edit.html.twig index 77005cd..b8990aa 100644 --- a/templates/snip/edit.html.twig +++ b/templates/snip/edit.html.twig @@ -1,6 +1,6 @@ {% extends 'base/base.html.twig' %} {% block body %} -

{{ snip }}

+

Editing {{ snip }}

{{ form(form) }} {% endblock %} \ No newline at end of file diff --git a/templates/snip/index.html.twig b/templates/snip/index.html.twig index 1ffb1c7..59bbc98 100644 --- a/templates/snip/index.html.twig +++ b/templates/snip/index.html.twig @@ -5,7 +5,7 @@ Create a new Snip
{% for snip in snips %} - + {{ snip }} {% endfor %} diff --git a/templates/snip/single.html.twig b/templates/snip/single.html.twig index e220df8..cd126b9 100644 --- a/templates/snip/single.html.twig +++ b/templates/snip/single.html.twig @@ -1,5 +1,15 @@ {% extends 'base/base.html.twig' %} {% block body %} -

{{ snip }}

+
+

+ + + + {{ snip }} +

+
+

{{ content }}

+
+
{% endblock %} \ No newline at end of file