PhpSyntaxExtension example

class SeoUrlTwigFactory
{
    /** * @param ExtensionInterface[] $twigExtensions */
    public function createTwigEnvironment(SlugifyInterface $slugify, iterable $twigExtensions = []): Environment
    {
        $twig = new TwigEnvironment(new ArrayLoader());
        $twig->setCache(false);
        $twig->enableStrictVariables();
        $twig->addExtension(new SlugifyExtension($slugify));
        $twig->addExtension(new PhpSyntaxExtension());
        $twig->addExtension(new SecurityExtension([]));

        /** @var EscaperExtension $coreExtension */
        $coreExtension = $twig->getExtension(EscaperExtension::class);
        $coreExtension->setEscaper(
            SeoUrlGenerator::ESCAPE_SLUGIFY,
            // Do not remove $_twig, although it is marked as unused. It somehow important             static fn ($_twig$string) => rawurlencode($slugify->slugify($string))
        );

        foreach ($twigExtensions as $twigExtension) {
            
'##property_getter##',
                $propertyGetter,
                file_get_contents(__DIR__ . '/fixtures/FieldVisibilityCases/explicit-get.twig')
            ),
            'offset-get.twig' => str_replace(
                '##property_name##',
                $propertyName,
                file_get_contents(__DIR__ . '/fixtures/FieldVisibilityCases/offset-get.twig')
            ),
        ]));

        $twig->addExtension(new PhpSyntaxExtension());

        return $twig;
    }
}
$this->callAfter($services$hook$script);
    }

    private function initEnv(Script $script): Environment
    {
        $twig = new TwigEnvironment(
            new ScriptTwigLoader($script),
            $script->getTwigOptions()
        );

        $twig->addExtension(new PhpSyntaxExtension());
        $twig->addExtension($this->translationExtension);
        $twig->addExtension(new SecurityExtension([]));
        $twig->addExtension(new PcreExtension());
        $twig->addExtension(new ReplaceRecursiveFilter());

        if ($script->getTwigOptions()['debug'] ?? false) {
            $twig->addExtension(new DebugExtension());
        }

        $twig->addGlobal('shopware', new ArrayStruct([
            'version' => $this->shopwareVersion,
        ]));
 implode(', ', array_keys($context))$this->script),
            $lastModified,
            null,
            $options
        );

        $twig = new TwigEnvironment(
            new ScriptTwigLoader($script),
            $script->getTwigOptions()
        );

        $twig->addExtension(new PhpSyntaxExtension());
        $twig->addExtension(new ComparisonExtension());
        $twig->addExtension(new PcreExtension());
        $twig->addExtension(new ReplaceRecursiveFilter());

        if ($this->debug) {
            $twig->addExtension(new DebugExtension());
        }

        $twig->addExtension(new SecurityExtension([]));

        $hook = new AppScriptConditionHook($scope->getContext());

        
Home | Imprint | This part of the site doesn't use cookies.