vendor/pimcore/pimcore/lib/Targeting/EventListener/TargetingSessionBagListener.php line 99

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Targeting\EventListener;
  16. use Pimcore\Event\Cache\FullPage\IgnoredSessionKeysEvent;
  17. use Pimcore\Event\Cache\FullPage\PrepareResponseEvent;
  18. use Pimcore\Event\FullPageCacheEvents;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  21. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  22. use Symfony\Component\HttpKernel\Event\RequestEvent;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. class TargetingSessionBagListener implements EventSubscriberInterface
  25. {
  26.     const TARGETING_BAG_SESSION 'pimcore_targeting_session';
  27.     const TARGETING_BAG_VISITOR 'pimcore_targeting_visitor';
  28.     /**
  29.      * {@inheritdoc}
  30.      *
  31.      * @return array
  32.      */
  33.     public static function getSubscribedEvents()//: array
  34.     {
  35.         return [
  36.             FullPageCacheEvents::IGNORED_SESSION_KEYS => 'configureIgnoredSessionKeys',
  37.             FullPageCacheEvents::PREPARE_RESPONSE => 'prepareFullPageCacheResponse',
  38.             //run after Symfony\Component\HttpKernel\EventListener\SessionListener
  39.             KernelEvents::REQUEST => ['onKernelRequest'127],
  40.         ];
  41.     }
  42.     /**
  43.      * @param RequestEvent $event
  44.      */
  45.     public function onKernelRequest(RequestEvent $event)
  46.     {
  47.         if (!$event->isMainRequest()) {
  48.             return;
  49.         }
  50.         $session $event->getRequest()->getSession();
  51.         //do not register bags, if session is already started
  52.         if ($session->isStarted()) {
  53.             return;
  54.         }
  55.         $this->configure($session);
  56.     }
  57.     /**
  58.      * @param SessionInterface $session
  59.      *
  60.      */
  61.     public function configure(SessionInterface $session)
  62.     {
  63.         $sessionBag = new AttributeBag('_' self::TARGETING_BAG_SESSION);
  64.         $sessionBag->setName(self::TARGETING_BAG_SESSION);
  65.         $visitorBag = new AttributeBag('_' self::TARGETING_BAG_VISITOR);
  66.         $visitorBag->setName(self::TARGETING_BAG_VISITOR);
  67.         $session->registerBag($sessionBag);
  68.         $session->registerBag($visitorBag);
  69.     }
  70.     public function configureIgnoredSessionKeys(IgnoredSessionKeysEvent $event)
  71.     {
  72.         // configures full page cache to ignore session data in targeting storage
  73.         $event->setKeys(array_merge($event->getKeys(), [
  74.             '_' self::TARGETING_BAG_SESSION,
  75.             '_' self::TARGETING_BAG_VISITOR,
  76.         ]));
  77.     }
  78.     /**
  79.      * Removes session cookie from cached response
  80.      *
  81.      * @param PrepareResponseEvent $event
  82.      */
  83.     public function prepareFullPageCacheResponse(PrepareResponseEvent $event)
  84.     {
  85.         $request $event->getRequest();
  86.         $response $event->getResponse();
  87.         if (!$request->hasSession()) {
  88.             return;
  89.         }
  90.         $sessionName $request->getSession()->getName();
  91.         if (empty($sessionName)) {
  92.             return;
  93.         }
  94.         $cookies $response->headers->getCookies();
  95.         foreach ($cookies as $cookie) {
  96.             if ($cookie->getName() === $sessionName) {
  97.                 $response->headers->removeCookie(
  98.                     $cookie->getName(),
  99.                     $cookie->getPath(),
  100.                     $cookie->getDomain()
  101.                 );
  102.             }
  103.         }
  104.     }
  105. }