Compare commits

..

No commits in common. "master" and "pregitcleanup" have entirely different histories.

39 changed files with 469 additions and 246 deletions

View File

@ -7,6 +7,7 @@
"php": ">=8.3",
"ext-ctype": "*",
"ext-iconv": "*",
"czproject/git-php": "^4.1",
"doctrine/doctrine-bundle": "^2.9",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.14",
@ -46,6 +47,11 @@
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",

74
composer.lock generated
View File

@ -4,8 +4,60 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "59e27d83488f4238ab779a64962a039a",
"content-hash": "0d9cbf5f6f95b13006484d60ccb4d67c",
"packages": [
{
"name": "czproject/git-php",
"version": "v4.2.0",
"source": {
"type": "git",
"url": "https://github.com/czproject/git-php.git",
"reference": "e257f2c3b43fe8fef19ddb5727b604416b423107"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/czproject/git-php/zipball/e257f2c3b43fe8fef19ddb5727b604416b423107",
"reference": "e257f2c3b43fe8fef19ddb5727b604416b423107",
"shasum": ""
},
"require": {
"php": ">=5.6.0"
},
"require-dev": {
"nette/tester": "^2.0"
},
"type": "library",
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Jan Pecha",
"email": "janpecha@email.cz"
}
],
"description": "Library for work with Git repository in PHP.",
"keywords": [
"git"
],
"support": {
"issues": "https://github.com/czproject/git-php/issues",
"source": "https://github.com/czproject/git-php/tree/v4.2.0"
},
"funding": [
{
"url": "https://www.janpecha.cz/donate/git-php/",
"type": "other"
}
],
"time": "2023-07-12T09:14:30+00:00"
},
{
"name": "doctrine/cache",
"version": "2.2.0",
@ -1080,16 +1132,16 @@
},
{
"name": "doctrine/orm",
"version": "2.17.2",
"version": "2.17.1",
"source": {
"type": "git",
"url": "https://github.com/doctrine/orm.git",
"reference": "393679a4795e49b0b3ac317dce84d0f8888f2b77"
"reference": "1a4fe6e0bb67762370937a7e6cee3da40a9122d1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/orm/zipball/393679a4795e49b0b3ac317dce84d0f8888f2b77",
"reference": "393679a4795e49b0b3ac317dce84d0f8888f2b77",
"url": "https://api.github.com/repos/doctrine/orm/zipball/1a4fe6e0bb67762370937a7e6cee3da40a9122d1",
"reference": "1a4fe6e0bb67762370937a7e6cee3da40a9122d1",
"shasum": ""
},
"require": {
@ -1122,10 +1174,10 @@
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.6",
"psr/log": "^1 || ^2 || ^3",
"squizlabs/php_codesniffer": "3.7.2",
"symfony/cache": "^4.4 || ^5.4 || ^6.4 || ^7.0",
"symfony/var-exporter": "^4.4 || ^5.4 || ^6.2 || ^7.0",
"symfony/yaml": "^3.4 || ^4.0 || ^5.0 || ^6.0 || ^7.0",
"vimeo/psalm": "4.30.0 || 5.16.0"
"symfony/cache": "^4.4 || ^5.4 || ^6.0",
"symfony/var-exporter": "^4.4 || ^5.4 || ^6.2",
"symfony/yaml": "^3.4 || ^4.0 || ^5.0 || ^6.0",
"vimeo/psalm": "4.30.0 || 5.15.0"
},
"suggest": {
"ext-dom": "Provides support for XSD validation for XML mapping files",
@ -1175,9 +1227,9 @@
],
"support": {
"issues": "https://github.com/doctrine/orm/issues",
"source": "https://github.com/doctrine/orm/tree/2.17.2"
"source": "https://github.com/doctrine/orm/tree/2.17.1"
},
"time": "2023-12-20T21:47:52+00:00"
"time": "2023-11-17T06:25:40+00:00"
},
{
"name": "doctrine/persistence",

View File

@ -6,7 +6,6 @@ doctrine:
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '15'
orm:
report_fields_where_declared: true
auto_generate_proxy_classes: true
enable_lazy_ghost_objects: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware

View File

@ -4,6 +4,8 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
snipStorageType: 'db' # 'db' or 'git
gitStoragePath: '%kernel.project_dir%/var/snips'
services:
# default configuration for services in *this* file
@ -23,3 +25,8 @@ services:
App\Service\LastRelease:
arguments:
- '%kernel.project_dir%/release.json'
App\Service\SnipServiceFactory:
arguments:
$gitStoragePath: '%gitStoragePath%'
$storageType: '%snipStorageType%'

View File

@ -14,7 +14,7 @@ set('repository', 'git@git.loken.nl:ardent/Snips.git');
set('git_tty', true);
// Shared files/dirs between deploys
set('shared_dirs', ['var/log', 'var/sessions']);
set('shared_dirs', ['var/log', 'var/sessions', 'var/snips']);
set('shared_files', ['.env.local']);
//set('writable_dirs', ['var']);
set('migrations_config', '');
@ -24,7 +24,7 @@ set('allow_anonymous_stats', false);
host('snips.loken.nl')
->setRemoteUser('www-data')
->set('branch', function () {
return input()->getOption('branch') ?: 'master';
return input()->getOption('branch') ?: 'main';
})
->set('deploy_path', '~/snips.loken.nl');
@ -67,7 +67,7 @@ task('deployment:log', function () { //https://stackoverflow.com/questions/59686
$commitDate = $commit[1];
// $line = sprintf('%s %s branch="%s" hash="%s"', $date, $commitHashShort, $branch, $commitHash);
$projectUrlBase = 'https://git.loken.nl/ardent/Snips';
$projectUrlBase = 'https://git.loken.nl/ardent/AnimeRSS4';
$array = [
'branch' => $branch,
'branchUrl' => sprintf('%s/src/branch/%s', $projectUrlBase, $branch),

View File

@ -1,35 +0,0 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231220204107 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE snip ADD active_version_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:ulid)\', DROP active_commit');
$this->addSql('ALTER TABLE snip ADD CONSTRAINT FK_FEBD97966A1E45F3 FOREIGN KEY (active_version_id) REFERENCES snip_content (id)');
$this->addSql('CREATE INDEX IDX_FEBD97966A1E45F3 ON snip (active_version_id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE snip DROP FOREIGN KEY FK_FEBD97966A1E45F3');
$this->addSql('DROP INDEX IDX_FEBD97966A1E45F3 ON snip');
$this->addSql('ALTER TABLE snip ADD active_commit VARCHAR(255) DEFAULT NULL, DROP active_version_id');
}
}

