src/Storefront/Framework/Twig/TwigDateRequestListener.php line 32

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Twig;
  3. use Composer\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Framework\Log\Package;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Twig\Extension\CoreExtension;
  9. #[Package('storefront')]
  10. class TwigDateRequestListener implements EventSubscriberInterface
  11. {
  12.     final public const TIMEZONE_COOKIE 'timezone';
  13.     /**
  14.      * @internal
  15.      */
  16.     public function __construct(private readonly ContainerInterface $container)
  17.     {
  18.     }
  19.     /**
  20.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  21.      */
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [KernelEvents::REQUEST => 'onKernelRequest'];
  25.     }
  26.     public function onKernelRequest(RequestEvent $event): void
  27.     {
  28.         $timezone = (string) $event->getRequest()->cookies->get(self::TIMEZONE_COOKIE);
  29.         if (!$timezone || !\in_array($timezonetimezone_identifiers_list(), true) || $timezone === 'UTC') {
  30.             // Default will be UTC @see https://symfony.com/doc/current/reference/configuration/twig.html#timezone
  31.             return;
  32.         }
  33.         $twig $this->container->get('twig');
  34.         if (!$twig->hasExtension(CoreExtension::class)) {
  35.             return;
  36.         }
  37.         /** @var CoreExtension $coreExtension */
  38.         $coreExtension $twig->getExtension(CoreExtension::class);
  39.         $coreExtension->setTimezone($timezone);
  40.     }
  41. }