Fully implement snip db storage

This commit is contained in:
Tim
2023-12-17 01:55:26 +01:00
parent 0fce8ee4fe
commit 6107f560e2
11 changed files with 89 additions and 57 deletions

View File

@ -19,15 +19,17 @@ readonly class SnipContentDB implements SnipContentInterface
{
// Create new snipContent entity with previous one as parent
$content = new SnipContent();
$content->setText($snipContents);
$content->setSnip($this->snip);
$content
->setText($snipContents)
->setSnip($this->snip)
;
if ($this->snip->getSnipContents()->count() > 0) {
$content->setParent($this->snip->getSnipContents()->last());
}
$this->em->persist($content);
$this->em->flush();
$this->snip->setActiveCommit($content->getId());
$this->em->persist($this->snip);
$this->em->flush();
@ -35,19 +37,22 @@ readonly class SnipContentDB implements SnipContentInterface
public function get(): string
{
// Return the content of the latest snipContent entity
return $this->snip->getSnipContents()->last()->getText();
$contentRepo = $this->em->getRepository(SnipContent::class);
return $contentRepo->find($this->snip->getActiveCommit())->getText();
}
public function getHistory(): array
public function getVersions(): array
{
// Return all snipContent entities (by parent)
return array_map(fn(SnipContent $content) => $content->getId(), $this->snip->getSnipContents()->toArray());
return array_map(fn(SnipContent $content) => [
'id' => (string)$content->getId(),
'name' => $content->getId()->getDateTime()->format('Y-m-d H:i:s'),
], $this->snip->getSnipContents()->toArray());
}
public function setCommit(string $commit): void
public function setVersion(string $version): void
{
$this->snip->setActiveCommit($commit);
$this->snip->setActiveCommit($version);
$this->em->persist($this->snip);
$this->em->flush();
}
@ -61,4 +66,9 @@ readonly class SnipContentDB implements SnipContentInterface
{
// Cleanup the history
}
public function getLatestVersion(): string
{
return $this->snip->getSnipContents()->last()->getId();
}
}

View File

@ -4,19 +4,18 @@ namespace App\Service\SnipContent;
use App\Entity\User;
use App\Git\CustomGitRepository;
use App\Git\SimpleCommit;
use Symfony\Component\Security\Core\User\UserInterface;
class SnipContentGit implements SnipContentInterface
{
private const SNIP_FILE_NAME = 'snip.txt';
private const MASTER_BRANCH_NAME = 'master';
private const string SNIP_FILE_NAME = 'snip.txt';
private const string MASTER_BRANCH_NAME = 'master';
public function __construct(
private readonly CustomGitRepository $repo,
private readonly ?User $user,
)
{
}
) {}
private function snipExists(): bool
{
@ -51,17 +50,17 @@ class SnipContentGit implements SnipContentInterface
return file_get_contents($this->getSnipPath());
}
/**
* @return array<\App\Git\SimpleCommit>
*/
public function getHistory(): array
public function getVersions(): array
{
return $this->repo->getAllCommits();
return array_map(fn(SimpleCommit $c) => [
'id' => $c->getHash(),
'name' => $c->getDate()->format('Y-m-d H:i:s'),
], $this->repo->getAllCommits());
}
public function setCommit(string $commit): void
public function setVersion(string $version): void
{
$this->repo->checkout($commit);
$this->repo->checkout($version);
}
public function getCommit(): string
@ -73,4 +72,9 @@ class SnipContentGit implements SnipContentInterface
{
system("rm -rf " . escapeshellarg($this->repo->getRepositoryPath()));
}
public function getLatestVersion(): string
{
return self::MASTER_BRANCH_NAME;
}
}

View File

@ -8,11 +8,14 @@ interface SnipContentInterface
public function get(): string;
public function getHistory(): array;
/** @return array{id: string, name: string} */
public function getVersions(): array;
public function setCommit(string $commit): void;
public function setVersion(string $version): void;
public function getCommit(): string;
public function getLatestVersion(): string;
public function delete(): void;
}

View File

@ -4,25 +4,34 @@ namespace App\Service;
use App\Entity\Snip;
use App\Git\CustomGit;
use App\Repository\SnipContentRepository;
use App\Service\SnipContent\SnipContentDB;
use App\Service\SnipContent\SnipContentGit;
use App\Service\SnipContent\SnipContentInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;
class SnipServiceFactory
{
public function __construct(
private readonly string $snipBasePath,
private readonly Security $security,
private readonly SnipContentRepository $snipContentRepository,
)
private readonly string $gitStoragePath,
private readonly string $storageType,
private readonly Security $security,
private readonly EntityManagerInterface $em,
) {}
public function create(Snip $snip): SnipContentInterface
{
return match ($this->storageType) {
'git' => $this->createGit($snip),
'db' => $this->createDB($snip),
default => throw new \Exception('Unknown storage type'),
};
}
public function createGit(Snip $snip): SnipContentGit
private function createGit(Snip $snip): SnipContentGit
{
$git = new CustomGit();
$repoPath = sprintf('%s/%s', $this->snipBasePath, $snip->getId());
$repoPath = sprintf('%s/%s', $this->gitStoragePath, $snip->getId());
if (!is_dir($repoPath)) {
$repo = $git->init($repoPath);
touch(sprintf('%s/.gitignore', $repoPath));
@ -34,8 +43,8 @@ class SnipServiceFactory
return new SnipContentGit($repo, $this->security->getUser());
}
public function createDB(Snip $snip): SnipContentDB
private function createDB(Snip $snip): SnipContentDB
{
return new SnipContentDB($snip, $this->security->getUser(), $this->snipContentRepository);
return new SnipContentDB($snip, $this->security->getUser(), $this->em);
}
}