Renamed SandboxedTwigProvider to SandboxedTwig Factory

This commit is contained in:
Jan Böhmer 2024-03-06 20:08:35 +01:00
parent a2cd5652d2
commit 1da3225e1d
3 changed files with 8 additions and 7 deletions

View file

@ -53,7 +53,7 @@ use Twig\Error\Error;
final class LabelHTMLGenerator final class LabelHTMLGenerator
{ {
public function __construct(private readonly ElementTypeNameGenerator $elementTypeNameGenerator, private readonly LabelTextReplacer $replacer, private readonly Environment $twig, private readonly LabelBarcodeGenerator $barcodeGenerator, private readonly SandboxedTwigProvider $sandboxedTwigProvider, private readonly Security $security, private readonly string $partdb_title) public function __construct(private readonly ElementTypeNameGenerator $elementTypeNameGenerator, private readonly LabelTextReplacer $replacer, private readonly Environment $twig, private readonly LabelBarcodeGenerator $barcodeGenerator, private readonly SandboxedTwigFactory $sandboxedTwigProvider, private readonly Security $security, private readonly string $partdb_title)
{ {
} }
@ -66,7 +66,7 @@ final class LabelHTMLGenerator
$twig_elements = []; $twig_elements = [];
if (LabelProcessMode::TWIG === $options->getProcessMode()) { if (LabelProcessMode::TWIG === $options->getProcessMode()) {
$sandboxed_twig = $this->sandboxedTwigProvider->getTwig($options); $sandboxed_twig = $this->sandboxedTwigProvider->createTwig($options);
$current_user = $this->security->getUser(); $current_user = $this->security->getUser();
} }

View file

@ -72,9 +72,10 @@ use Twig\Loader\ArrayLoader;
use Twig\Sandbox\SecurityPolicyInterface; use Twig\Sandbox\SecurityPolicyInterface;
/** /**
* This service creates a sandboxed twig environment for the label system.
* @see \App\Tests\Services\LabelSystem\SandboxedTwigProviderTest * @see \App\Tests\Services\LabelSystem\SandboxedTwigProviderTest
*/ */
final class SandboxedTwigProvider final class SandboxedTwigFactory
{ {
private const ALLOWED_TAGS = ['apply', 'autoescape', 'do', 'for', 'if', 'set', 'verbatim', 'with']; private const ALLOWED_TAGS = ['apply', 'autoescape', 'do', 'for', 'if', 'set', 'verbatim', 'with'];
private const ALLOWED_FILTERS = ['abs', 'batch', 'capitalize', 'column', 'country_name', private const ALLOWED_FILTERS = ['abs', 'batch', 'capitalize', 'column', 'country_name',
@ -129,7 +130,7 @@ final class SandboxedTwigProvider
{ {
} }
public function getTwig(LabelOptions $options): Environment public function createTwig(LabelOptions $options): Environment
{ {
if (LabelProcessMode::TWIG !== $options->getProcessMode()) { if (LabelProcessMode::TWIG !== $options->getProcessMode()) {
throw new InvalidArgumentException('The LabelOptions must explicitly allow twig via lines_mode = "twig"!'); throw new InvalidArgumentException('The LabelOptions must explicitly allow twig via lines_mode = "twig"!');

View file

@ -47,18 +47,18 @@ use App\Entity\LabelSystem\LabelSupportedElement;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot; use App\Entity\Parts\PartLot;
use App\Entity\Parts\StorageLocation; use App\Entity\Parts\StorageLocation;
use App\Services\LabelSystem\SandboxedTwigProvider; use App\Services\LabelSystem\SandboxedTwigFactory;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Twig\Sandbox\SecurityError; use Twig\Sandbox\SecurityError;
class SandboxedTwigProviderTest extends WebTestCase class SandboxedTwigFactoryTest extends WebTestCase
{ {
private ?object $service = null; private ?object $service = null;
protected function setUp(): void protected function setUp(): void
{ {
self::bootKernel(); self::bootKernel();
$this->service = self::getContainer()->get(SandboxedTwigProvider::class); $this->service = self::getContainer()->get(SandboxedTwigFactory::class);
} }
public function twigDataProvider(): array public function twigDataProvider(): array