From 64bd7e364252b74f45da49dee9eddc837a96858d Mon Sep 17 00:00:00 2001 From: tim Date: Mon, 18 Dec 2023 01:26:44 +0100 Subject: [PATCH] Start on removing git and better integrate db --- composer.json | 1 - composer.lock | 54 +------------ config/services.yaml | 7 -- deploy.php | 2 +- src/Controller/SnipController.php | 11 +-- ...ryController.php => VersionController.php} | 13 +-- src/Git/CustomGit.php | 13 --- src/Git/CustomGitRepository.php | 33 -------- src/Git/SimpleCommit.php | 26 ------ src/Repository/.gitignore | 0 src/Service/SnipContent/SnipContentGit.php | 80 ------------------- .../SnipContent/SnipContentInterface.php | 21 ----- ...ipContentDB.php => SnipContentService.php} | 12 +-- .../Stages/IncludeReferenceStage.php | 35 +++++--- src/Service/SnipServiceFactory.php | 37 +-------- templates/snip/single.html.twig | 13 +-- .../{history => version}/index.html.twig | 4 +- 17 files changed, 58 insertions(+), 304 deletions(-) rename src/Controller/{HistoryController.php => VersionController.php} (79%) delete mode 100644 src/Git/CustomGit.php delete mode 100644 src/Git/CustomGitRepository.php delete mode 100644 src/Git/SimpleCommit.php delete mode 100644 src/Repository/.gitignore delete mode 100644 src/Service/SnipContent/SnipContentGit.php delete mode 100644 src/Service/SnipContent/SnipContentInterface.php rename src/Service/SnipContent/{SnipContentDB.php => SnipContentService.php} (87%) rename templates/{history => version}/index.html.twig (79%) diff --git a/composer.json b/composer.json index f7fbeb6..006d61a 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,6 @@ "php": ">=8.3", "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 d15a48c..757823e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,60 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0d9cbf5f6f95b13006484d60ccb4d67c", + "content-hash": "59e27d83488f4238ab779a64962a039a", "packages": [ - { - "name": "czproject/git-php", - "version": "v4.2.0", - "source": { - "type": "git", - "url": "https://github.com/czproject/git-php.git", - "reference": "e257f2c3b43fe8fef19ddb5727b604416b423107" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/czproject/git-php/zipball/e257f2c3b43fe8fef19ddb5727b604416b423107", - "reference": "e257f2c3b43fe8fef19ddb5727b604416b423107", - "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.2.0" - }, - "funding": [ - { - "url": "https://www.janpecha.cz/donate/git-php/", - "type": "other" - } - ], - "time": "2023-07-12T09:14:30+00:00" - }, { "name": "doctrine/cache", "version": "2.2.0", diff --git a/config/services.yaml b/config/services.yaml index bf61fd9..5268342 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -4,8 +4,6 @@ # 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 @@ -25,8 +23,3 @@ services: App\Service\LastRelease: arguments: - '%kernel.project_dir%/release.json' - - App\Service\SnipServiceFactory: - arguments: - $gitStoragePath: '%gitStoragePath%' - $storageType: '%snipStorageType%' \ No newline at end of file diff --git a/deploy.php b/deploy.php index 9860a13..14ac384 100644 --- a/deploy.php +++ b/deploy.php @@ -67,7 +67,7 @@ task('deployment:log', function () { //https://stackoverflow.com/questions/59686 $commitDate = $commit[1]; // $line = sprintf('%s %s branch="%s" hash="%s"', $date, $commitHashShort, $branch, $commitHash); - $projectUrlBase = 'https://git.loken.nl/ardent/AnimeRSS4'; + $projectUrlBase = 'https://git.loken.nl/ardent/Snips'; $array = [ 'branch' => $branch, 'branchUrl' => sprintf('%s/src/branch/%s', $projectUrlBase, $branch), diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index 4c2ad59..4e07730 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -49,11 +49,12 @@ class SnipController extends AbstractController $this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip); $snipService = $this->snipServiceFactory->create($snip); - dump($snipService); + return $this->render('snip/single.html.twig', [ 'snip' => $snip, - 'content' => $pl->parse($snipService->get()), - 'branch' => $snipService->getCommit(), + 'content' => $pl->parse($snipService->getActiveText()), + 'activeVersion' => $snipService->getActiveVersion(), + 'latestVersion' => $snipService->getLatestVersion(), ]); } @@ -63,7 +64,7 @@ class SnipController extends AbstractController $this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip); $response = new Response( - $pl->clean($this->snipServiceFactory->create($snip)->get()), + $pl->clean($this->snipServiceFactory->create($snip)->getActiveText()), Response::HTTP_OK, ['Content-Type' => 'text/html'] ); @@ -88,7 +89,7 @@ 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->create($snip)->getActiveText()); } $form->handleRequest($request); diff --git a/src/Controller/HistoryController.php b/src/Controller/VersionController.php similarity index 79% rename from src/Controller/HistoryController.php rename to src/Controller/VersionController.php index 1a08bd3..56ee259 100644 --- a/src/Controller/HistoryController.php +++ b/src/Controller/VersionController.php @@ -3,14 +3,15 @@ namespace App\Controller; use App\Entity\Snip; +use App\Entity\SnipContent; use App\Security\Voter\SnipVoter; use App\Service\SnipServiceFactory; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -#[Route('/history/{snip}', name: 'history')] -class HistoryController extends AbstractController +#[Route('/version/{snip}', name: 'version')] +class VersionController extends AbstractController { public function __construct( private readonly SnipServiceFactory $snipServiceFactory, @@ -22,7 +23,7 @@ class HistoryController extends AbstractController $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); $snipService = $this->snipServiceFactory->create($snip); - return $this->render('history/index.html.twig', [ + return $this->render('version/index.html.twig', [ 'snip' => $snip, 'versions' => $snipService->getVersions(), 'latestVersion' => $snipService->getLatestVersion(), @@ -30,12 +31,12 @@ class HistoryController extends AbstractController } #[Route('/set/{version}', name: '_set')] - public function set(Snip $snip, string $version): Response + public function set(Snip $snip, SnipContent $version): Response { $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); - $this->snipServiceFactory->create($snip)->setVersion($version); - $this->addFlash('success', 'Snip version set to ' . $version); + $this->snipServiceFactory->create($snip)->setVersion($version->getId()); + $this->addFlash('success', 'Snip version set to ' . $version->getId()); return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]); } } \ No newline at end of file diff --git a/src/Git/CustomGit.php b/src/Git/CustomGit.php deleted file mode 100644 index e7350e3..0000000 --- a/src/Git/CustomGit.php +++ /dev/null @@ -1,13 +0,0 @@ -runner); - } -} diff --git a/src/Git/CustomGitRepository.php b/src/Git/CustomGitRepository.php deleted file mode 100644 index 6b5585c..0000000 --- a/src/Git/CustomGitRepository.php +++ /dev/null @@ -1,33 +0,0 @@ - - * @throws \CzProject\GitPhp\GitException - */ - public function getAllCommits(): array - { - $result = $this->run('log', '--pretty=%H,%cI'); - - if (empty($result->getOutput())) { - return []; - } - - $commits = []; - foreach ($result->getOutput() as $line) { - $parts = explode(',', $line); - $commits[] = new SimpleCommit( - $parts[0], - new DateTime($parts[1]) - ); - } - - return $commits; - } -} \ No newline at end of file diff --git a/src/Git/SimpleCommit.php b/src/Git/SimpleCommit.php deleted file mode 100644 index 254c846..0000000 --- a/src/Git/SimpleCommit.php +++ /dev/null @@ -1,26 +0,0 @@ -hash; - } - - public function getDate(): DateTime - { - return $this->date; - } -} \ No newline at end of file diff --git a/src/Repository/.gitignore b/src/Repository/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/src/Service/SnipContent/SnipContentGit.php b/src/Service/SnipContent/SnipContentGit.php deleted file mode 100644 index 005ff3f..0000000 --- a/src/Service/SnipContent/SnipContentGit.php +++ /dev/null @@ -1,80 +0,0 @@ -getSnipPath()); - } - - private function getSnipPath(): string - { - return sprintf('%s/snip.txt', $this->repo->getRepositoryPath()); - } - - public function update(string $snipContents): void - { - if (!$this->user instanceof UserInterface) { - return; - } - if ($this->repo->getCurrentBranchName() !== self::MASTER_BRANCH_NAME) { - $this->repo->checkout(self::MASTER_BRANCH_NAME); - } - file_put_contents($this->getSnipPath(), $snipContents); - $this->repo->addFile(self::SNIP_FILE_NAME); - 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()); - } - - public function getVersions(): array - { - return array_map(fn(SimpleCommit $c) => [ - 'id' => $c->getHash(), - 'name' => $c->getDate()->format('Y-m-d H:i:s'), - ], $this->repo->getAllCommits()); - } - - public function setVersion(string $version): void - { - $this->repo->checkout($version); - } - - public function getCommit(): string - { - return $this->repo->getCurrentBranchName(); - } - - public function delete(): void - { - 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 deleted file mode 100644 index f687eb5..0000000 --- a/src/Service/SnipContent/SnipContentInterface.php +++ /dev/null @@ -1,21 +0,0 @@ -em->flush(); } - public function get(): string + // Shortcut to get the active text + public function getActiveText(): string { $contentRepo = $this->em->getRepository(SnipContent::class); return $contentRepo->find($this->snip->getActiveCommit())->getText(); } + /** @return array{id: string, name: string} */ public function getVersions(): array { // Return all snipContent entities (by parent) @@ -57,14 +57,14 @@ readonly class SnipContentDB implements SnipContentInterface $this->em->flush(); } - public function getCommit(): string + public function getActiveVersion(): string { return $this->snip->getActiveCommit(); } public function delete(): void { - // Cleanup the history + // Cleanup the versions } public function getLatestVersion(): string diff --git a/src/Service/SnipParser/Stages/IncludeReferenceStage.php b/src/Service/SnipParser/Stages/IncludeReferenceStage.php index 03c1336..0405c4d 100644 --- a/src/Service/SnipParser/Stages/IncludeReferenceStage.php +++ b/src/Service/SnipParser/Stages/IncludeReferenceStage.php @@ -2,10 +2,10 @@ namespace App\Service\SnipParser\Stages; +use App\Repository\SnipContentRepository; use App\Repository\SnipRepository; use App\Security\Voter\SnipVoter; use App\Service\SnipParser\Pipeline; -use App\Service\SnipServiceFactory; use League\Pipeline\StageInterface; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -13,10 +13,10 @@ use Symfony\Component\DependencyInjection\Attribute\Autowire; class IncludeReferenceStage implements StageInterface { public function __construct( - #[Autowire(lazy: true)] private readonly Security $security, - #[Autowire(lazy: true)] private readonly SnipRepository $snipRepository, - #[Autowire(lazy: true)] private readonly SnipServiceFactory $snipServiceFactory, - #[Autowire(lazy: true)] private readonly Pipeline $pipeline, + #[Autowire(lazy: true)] private readonly Security $security, + #[Autowire(lazy: true)] private readonly SnipRepository $snipRepository, + #[Autowire(lazy: true)] private readonly SnipContentRepository $snipContentRepository, + #[Autowire(lazy: true)] private readonly Pipeline $pipeline, ) {} public function __invoke(mixed $payload): string @@ -26,19 +26,32 @@ class IncludeReferenceStage implements StageInterface private function replaceReferences(mixed $payload): string { - // replaces all references (#n) to other snips with links - $pattern = '/\{\{(\d+)\}\}/'; + // replaces all references ({{ID}}) with the content of the snip + $pattern = '/\{\{([A-Z0-9]+)\}\}/'; return preg_replace_callback($pattern, function ($matches) { - $snip = $this->snipRepository->find($matches[1]); - if ($snip === null) { - return sprintf('%s', $matches[0]); + $id = $matches[1]; + try { + $content = $this->snipContentRepository->find($id); + } catch (\Exception) { + $content = null; + } + if ($content) { + $snip = $content->getSnip(); + } else { + $snip = $this->snipRepository->find($id); + if ($snip) { + $content = $this->snipContentRepository->find($snip->getActiveCommit()); + } + } + if ($content === null) { + return sprintf('%s', $matches[0]); } if (!$this->security->isGranted(SnipVoter::VIEW, $snip)) { return sprintf('%s', $matches[0]); } - return $this->pipeline->parse($this->snipServiceFactory->create($snip)->get()); + return $this->pipeline->parse($content->getText()); }, $payload); } } \ No newline at end of file diff --git a/src/Service/SnipServiceFactory.php b/src/Service/SnipServiceFactory.php index 1c85c18..1359beb 100644 --- a/src/Service/SnipServiceFactory.php +++ b/src/Service/SnipServiceFactory.php @@ -3,48 +3,17 @@ namespace App\Service; use App\Entity\Snip; -use App\Git\CustomGit; -use App\Service\SnipContent\SnipContentDB; -use App\Service\SnipContent\SnipContentGit; -use App\Service\SnipContent\SnipContentInterface; +use App\Service\SnipContent\SnipContentService; use Doctrine\ORM\EntityManagerInterface; -use Symfony\Bundle\SecurityBundle\Security; class SnipServiceFactory { public function __construct( - private readonly string $gitStoragePath, - private readonly string $storageType, - private readonly Security $security, private readonly EntityManagerInterface $em, ) {} - public function create(Snip $snip): SnipContentInterface + public function create(Snip $snip): SnipContentService { - return match ($this->storageType) { - 'git' => $this->createGit($snip), - 'db' => $this->createDB($snip), - default => throw new \Exception('Unknown storage type'), - }; - } - - private function createGit(Snip $snip): SnipContentGit - { - $git = new CustomGit(); - $repoPath = sprintf('%s/%s', $this->gitStoragePath, $snip->getId()); - if (!is_dir($repoPath)) { - $repo = $git->init($repoPath); - touch(sprintf('%s/.gitignore', $repoPath)); - $repo->addFile('.gitignore'); - $repo->commit('Initial commit'); - } else { - $repo = $git->open($repoPath); - } - return new SnipContentGit($repo, $this->security->getUser()); - } - - private function createDB(Snip $snip): SnipContentDB - { - return new SnipContentDB($snip, $this->security->getUser(), $this->em); + return new SnipContentService($snip, $this->em); } } \ No newline at end of file diff --git a/templates/snip/single.html.twig b/templates/snip/single.html.twig index b1d0c7a..eca4897 100644 --- a/templates/snip/single.html.twig +++ b/templates/snip/single.html.twig @@ -10,8 +10,8 @@ Edit - - History + + Versions Delete @@ -26,12 +26,15 @@ {{ include('snip/badge.html.twig', {snip: snip}) }} {{ snip }} #{{ snip.id }} -
-

Current version: {{ branch }}

-
{{ content|raw }}
+ {% endblock %} diff --git a/templates/history/index.html.twig b/templates/version/index.html.twig similarity index 79% rename from templates/history/index.html.twig rename to templates/version/index.html.twig index 79714ec..2b142cc 100644 --- a/templates/history/index.html.twig +++ b/templates/version/index.html.twig @@ -6,13 +6,13 @@
Back - + Latest

{% for version in versions %} - + {{ version.name }} - {{ version.id }} {% endfor %}