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

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