<?php
namespace App\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Contracts\Cache\CacheInterface;
/**
* * Class CurrentSiteListener
* * @package App\EventListener
* */
class ActiveInstanceListener
{
private $em;
private $cacheConfig;
private $session;
/**
* CurrentSiteListener constructor.
* @param EntityManagerInterface $em
* @param CacheInterface $cacheConfig
*/
public function __construct(
SessionInterface $session,
EntityManagerInterface $em,
CacheInterface $cacheConfig
) {
$this->em = $em;
$this->cacheConfig = $cacheConfig;
}
/**
* * @param RequestEvent $event
* */
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$instance = $request->get('instance', null);
//$session = $request->getSession();
if (!$this->session) {
$this->session = new Session();
}
$activeInstance = $this->session->get('gestit_instance', 'gestit_0822');
// Si on change d'instance alors on reinitialise la session
if ($instance && $activeInstance != $instance) {
$this->session->invalidate();
$this->session = new Session();
$this->session->set('gestit_instance', $instance);
}
//
/*echo "<br> id " . $this->session->getId() . "<br>";
print_r($this->session->all());
*/
$this->em->getConnection()->selectDatabase($activeInstance);
/*$session = new Session();
$session->start();
$activeInstance = null;
if($session) {
$activeInstance = $session->get('gestint_instance',null);
}
else {
$session = new Session();
}
$session->set('gestit_instance',$instance);
//$request->setSession($session);*/
//throw new NotFoundHttpException('no config found for this host ' . $instance . ' instance active :: ' . $activeInstance);
}
}