Implement very basic twig parser

This commit is contained in:
Tim 2025-04-23 01:24:08 +02:00
parent 943177bc08
commit 701bf6c1ed
4 changed files with 29 additions and 3 deletions

View File

@ -24,7 +24,7 @@
"symfony/validator": "7.2.*", "symfony/validator": "7.2.*",
"symfony/yaml": "7.2.*", "symfony/yaml": "7.2.*",
"twig/extra-bundle": "^2.12|^3.0", "twig/extra-bundle": "^2.12|^3.0",
"twig/twig": "^2.12|^3.0" "twig/twig": "^3.0"
}, },
"require-dev": { "require-dev": {
"deployer/deployer": "^7.3", "deployer/deployer": "^7.3",

View File

@ -18,7 +18,6 @@ class SnipType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void public function buildForm(FormBuilderInterface $builder, array $options): void
{ {
dump(iterator_to_array($this->parserFactory->getChoices()));
$builder $builder
->add('name') ->add('name')
->add('parser', ChoiceType::class, [ ->add('parser', ChoiceType::class, [

View File

@ -45,8 +45,11 @@ readonly class SnipContentService
return $this->rebuildText($contentRepo->find($snip->getActiveVersion())); return $this->rebuildText($contentRepo->find($snip->getActiveVersion()));
} }
public function rebuildText(SnipContent $snipContent): string public function rebuildText(?SnipContent $snipContent): string
{ {
if ($snipContent === null) {
return '';
}
if ($snipContent->getText()) { if ($snipContent->getText()) {
return $snipContent->getText(); return $snipContent->getText();
} }

View File

@ -0,0 +1,24 @@
<?php
namespace App\Service\SnipParser\Twig;
use App\Service\SnipParser\AbstractParser;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
class TwigParser extends AbstractParser
{
public static function getName(): string
{
return 'twig';
}
public function parseView(string $content): string
{
$arrayLoader = new ArrayLoader([
'index' => $content,
]);
$twig = new Environment($arrayLoader);
return $twig->render('index');
}
}