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

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