vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/EventListener/SessionBagListener.php line 61

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\EcommerceFrameworkBundle\EventListener;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  17. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  18. use Symfony\Component\HttpKernel\Event\RequestEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. class SessionBagListener implements EventSubscriberInterface
  21. {
  22.     const ATTRIBUTE_BAG_CART 'ecommerceframework_cart';
  23.     const ATTRIBUTE_BAG_ENVIRONMENT 'ecommerceframework_environment';
  24.     const ATTRIBUTE_BAG_PRICING_ENVIRONMENT 'ecommerceframework_pricing_environment';
  25.     const ATTRIBUTE_BAG_PAYMENT_ENVIRONMENT 'ecommerceframework_payment_environment';
  26.     /**
  27.      * @return array
  28.      */
  29.     public static function getSubscribedEvents()// : array
  30.     {
  31.         return [
  32.             //run after Symfony\Component\HttpKernel\EventListener\SessionListener
  33.             KernelEvents::REQUEST => ['onKernelRequest'127],
  34.         ];
  35.     }
  36.     /**
  37.      * @return string[]
  38.      */
  39.     protected function getBagNames()
  40.     {
  41.         return [
  42.             self::ATTRIBUTE_BAG_CART,
  43.             self::ATTRIBUTE_BAG_ENVIRONMENT,
  44.             self::ATTRIBUTE_BAG_PRICING_ENVIRONMENT,
  45.             self::ATTRIBUTE_BAG_PAYMENT_ENVIRONMENT,
  46.         ];
  47.     }
  48.     /**
  49.      * @param RequestEvent $event
  50.      */
  51.     public function onKernelRequest(RequestEvent $event)
  52.     {
  53.         if (!$event->isMainRequest()) {
  54.             return;
  55.         }
  56.         $session $event->getRequest()->getSession();
  57.         //do not register bags, if session is already started
  58.         if ($session->isStarted()) {
  59.             return;
  60.         }
  61.         $this->configure($session);
  62.     }
  63.     /**
  64.      * @param SessionInterface $session
  65.      *
  66.      */
  67.     public function configure(SessionInterface $session)
  68.     {
  69.         $bagNames $this->getBagNames();
  70.         foreach ($bagNames as $bagName) {
  71.             $bag = new AttributeBag('_' $bagName);
  72.             $bag->setName($bagName);
  73.             $session->registerBag($bag);
  74.         }
  75.     }
  76.     /**
  77.      * Clears all session bags filled from the e-commerce framework
  78.      *
  79.      * @param SessionInterface $session
  80.      */
  81.     public function clearSession(SessionInterface $session)
  82.     {
  83.         $bagNames $this->getBagNames();
  84.         foreach ($bagNames as $bagName) {
  85.             $sessionBag $session->getBag($bagName);
  86.             $sessionBag->clear();
  87.         }
  88.     }
  89. }