vendor/pimcore/pimcore/lib/Targeting/EventListener/FullPageCacheCookieCleanupListener.php line 39

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\PrepareResponseEvent;
  17. use Pimcore\Event\FullPageCacheEvents;
  18. use Pimcore\Targeting\Storage\CookieStorage;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\Cookie;
  21. /**
  22.  * Removes cookie storage cookies from cached response (only from the response object, not
  23.  * from the client's browser).
  24.  */
  25. class FullPageCacheCookieCleanupListener implements EventSubscriberInterface
  26. {
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             FullPageCacheEvents::PREPARE_RESPONSE => 'onPrepareFullPageCacheResponse',
  31.         ];
  32.     }
  33.     public function onPrepareFullPageCacheResponse(PrepareResponseEvent $event)
  34.     {
  35.         $response $event->getResponse();
  36.         $cookies $response->headers->getCookies();
  37.         $blacklist = [
  38.             CookieStorage::COOKIE_NAME_VISITOR,
  39.             CookieStorage::COOKIE_NAME_SESSION,
  40.         ];
  41.         foreach ($cookies as $cookie) {
  42.             if (in_array($cookie->getName(), $blacklist)) {
  43.                 $response->headers->removeCookie(
  44.                     $cookie->getName(),
  45.                     $cookie->getPath(),
  46.                     $cookie->getDomain()
  47.                 );
  48.             }
  49.         }
  50.     }
  51. }