From 607435bff02ff21dbc793e5d5e5e607a16b4ec2c Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 4 Apr 2023 23:44:10 +0200 Subject: [PATCH] Fix new snip error with git repo Allow simple raw rendering (in html) --- src/Controller/SnipController.php | 39 +++++++++++++++++++++++++------ src/Service/SnipService.php | 2 ++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index e1cb66e..e716654 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -16,7 +16,7 @@ use Symfony\Component\Routing\Annotation\Route; class SnipController extends AbstractController { public function __construct( - private readonly SnipRepository $repository, + private readonly SnipRepository $repository, private readonly SnipServiceFactory $snipServiceFactory, ) { @@ -39,21 +39,44 @@ class SnipController extends AbstractController ]); } + #[Route('/raw/{snip}', name: '_raw')] + public function raw(Snip $snip, Request $request): Response + { + $response = new Response( + $this->snipServiceFactory->create($snip)->get(), + Response::HTTP_OK, + ['Content-Type' => 'text/html'] + ); + $response + ->setVary(['Accept', 'Accept-Encoding']) + ->setEtag(md5($response->getContent())) + ->setTtl(3600) + ->setClientTtl(300); + + if (!$request->isNoCache()) { + $response->isNotModified($request); + } + + return $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); + if ($snip->getId()) { + $form->get('content')->setData($this->snipServiceFactory->create($snip)->get()); + } $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $snip->setCreatedAtTodayNoSeconds() - ->setCreatedBy($this->getUser()); $this->repository->save($snip); - $snipService->update($form->get('content')->getData()); + $this->snipServiceFactory->create($snip)->update($form->get('content')->getData()); $this->addFlash('success', sprintf('Snip "%s" saved successfully', $snip)); @@ -72,6 +95,8 @@ class SnipController extends AbstractController public function new(Request $request): Response { $snip = new Snip(); + $snip->setCreatedAtTodayNoSeconds() + ->setCreatedBy($this->getUser()); return $this->edit($snip, $request); } diff --git a/src/Service/SnipService.php b/src/Service/SnipService.php index 9250d09..f19f39f 100644 --- a/src/Service/SnipService.php +++ b/src/Service/SnipService.php @@ -19,6 +19,7 @@ class SnipService { $git = new Git(); $repoPath = sprintf('%s/%s', $snipBasePath, $snip->getId()); + dump($repoPath, $snip); if (!is_dir($repoPath)) { $this->repo = $git->init($repoPath); } else { @@ -38,6 +39,7 @@ class SnipService public function update(string $snipContents): void { + dump($this->getSnipPath()); file_put_contents($this->getSnipPath(), $snipContents); $this->repo->addFile('snip.txt'); if ($this->repo->hasChanges()) {