From a405578f93e8eab65fb3eaa324fabb828a066df7 Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 15 Apr 2023 13:20:07 +0200 Subject: [PATCH] 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