First fully working version of saving snips content with git control
This commit is contained in:
@ -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));
|
||||
|
||||
|
@ -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')));
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
55
src/Service/SnipService.php
Normal file
55
src/Service/SnipService.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Snip;
|
||||
use App\Entity\User;
|
||||
use CzProject\GitPhp\Git;
|
||||
use CzProject\GitPhp\GitRepository;
|
||||
|
||||
class SnipService
|
||||
{
|
||||
private GitRepository $repo;
|
||||
|
||||
public function __construct(
|
||||
Snip $snip,
|
||||
string $snipBasePath,
|
||||
private readonly User $user,
|
||||
)
|
||||
{
|
||||
$git = new Git();
|
||||
$repoPath = sprintf('%s/%s', $snipBasePath, $snip->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());
|
||||
}
|
||||
}
|
22
src/Service/SnipServiceFactory.php
Normal file
22
src/Service/SnipServiceFactory.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Snip;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
class SnipServiceFactory
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
private readonly string $snipBasePath,
|
||||
private readonly Security $security,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function create(Snip $snip): SnipService
|
||||
{
|
||||
return new SnipService($snip, $this->snipBasePath, $this->security->getUser());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user