vendor/pimcore/pimcore/lib/Cache/FullPage/SessionStatus.php line 49

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\Cache\FullPage;
  16. use Pimcore\Event\Cache\FullPage\IgnoredSessionKeysEvent;
  17. use Pimcore\Event\FullPageCacheEvents;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. /**
  21.  * Determines if the full page cache should be disabled due to
  22.  * session (started session containing data).
  23.  */
  24. class SessionStatus
  25. {
  26.     /**
  27.      * @var string
  28.      */
  29.     private $symfonyMetadataStorageKey;
  30.     /**
  31.      * @var EventDispatcherInterface
  32.      */
  33.     private $eventDispatcher;
  34.     public function __construct(
  35.         string $symfonyMetadataStorageKey,
  36.         EventDispatcherInterface $eventDispatcher
  37.     ) {
  38.         $this->symfonyMetadataStorageKey $symfonyMetadataStorageKey;
  39.         $this->eventDispatcher $eventDispatcher;
  40.     }
  41.     public function isDisabledBySession(Request $request): bool
  42.     {
  43.         if (!$request->hasSession() || empty($request->getSession()->getId())) {
  44.             return false;
  45.         }
  46.         // we fall back to $_SESSION from here on as the session API does not expose a list of namespaces
  47.         $sessionData $_SESSION ?? null;
  48.         if (empty($sessionData)) {
  49.             return false;
  50.         }
  51.         if (!is_array($sessionData)) {
  52.             return false;
  53.         }
  54.         // disable full page cache if any session key besides the ignored
  55.         // ones (e.g. symfony metadata, targeting) have data
  56.         $ignoredSessionKeys $this->getIgnoredSessionKeys();
  57.         foreach ($sessionData as $key => $value) {
  58.             if (!in_array($key$ignoredSessionKeys) && !empty($value)) {
  59.                 return true;
  60.             }
  61.         }
  62.         return false;
  63.     }
  64.     private function getIgnoredSessionKeys(): array
  65.     {
  66.         $event = new IgnoredSessionKeysEvent([$this->symfonyMetadataStorageKey]);
  67.         $this->eventDispatcher->dispatch(FullPageCacheEvents::IGNORED_SESSION_KEYS$event);
  68.         return $event->getKeys();
  69.     }
  70. }