src/EventListener/ActiveInstanceListener.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\HttpFoundation\Session\Session;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Contracts\Cache\CacheInterface;
  10. /**
  11.  *  * Class CurrentSiteListener
  12.  *  * @package App\EventListener
  13.  *  */
  14. class ActiveInstanceListener
  15. {
  16.     private $em;
  17.     private $cacheConfig;
  18.     private $session;
  19.     /**
  20.     * CurrentSiteListener constructor.
  21.     * @param EntityManagerInterface $em
  22.     * @param CacheInterface $cacheConfig
  23.     */
  24.     public function __construct(
  25.         SessionInterface $session,
  26.         EntityManagerInterface $em,
  27.         CacheInterface  $cacheConfig
  28.     ) {
  29.         $this->em $em;
  30.         $this->cacheConfig $cacheConfig;
  31.     }
  32.     /**
  33.  *      * @param RequestEvent $event
  34.  *      */
  35.     public function onKernelRequest(RequestEvent $event)
  36.     {
  37.         $request $event->getRequest();
  38.         $instance $request->get('instance'null);
  39.         //$session = $request->getSession();
  40.         if (!$this->session) {
  41.             $this->session = new Session();
  42.         }
  43.         $activeInstance $this->session->get('gestit_instance''gestit_0822');
  44.         // Si on change d'instance alors on reinitialise la session
  45.         if ($instance && $activeInstance != $instance) {
  46.             $this->session->invalidate();
  47.             $this->session = new Session();
  48.             $this->session->set('gestit_instance'$instance);
  49.         }
  50.         //
  51.         /*echo "<br> id " . $this->session->getId() . "<br>";
  52.         print_r($this->session->all());
  53.         */
  54.         $this->em->getConnection()->selectDatabase($activeInstance);
  55.         /*$session = new Session();
  56.         $session->start();
  57.         $activeInstance = null;
  58.         if($session) {
  59.             $activeInstance = $session->get('gestint_instance',null);
  60.         }
  61.         else {
  62.             $session = new Session();
  63.         }
  64.         $session->set('gestit_instance',$instance);
  65.         //$request->setSession($session);*/
  66.        //throw new NotFoundHttpException('no config found for this host ' . $instance . ' instance active :: ' . $activeInstance);
  67.     }
  68. }