Compare commits

..

9 Commits

39 changed files with 246 additions and 469 deletions

View File

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

74
composer.lock generated
View File

@ -4,60 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "0d9cbf5f6f95b13006484d60ccb4d67c", "content-hash": "59e27d83488f4238ab779a64962a039a",
"packages": [ "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", "name": "doctrine/cache",
"version": "2.2.0", "version": "2.2.0",
@ -1132,16 +1080,16 @@
}, },
{ {
"name": "doctrine/orm", "name": "doctrine/orm",
"version": "2.17.1", "version": "2.17.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/doctrine/orm.git", "url": "https://github.com/doctrine/orm.git",
"reference": "1a4fe6e0bb67762370937a7e6cee3da40a9122d1" "reference": "393679a4795e49b0b3ac317dce84d0f8888f2b77"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/doctrine/orm/zipball/1a4fe6e0bb67762370937a7e6cee3da40a9122d1", "url": "https://api.github.com/repos/doctrine/orm/zipball/393679a4795e49b0b3ac317dce84d0f8888f2b77",
"reference": "1a4fe6e0bb67762370937a7e6cee3da40a9122d1", "reference": "393679a4795e49b0b3ac317dce84d0f8888f2b77",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1174,10 +1122,10 @@
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.6", "phpunit/phpunit": "^7.5 || ^8.5 || ^9.6",
"psr/log": "^1 || ^2 || ^3", "psr/log": "^1 || ^2 || ^3",
"squizlabs/php_codesniffer": "3.7.2", "squizlabs/php_codesniffer": "3.7.2",
"symfony/cache": "^4.4 || ^5.4 || ^6.0", "symfony/cache": "^4.4 || ^5.4 || ^6.4 || ^7.0",
"symfony/var-exporter": "^4.4 || ^5.4 || ^6.2", "symfony/var-exporter": "^4.4 || ^5.4 || ^6.2 || ^7.0",
"symfony/yaml": "^3.4 || ^4.0 || ^5.0 || ^6.0", "symfony/yaml": "^3.4 || ^4.0 || ^5.0 || ^6.0 || ^7.0",
"vimeo/psalm": "4.30.0 || 5.15.0" "vimeo/psalm": "4.30.0 || 5.16.0"
}, },
"suggest": { "suggest": {
"ext-dom": "Provides support for XSD validation for XML mapping files", "ext-dom": "Provides support for XSD validation for XML mapping files",
@ -1227,9 +1175,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/doctrine/orm/issues", "issues": "https://github.com/doctrine/orm/issues",
"source": "https://github.com/doctrine/orm/tree/2.17.1" "source": "https://github.com/doctrine/orm/tree/2.17.2"
}, },
"time": "2023-11-17T06:25:40+00:00" "time": "2023-12-20T21:47:52+00:00"
}, },
{ {
"name": "doctrine/persistence", "name": "doctrine/persistence",

View File

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

View File

@ -4,8 +4,6 @@
# Put parameters here that don't need to change on each machine where the app is deployed # 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 # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters: parameters:
snipStorageType: 'db' # 'db' or 'git
gitStoragePath: '%kernel.project_dir%/var/snips'
services: services:
# default configuration for services in *this* file # default configuration for services in *this* file
@ -25,8 +23,3 @@ services:
App\Service\LastRelease: App\Service\LastRelease:
arguments: arguments:
- '%kernel.project_dir%/release.json' - '%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); set('git_tty', true);
// Shared files/dirs between deploys // Shared files/dirs between deploys
set('shared_dirs', ['var/log', 'var/sessions', 'var/snips']); set('shared_dirs', ['var/log', 'var/sessions']);
set('shared_files', ['.env.local']); set('shared_files', ['.env.local']);
//set('writable_dirs', ['var']); //set('writable_dirs', ['var']);
set('migrations_config', ''); set('migrations_config', '');
@ -24,7 +24,7 @@ set('allow_anonymous_stats', false);
host('snips.loken.nl') host('snips.loken.nl')
->setRemoteUser('www-data') ->setRemoteUser('www-data')
->set('branch', function () { ->set('branch', function () {
return input()->getOption('branch') ?: 'main'; return input()->getOption('branch') ?: 'master';
}) })
->set('deploy_path', '~/snips.loken.nl'); ->set('deploy_path', '~/snips.loken.nl');
@ -67,7 +67,7 @@ task('deployment:log', function () { //https://stackoverflow.com/questions/59686
$commitDate = $commit[1]; $commitDate = $commit[1];
// $line = sprintf('%s %s branch="%s" hash="%s"', $date, $commitHashShort, $branch, $commitHash); // $line = sprintf('%s %s branch="%s" hash="%s"', $date, $commitHashShort, $branch, $commitHash);
$projectUrlBase = 'https://git.loken.nl/ardent/AnimeRSS4'; $projectUrlBase = 'https://git.loken.nl/ardent/Snips';
$array = [ $array = [
'branch' => $branch, 'branch' => $branch,
'branchUrl' => sprintf('%s/src/branch/%s', $projectUrlBase, $branch), 'branchUrl' => sprintf('%s/src/branch/%s', $projectUrlBase, $branch),

View File

@ -0,0 +1,35 @@
<?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

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

View File

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

View File

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

View File

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

View File

@ -40,10 +40,17 @@ trait TrackedTrait
return $this; return $this;
} }
public function setCreatedAtTodayNoSeconds(): self public function setCreatedAtNowNoSeconds(): self
{ {
$this->setCreatedAt(DateTime::createFromFormat('Y-m-d H:i', date('Y-m-d H:i'))); $this->setCreatedAt(DateTime::createFromFormat('Y-m-d H:i', date('Y-m-d H:i')));
return $this; 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)] #[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)]
private Collection $snipContents; private Collection $snipContents;
#[ORM\Column(length: 255, nullable: true)] #[ORM\ManyToOne]
private ?string $activeCommit = null; private ?SnipContent $activeVersion = null;
public function __construct() public function __construct()
{ {
@ -99,14 +99,19 @@ class Snip
return $this; return $this;
} }
public function getActiveCommit(): ?string public function getLatestVersion(): ?SnipContent
{ {
return $this->activeCommit; return $this->snipContents->last();
} }
public function setActiveCommit(?string $activeCommit): static public function getActiveVersion(): ?SnipContent
{ {
$this->activeCommit = $activeCommit; return $this->activeVersion;
}
public function setActiveVersion(?SnipContent $activeVersion): static
{
$this->activeVersion = $activeVersion;
return $this; return $this;
} }

View File

@ -1,13 +0,0 @@
<?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

@ -1,33 +0,0 @@
<?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;
}
}

View File

@ -1,26 +0,0 @@
<?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;
}
}

