Change snip content text rebuilding to static, allowing a lot cleaner interface getting the lastest text

This commit is contained in:
Tim 2025-05-13 12:33:25 +02:00
parent 04b2037f2d
commit 62136a0ca0
7 changed files with 25 additions and 29 deletions

View File

@ -29,13 +29,13 @@ class ApiController extends AbstractApiController
}
#[Route('/snip/{snip}', methods: ['GET'])]
public function getSnip(Snip $snip, SnipContentService $cs): Response
public function getSnip(Snip $snip): Response
{
$this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip);
return $this->successResponse([
'id' => $snip->getId(),
'content' => $cs->getActiveText($snip),
'content' => $snip->getActiveText(),
'createdBy' => [
'id' => $snip->getCreatedBy()->getId(),
'name' => $snip->getCreatedBy()->getName(),
@ -66,7 +66,7 @@ class ApiController extends AbstractApiController
return $this->successResponse([
'id' => $snip->getId(),
'name' => $snip->getName(),
'content' => $cs->getActiveText($snip),
'content' => $snip->getActiveText(),
'createdBy' => [
'id' => $snip->getCreatedBy()->getId(),
'name' => $snip->getCreatedBy()->getName(),

View File

@ -13,9 +13,7 @@ use Symfony\Component\Routing\Attribute\Route;
#[Route('/content', name: 'content')]
class SnipContentController extends AbstractController
{
public function __construct(
private readonly SnipContentService $contentService,
) {}
public function __construct() {}
#[Route('/compare/{to}/{from}', name: '_compare')]
public function compare(SnipContent $to, ?SnipContent $from = null): Response
@ -27,8 +25,8 @@ class SnipContentController extends AbstractController
}
$diff = MyersDiff::buildDiffLines(
$this->contentService->rebuildText($from),
$this->contentService->rebuildText($to),
SnipContentService::rebuildText($from),
SnipContentService::rebuildText($to),
);
return $this->render('content/compare.html.twig', [

View File

@ -22,7 +22,6 @@ class SnipController extends AbstractController
{
public function __construct(
private readonly SnipRepository $repository,
private readonly SnipContentService $contentService,
) {}
#[Route('/', name: '_index')]
@ -49,7 +48,7 @@ class SnipController extends AbstractController
return $this->render('snip/single.html.twig', [
'snip' => $snip,
'content' => $pf->getBySnip($snip)->parseView($this->contentService->getActiveText($snip)),
'content' => $pf->getBySnip($snip)->parseView($snip->getActiveText()),
]);
}
@ -59,7 +58,7 @@ class SnipController extends AbstractController
$this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip);
$response = new Response(
$pf->getBySnip($snip)->parseRaw($this->contentService->getActiveText($snip)),
$pf->getBySnip($snip)->parseRaw($snip->getActiveText()),
Response::HTTP_OK,
['Content-Type' => 'text/html']
);
@ -78,7 +77,7 @@ class SnipController extends AbstractController
}
#[Route('/edit/{snip}', name: '_edit')]
public function edit(Snip $snip, Request $request): Response
public function edit(Snip $snip, Request $request, SnipContentService $contentService): Response
{
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
@ -94,7 +93,7 @@ class SnipController extends AbstractController
$form = $this->createForm(SnipType::class, $snip);
$form->add('Save', SubmitType::class);
if ($snip->getId()) {
$form->get('content')->setData($this->contentService->getActiveText($snip));
$form->get('content')->setData($snip->getActiveText());
}
$form->handleRequest($request);
@ -105,7 +104,7 @@ class SnipController extends AbstractController
]);
}
$this->repository->save($snip);
$this->contentService->update($snip, $form->get('content')->getData());
$contentService->update($snip, $form->get('content')->getData());
$this->addFlash('success', sprintf('Snip "%s" saved', $snip));

View File

@ -4,6 +4,7 @@ namespace App\Entity;
use App\Entity\Helpers\TrackedTrait;
use App\Repository\SnipRepository;
use App\Service\SnipContent\SnipContentService;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
@ -56,6 +57,11 @@ class Snip
return $this->name ?? '';
}
public function getActiveText(): string
{
return SnipContentService::rebuildText($this->getActiveVersion());
}
public function getId(): ?int
{
return $this->id;

View File

@ -16,7 +16,7 @@ readonly class SnipContentService
public function update(Snip $snip, string $snipContents): void
{
$parentContent = $snip->getActiveVersion();
if ($this->rebuildText($parentContent) === $snipContents) {
if (self::rebuildText($parentContent) === $snipContents) {
return;
}
@ -39,12 +39,7 @@ readonly class SnipContentService
$this->em->flush();
}
public function getActiveText(Snip $snip): string
{
return $this->rebuildText($snip->getActiveVersion());
}
public function rebuildText(?SnipContent $snipContent): string
public static function rebuildText(?SnipContent $snipContent): string
{
if ($snipContent === null) {
return '';
@ -59,7 +54,7 @@ readonly class SnipContentService
}
return MyersDiff::rebuildBFromCompact(
$this->rebuildText($parentContent), $snipContent->getDiff()
self::rebuildText($parentContent), $snipContent->getDiff()
);
}
@ -80,7 +75,7 @@ readonly class SnipContentService
return;
}
$contentText = $content->getText();
$parentText = $this->rebuildText($content->getParent());
$parentText = self::rebuildText($content->getParent());
$diff = MyersDiff::calculate($parentText, $contentText);
$content->setDiff($diff);
$content->setText(null);
@ -93,7 +88,7 @@ readonly class SnipContentService
if ($content->getDiff() === null) {
return;
}
$content->setText($this->rebuildText($content));
$content->setText(self::rebuildText($content));
$content->setDiff(null);
$this->em->persist($content);
$this->em->flush();

View File

@ -17,7 +17,6 @@ class IncludeReferenceStage implements StageInterface
#[Autowire(lazy: true)] private readonly SnipRepository $snipRepository,
#[Autowire(lazy: true)] private readonly SnipContentRepository $snipContentRepository,
#[Autowire(lazy: true)] private readonly GenericParser $pipeline,
#[Autowire(lazy: true)] private readonly SnipContentService $snipContentService,
) {}
public function __invoke(mixed $payload): string
@ -53,7 +52,7 @@ class IncludeReferenceStage implements StageInterface
}
return $this->pipeline->parseView(
$this->snipContentService->rebuildText($content)
SnipContentService::rebuildText($content)
);
}, $payload);
}

View File

@ -15,13 +15,12 @@ class SnipLoader implements LoaderInterface
{
public function __construct(
private readonly SnipRepository $repository,
private readonly SnipContentService $contentService,
private readonly Security $security,
) {}
public function getSourceContext(string $name): Source
{
return new Source($this->contentService->getActiveText($this->getFromKey($name)), $name);
return new Source($this->getFromKey($name)->getActiveText(), $name);
}
public function getCacheKey(string $name): string