View File

@ -3,15 +3,14 @@
namespace App\Controller;
use App\Entity\Snip;
use App\Entity\SnipContent;
use App\Security\Voter\SnipVoter;
use App\Service\SnipServiceFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/version/{snip}', name: 'version')]
class VersionController extends AbstractController
#[Route('/history/{snip}', name: 'history')]
class HistoryController extends AbstractController
{
public function __construct(
private readonly SnipServiceFactory $snipServiceFactory,
@ -21,19 +20,22 @@ class VersionController extends AbstractController
public function index(Snip $snip): Response
{
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
return $this->render('version/index.html.twig', [
$snipService = $this->snipServiceFactory->create($snip);
return $this->render('history/index.html.twig', [
'snip' => $snip,
'versions' => $snipService->getVersions(),
'latestVersion' => $snipService->getLatestVersion(),
]);
}
#[Route('/set/{version}', name: '_set')]
public function set(Snip $snip, SnipContent $version): Response
public function set(Snip $snip, string $version): Response
{
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
$this->snipServiceFactory->create($snip)->setVersion($version);
$this->addFlash('success', 'Snip version set to ' . $version->getId());
$this->addFlash('success', 'Snip version set to ' . $version);
return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]);
}
}

View File

@ -4,7 +4,7 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{

View File

@ -11,7 +11,7 @@ use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController

View File

@ -13,7 +13,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/snip', name: 'snip')]
class SnipController extends AbstractController
@ -49,10 +49,11 @@ class SnipController extends AbstractController
$this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip);
$snipService = $this->snipServiceFactory->create($snip);
dump($snipService);
return $this->render('snip/single.html.twig', [
'snip' => $snip,
'content' => $pl->parse($snipService->getActiveText()),
'content' => $pl->parse($snipService->get()),
'branch' => $snipService->getCommit(),
]);
}
@ -62,7 +63,7 @@ class SnipController extends AbstractController
$this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip);
$response = new Response(
$pl->clean($this->snipServiceFactory->create($snip)->getActiveText()),
$pl->clean($this->snipServiceFactory->create($snip)->get()),
Response::HTTP_OK,
['Content-Type' => 'text/html']
);
@ -87,7 +88,7 @@ class SnipController extends AbstractController
$form = $this->createForm(SnipType::class, $snip);
$form->add('Save', SubmitType::class);
if ($snip->getId()) {
$form->get('content')->setData($this->snipServiceFactory->create($snip)->getActiveText());
$form->get('content')->setData($this->snipServiceFactory->create($snip)->get());
}
$form->handleRequest($request);
@ -112,7 +113,7 @@ class SnipController extends AbstractController
public function new(Request $request): Response
{
$snip = new Snip();
$snip->setCreatedAtNow()
$snip->setCreatedAtTodayNoSeconds()
->setCreatedBy($this->getUser());
return $this->edit($snip, $request);
@ -132,7 +133,7 @@ class SnipController extends AbstractController
return $this->redirectToRoute('snip_index');
}
return $this->render('generic/form.html.twig', [
return $this->render('form.html.twig', [
'message' => sprintf('Do you really want to delete "%s"?', $snip),
'form' => $form->createView(),
]);

View File

@ -2,6 +2,7 @@
namespace App\Controller;
use App\Entity\User;
use App\Form\ProfileType;
use App\Form\UserSettingsType;
use App\Service\LastRelease;
@ -10,7 +11,8 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Uid\Uuid;
#[Route('/user', name: 'user')]
class UserController extends AbstractController

View File

@ -40,17 +40,10 @@ trait TrackedTrait
return $this;
}
public function setCreatedAtNowNoSeconds(): self
public function setCreatedAtTodayNoSeconds(): self
{
$this->setCreatedAt(DateTime::createFromFormat('Y-m-d H:i', date('Y-m-d H:i')));
return $this;
}
public function setCreatedAtNow(): self
{
$this->setCreatedAt(new DateTime());
return $this;
}
}