View File

View File

@ -1,74 +0,0 @@
<?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

@ -1,80 +0,0 @@
<?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

@ -1,21 +0,0 @@
<?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

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

View File

@ -4,12 +4,12 @@ namespace App\Service\SnipParser\Stages;
use League\Pipeline\StageInterface; use League\Pipeline\StageInterface;
class ReplaceStage implements StageInterface readonly class ReplaceStage implements StageInterface
{ {
// replaces a string with another string // replaces a string with another string
public function __construct( public function __construct(
public readonly string $search, public string $search,
public readonly string $replace, public string $replace,
) {} ) {}
public function __invoke(mixed $payload): string 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\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class UrlReferenceStage implements StageInterface readonly class UrlReferenceStage implements StageInterface
{ {
public function __construct( public function __construct(
#[Autowire(lazy: true)] private readonly UrlGeneratorInterface $router, #[Autowire(lazy: true)] private UrlGeneratorInterface $router,
#[Autowire(lazy: true)] private readonly Security $security, #[Autowire(lazy: true)] private Security $security,
#[Autowire(lazy: true)] private readonly SnipRepository $snipRepository, #[Autowire(lazy: true)] private SnipRepository $snipRepository,
) {} ) {}
public function __invoke(mixed $payload): string public function __invoke(mixed $payload): string

View File

@ -3,48 +3,17 @@
namespace App\Service; namespace App\Service;
use App\Entity\Snip; use App\Entity\Snip;
use App\Git\CustomGit; use App\Service\SnipContent\SnipContentService;
use App\Service\SnipContent\SnipContentDB;
use App\Service\SnipContent\SnipContentGit;
use App\Service\SnipContent\SnipContentInterface;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;
class SnipServiceFactory readonly class SnipServiceFactory
{ {
public function __construct( public function __construct(
private readonly string $gitStoragePath, private EntityManagerInterface $em,
private readonly string $storageType,
private readonly Security $security,
private readonly EntityManagerInterface $em,
) {} ) {}
public function create(Snip $snip): SnipContentInterface public function create(Snip $snip): SnipContentService
{ {
return match ($this->storageType) { return new SnipContentService($snip, $this->em);
'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> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}SNIPS{% endblock %}</title> <title>{% if title is defined %}{{ title }}{% else %}SNIPS{% endif %}</title>
<link rel="shortcut icon" type="image/jpg" href="/favicon.png"> <link rel="shortcut icon" type="image/jpg" href="/favicon.png">
{% block css %} {% block css %}
@ -37,19 +37,7 @@
</div> </div>
{# body blocks #} {# body blocks #}
{% block bodyraw %} {% block content %}
<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 %} {% endblock %}
{# javascript block #} {# javascript block #}

View File

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

View File

@ -0,0 +1,10 @@
{% 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 %}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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