vendor/shopware/storefront/Framework/Twig/Extension/CsrfFunctionExtension.php line 26

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Twig\Extension;
  3. use Shopware\Core\Framework\Feature;
  4. use Shopware\Storefront\Framework\Csrf\CsrfPlaceholderHandler;
  5. use Twig\Extension\AbstractExtension;
  6. use Twig\TwigFunction;
  7. /**
  8.  * @deprecated tag:v6.5.0 - CsrfFunctionExtension will be removed without replacement.
  9.  */
  10. class CsrfFunctionExtension extends AbstractExtension
  11. {
  12.     public function getFunctions(): array
  13.     {
  14.         // This need to be because twig extensions cannot be hard deprecated that easily
  15.         if (Feature::isActive('v6.5.0.0')) {
  16.             return [
  17.                 new TwigFunction('sw_csrf', [$this'createCsrfPlaceholder'], ['is_safe' => ['html']]),
  18.             ];
  19.         }
  20.         Feature::triggerDeprecationOrThrow(
  21.             'v6.5.0.0',
  22.             Feature::deprecatedClassMessage(__CLASS__'v6.5.0.0')
  23.         );
  24.         return [
  25.             new TwigFunction('sw_csrf', [$this'createCsrfPlaceholder'], ['is_safe' => ['html']]),
  26.         ];
  27.     }
  28.     /**
  29.      * @param array<string, string> $parameters
  30.      */
  31.     public function createCsrfPlaceholder(string $intent, array $parameters = []): string
  32.     {
  33.         // This need to be because twig extensions cannot be hard deprecated that easily
  34.         if (Feature::isActive('v6.5.0.0')) {
  35.             return '';
  36.         }
  37.         Feature::triggerDeprecationOrThrow(
  38.             'v6.5.0.0',
  39.             Feature::deprecatedClassMessage(__CLASS__'v6.5.0.0')
  40.         );
  41.         $mode $parameters['mode'] ?? 'input';
  42.         if ($mode === 'input') {
  43.             return $this->createInput($intent);
  44.         }
  45.         return CsrfPlaceholderHandler::CSRF_PLACEHOLDER $intent '#';
  46.     }
  47.     private function createInput(string $intent): string
  48.     {
  49.         return sprintf(
  50.             '<input type="hidden" name="_csrf_token" value="%s">',
  51.             CsrfPlaceholderHandler::CSRF_PLACEHOLDER $intent '#'
  52.         );
  53.     }
  54. }