View File

@ -27,8 +27,8 @@ class Snip
#[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)]
private Collection $snipContents;
#[ORM\ManyToOne]
private ?SnipContent $activeVersion = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $activeCommit = null;
public function __construct()
{
@ -99,19 +99,14 @@ class Snip
return $this;
}
public function getLatestVersion(): ?SnipContent
public function getActiveCommit(): ?string
{
return $this->snipContents->last();
return $this->activeCommit;
}
public function getActiveVersion(): ?SnipContent
public function setActiveCommit(?string $activeCommit): static
{
return $this->activeVersion;
}
public function setActiveVersion(?SnipContent $activeVersion): static
{
$this->activeVersion = $activeVersion;
$this->activeCommit = $activeCommit;
return $this;
}

13
src/Git/CustomGit.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace App\Git;
use CzProject\GitPhp\Git;
class CustomGit extends Git
{
public function open($directory): CustomGitRepository
{
return new CustomGitRepository($directory, $this->runner);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Git;
use CzProject\GitPhp\GitRepository;
use DateTime;
class CustomGitRepository extends GitRepository
{
/**
* @return array<SimpleCommit>
* @throws \CzProject\GitPhp\GitException
*/
public function getAllCommits(): array
{
$result = $this->run('log', '--pretty=%H,%cI');
if (empty($result->getOutput())) {
return [];
}
$commits = [];
foreach ($result->getOutput() as $line) {
$parts = explode(',', $line);
$commits[] = new SimpleCommit(
$parts[0],
new DateTime($parts[1])
);
}
return $commits;
}
}

26
src/Git/SimpleCommit.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace App\Git;
use DateTime;
class SimpleCommit
{
public function __construct(
private readonly string $hash,
private readonly DateTime $date,
)
{
}
public function getHash(): string
{
return $this->hash;
}
public function getDate(): DateTime
{
return $this->date;
}
}

0
src/Repository/.gitignore vendored Normal file
View File

View File

@ -0,0 +1,74 @@
<?php
namespace App\Service\SnipContent;
use App\Entity\Snip;
use App\Entity\SnipContent;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
readonly class SnipContentDB implements SnipContentInterface
{
public function __construct(
private Snip $snip,
private User $user,
private EntityManagerInterface $em,
) {}
public function update(string $snipContents): void
{
// Create new snipContent entity with previous one as parent
$content = new SnipContent();
$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();
}
public function get(): string
{
$contentRepo = $this->em->getRepository(SnipContent::class);
return $contentRepo->find($this->snip->getActiveCommit())->getText();
}
public function getVersions(): array
{
// Return all snipContent entities (by parent)
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 setVersion(string $version): void
{
$this->snip->setActiveCommit($version);
$this->em->persist($this->snip);
$this->em->flush();
}
public function getCommit(): string
{
return $this->snip->getActiveCommit();
}
public function delete(): void
{
// Cleanup the history
}
public function getLatestVersion(): string
{
return $this->snip->getSnipContents()->last()->getId();
}
}

View File

@ -0,0 +1,80 @@
<?php
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 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
{
return file_exists($this->getSnipPath());
}
private function getSnipPath(): string
{
return sprintf('%s/snip.txt', $this->repo->getRepositoryPath());
}
public function update(string $snipContents): void
{
if (!$this->user instanceof UserInterface) {
return;
}
if ($this->repo->getCurrentBranchName() !== self::MASTER_BRANCH_NAME) {
$this->repo->checkout(self::MASTER_BRANCH_NAME);
}
file_put_contents($this->getSnipPath(), $snipContents);
$this->repo->addFile(self::SNIP_FILE_NAME);
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());
}
public function getVersions(): array
{
return array_map(fn(SimpleCommit $c) => [
'id' => $c->getHash(),
'name' => $c->getDate()->format('Y-m-d H:i:s'),
], $this->repo->getAllCommits());
}
public function setVersion(string $version): void
{
$this->repo->checkout($version);
}
public function getCommit(): string
{
return $this->repo->getCurrentBranchName();
}
public function delete(): void
{
system("rm -rf " . escapeshellarg($this->repo->getRepositoryPath()));
}
public function getLatestVersion(): string
{
return self::MASTER_BRANCH_NAME;
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Service\SnipContent;
interface SnipContentInterface
{
public function update(string $snipContents): void;
public function get(): string;
/** @return array{id: string, name: string} */
public function getVersions(): array;
public function setVersion(string $version): void;
public function getCommit(): string;
public function getLatestVersion(): string;
public function delete(): void;
}

View File

@ -1,57 +0,0 @@
<?php
namespace App\Service\SnipContent;
use App\Entity\Snip;
use App\Entity\SnipContent;
use Doctrine\ORM\EntityManagerInterface;
readonly class SnipContentService
{
public function __construct(
private Snip $snip,
private EntityManagerInterface $em,
) {}
public function update(string $snipContents): void
{
if ($this->snip->getActiveVersion()?->getText() === $snipContents) {
return;
}
// Create new snipContent entity with previous one as parent
$content = new SnipContent();
$content
->setText($snipContents)
->setSnip($this->snip)
;
if ($this->snip->getActiveVersion() !== null) {
$content->setParent($this->snip->getActiveVersion());
}
$this->em->persist($content);
$this->em->flush();
$this->snip->setActiveVersion($content);
$this->em->persist($this->snip);
$this->em->flush();
}
// Shortcut to get the active text
public function getActiveText(): string
{
$contentRepo = $this->em->getRepository(SnipContent::class);
return $contentRepo->find($this->snip->getActiveVersion())->getText();
}
public function setVersion(SnipContent $version): void
{
$this->snip->setActiveVersion($version);
$this->em->persist($this->snip);
$this->em->flush();
}
public function delete(): void
{
// Cleanup the versions
}
}

View File

@ -2,10 +2,10 @@
namespace App\Service\SnipParser\Stages;
use App\Repository\SnipContentRepository;
use App\Repository\SnipRepository;
use App\Security\Voter\SnipVoter;
use App\Service\SnipParser\Pipeline;
use App\Service\SnipServiceFactory;
use League\Pipeline\StageInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
@ -13,10 +13,10 @@ use Symfony\Component\DependencyInjection\Attribute\Autowire;
class IncludeReferenceStage implements StageInterface
{
public function __construct(
#[Autowire(lazy: true)] private readonly Security $security,
#[Autowire(lazy: true)] private readonly SnipRepository $snipRepository,
#[Autowire(lazy: true)] private readonly SnipContentRepository $snipContentRepository,
#[Autowire(lazy: true)] private readonly Pipeline $pipeline,
#[Autowire(lazy: true)] private readonly Security $security,
#[Autowire(lazy: true)] private readonly SnipRepository $snipRepository,
#[Autowire(lazy: true)] private readonly SnipServiceFactory $snipServiceFactory,
#[Autowire(lazy: true)] private readonly Pipeline $pipeline,
) {}
public function __invoke(mixed $payload): string
@ -26,32 +26,19 @@ class IncludeReferenceStage implements StageInterface
private function replaceReferences(mixed $payload): string
{
// replaces all references ({{ID}}) with the content of the snip
$pattern = '/\{\{([A-Z0-9]+)\}\}/';
// replaces all references (#n) to other snips with links
$pattern = '/\{\{(\d+)\}\}/';
return preg_replace_callback($pattern, function ($matches) {
$id = $matches[1];
try {
$content = $this->snipContentRepository->find($id);
} catch (\Exception) {
$content = null;
}
if ($content) {
$snip = $content->getSnip();
} else {
$snip = $this->snipRepository->find($id);
if ($snip) {
$content = $this->snipContentRepository->find($snip->getActiveVersion());
}
}
if ($content === null) {
return sprintf('<span title="snip or content not found">%s</span>', $matches[0]);
$snip = $this->snipRepository->find($matches[1]);
if ($snip === null) {
return sprintf('<span title="not found">%s</span>', $matches[0]);
}
if (!$this->security->isGranted(SnipVoter::VIEW, $snip)) {
return sprintf('<span title="access denied">%s</span>', $matches[0]);
}
return $this->pipeline->parse($content->getText());
return $this->pipeline->parse($this->snipServiceFactory->create($snip)->get());
}, $payload);
}
}

View File

@ -4,12 +4,12 @@ namespace App\Service\SnipParser\Stages;
use League\Pipeline\StageInterface;
readonly class ReplaceStage implements StageInterface
class ReplaceStage implements StageInterface
{
// replaces a string with another string
public function __construct(
public string $search,
public string $replace,
public readonly string $search,
public readonly string $replace,
) {}
public function __invoke(mixed $payload): string

View File

@ -9,12 +9,12 @@ use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
readonly class UrlReferenceStage implements StageInterface
class UrlReferenceStage implements StageInterface
{
public function __construct(
#[Autowire(lazy: true)] private UrlGeneratorInterface $router,
#[Autowire(lazy: true)] private Security $security,
#[Autowire(lazy: true)] private SnipRepository $snipRepository,
#[Autowire(lazy: true)] private readonly UrlGeneratorInterface $router,
#[Autowire(lazy: true)] private readonly Security $security,
#[Autowire(lazy: true)] private readonly SnipRepository $snipRepository,
) {}
public function __invoke(mixed $payload): string

View File

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

View File

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% if title is defined %}{{ title }}{% else %}SNIPS{% endif %}</title>
<title>{% block title %}SNIPS{% endblock %}</title>
<link rel="shortcut icon" type="image/jpg" href="/favicon.png">
{% block css %}
@ -37,7 +37,19 @@
</div>
{# body blocks #}
{% block content %}
{% block bodyraw %}
<div class="container">
<div class="row">
<div class="col-sm mx-auto">
{% block body %}{% endblock %}
</div>
{% if block('body2') is defined %}
<div class="col-sm mx-auto">
{{ block('body2') }}
</div>
{% endif %}
</div>
</div>
{% endblock %}
{# javascript block #}

View File

@ -1,8 +0,0 @@
{% extends 'base/base.html.twig' %}
{% block content %}
<div class="container">
{% block container %}
{% endblock %}
</div>
{% endblock %}

View File

@ -1,10 +0,0 @@
{% extends 'base/container.html.twig' %}
{% block container %}
<div class="row">
<div class="col-sm mx-auto">
{% if title is defined %}<h3>{{ title }}</h3>{% endif %}
{% block body %}{% endblock %}
</div>
</div>
{% endblock %}

6
templates/form.html.twig Normal file
View File

@ -0,0 +1,6 @@
{% extends 'base/base.html.twig' %}
{% block body %}
<h3>{{ message }}</h3>
{{ form(form) }}
{% endblock %}

View File

@ -1,7 +0,0 @@
{% extends 'base/single.column.html.twig' %}
{% set title %}{{ message }}{% endset %}
{% block body %}
{{ form(form) }}
{% endblock %}

View File

@ -1,5 +0,0 @@
{% extends 'base/single.column.html.twig' %}
{% block body %}
{{ text | nl2br }}
{% endblock %}

View File

@ -1,4 +1,4 @@
{% extends 'base/single.column.html.twig' %}
{% extends 'base/base.html.twig' %}
{% block title %}Snip {{ snip }}{% endblock %}
@ -6,14 +6,14 @@
<a href="{{ path('snip_single', {snip: snip.id}) }}" class="btn btn-primary">
<i class="fa fa-arrow-left"></i> Back
</a>
<a href="{{ path('version_set', {version: snip.latestVersion.id, snip: snip.id}) }}" class="btn btn-warning">
<a href="{{ path('history_set', {version: latestVersion, snip: snip.id}) }}" class="btn btn-warning">
<i class="fa fa-refresh"></i> Latest
</a>
<br><br>
<div class="list-group">
{% for version in snip.snipContents %}
<a class="list-group-item" href="{{ path('version_set', {version: version.id, snip: snip.id}) }}">
{{ version.id.dateTime|date('Y-m-d H:i:s') }} - {{ version.id }}
{% for version in versions %}
<a class="list-group-item" href="{{ path('history_set', {version: version.id, snip: snip.id}) }}">
{{ version.name }} - {{ version.id }}
</a>
{% endfor %}
</div>

View File

@ -1,6 +1,6 @@
{% extends 'base/single.column.html.twig' %}
{% extends 'base/base.html.twig' %}
{% set title %}Login{% endset %}
{% block title %}Login{% endblock %}
{% block body %}
<form action="{{ path('login') }}" method="post">
@ -12,6 +12,7 @@
You are already logged in as {{ app.user }}, <a href="{{ path('logout') }}">Logout</a>
</div>
{% endif %}
<h1 class="h3 mb-3 font-weight-normal">Please login</h1>
<label for="inputUsername">Username</label>
<input type="text" value="{{ last_username }}" name="_username" id="inputUsername" class="form-control" required
autofocus>

View File

@ -1,7 +1,8 @@
{% extends 'base/single.column.html.twig' %}
{% extends 'base/base.html.twig' %}
{% set title %}Register{% endset %}
{% block title %}Register{% endblock %}
{% block body %}
<h1 class="h3 mb-3 font-weight-normal">Register new user</h1>
{{ form(registrationForm) }}
{% endblock %}

View File

@ -0,0 +1,5 @@
{% extends 'base/base.html.twig' %}
{% block body %}
{{ text | nl2br }}
{% endblock %}

View File

@ -1,6 +1,6 @@
{% extends 'base/single.column.html.twig' %}
{% extends 'base/base.html.twig' %}
{% set title %}Edit {{ snip }}{% endset %}
{% block title %}Edit {{ snip }}{% endblock %}
{% block body %}
{% if snip.id %}
@ -13,5 +13,6 @@
<i class="fa fa-list"></i>
Index
</a><br><br>
<h2>Editing {{ snip }}</h2>
{{ form(form) }}
{% endblock %}

View File

@ -1,19 +1,19 @@
{% extends 'base/single.column.html.twig' %}
{% extends 'base/base.html.twig' %}
{% block title %}{{ title }}{% endblock %}
{% block body %}
<h1>{{ title }}</h1>
<a class="btn btn-success" href="{{ path('snip_new') }}">
<i class="fa fa-plus"></i> Add
</a>
<br><br>
</a><br><br>
<div class="list-group">
{% for snip in snips %}
<a class="list-group-item d-flex justify-content-between" href="{{ path('snip_single', {snip: snip.id}) }}">
<span>
{% if snip.createdBy == app.user %}
{{ include('snip/badge.html.twig', {snip: snip}) }}
{% endif %}
{{ snip }}
</span>
<a class="list-group-item" href="{{ path('snip_single', {snip: snip.id}) }}">
{% if snip.createdBy == app.user %}
{{ include('snip/badge.html.twig', {snip: snip}) }}
{% endif %}
{{ snip }}
{% if snip.createdBy != app.user %}
{{ include('user/badge.html.twig', {user: snip.createdBy}) }}
{% endif %}

View File

@ -1,6 +1,6 @@
{% extends 'base/single.column.html.twig' %}
{% extends 'base/base.html.twig' %}
{% set title %}Snip {{ snip }}{% endset %}
{% block title %}Snip {{ snip }}{% endblock %}
{% block body %}
<a href="{{ path('snip_index') }}" class="btn btn-primary">
@ -10,8 +10,8 @@
<a class="btn btn-warning" href="{{ path('snip_edit', {snip: snip.id}) }}">
<i class="fa fa-pencil" aria-hidden="true"></i> Edit
</a>
<a class="btn btn-info" href="{{ path('version_index', {snip: snip.id}) }}">
<i class="fa fa-history" aria-hidden="true"></i> Versions
<a class="btn btn-info" href="{{ path('history_index', {snip: snip.id}) }}">
<i class="fa fa-history" aria-hidden="true"></i> History
</a>
<a href="{{ path('snip_delete', {snip: snip.id}) }}" class="btn btn-danger">
<i class="fa fa-trash"></i> Delete
@ -26,15 +26,12 @@
{{ include('snip/badge.html.twig', {snip: snip}) }}
{{ snip }} <small class="text-muted">#{{ snip.id }}</small>
</h4>
<div class="card-header">
<p class="card-text">Current version: {{ branch }}</p>
</div>
<div class="card-body">
{{ content|raw }}
</div>
<div class="card-footer">
<p class="card-text text-muted">
Current version: {{ snip.activeVersion.id }}
{% if snip.activeVersion == snip.latestVersion %}(latest){% endif %}
</p>
</div>
</div>
{% endblock %}

View File

@ -1,9 +1,9 @@
{% extends 'base/single.column.html.twig' %}
{% extends "base/base.html.twig" %}
{% block body %}
<div class="row">
<div class="col-sm">
<h3>{{ app.user.name }}</h3>
<h4>{{ app.user.name }}</h4>
<br/>
{% if is_granted('ROLE_ADMIN') %}
<br/><br/>
@ -16,7 +16,7 @@
{% endif %}
</div>
<div class="col-sm">
<h3>Change profile</h3>
<h4>Change profile</h4>
{{ form(form) }}
</div>
</div>