platform/src/Storefront/Framework/Twig/TwigDateRequestListener.php line 29

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Twig;
  3. use Composer\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Twig\Environment;
  6. use Twig\Extension\CoreExtension;
  7. class TwigDateRequestListener implements EventSubscriberInterface
  8. {
  9.     public const TIMEZONE_COOKIE 'timezone';
  10.     /**
  11.      * @var Environment
  12.      */
  13.     private $twig;
  14.     public function __construct(Environment $twig)
  15.     {
  16.         $this->twig $twig;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return ['kernel.request' => 'onKernelRequest'];
  21.     }
  22.     public function onKernelRequest(RequestEvent $event): void
  23.     {
  24.         $timezone = (string) $event->getRequest()->cookies->get(self::TIMEZONE_COOKIE);
  25.         if (!$timezone || !\in_array($timezonetimezone_identifiers_list(), true)) {
  26.             $timezone 'UTC';
  27.         }
  28.         if (!$this->twig->hasExtension(CoreExtension::class)) {
  29.             return;
  30.         }
  31.         /** @var CoreExtension $coreExtension */
  32.         $coreExtension $this->twig->getExtension(CoreExtension::class);
  33.         $coreExtension->setTimezone($timezone);
  34.     }
  35. }