src/CoreBundle/Service/Session/Session.php line 140

Open in your IDE?
  1. <?php
  2. namespace Core\Service\Session;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. class Session
  7. {
  8.     /**
  9.      * @var EntityManagerInterface
  10.      */
  11.     private $entity_manager;
  12.     /**
  13.      * @var ManagerRegistry
  14.      */
  15.     private $manager;
  16.     /**
  17.      * @var string - Clé du cookie de l'application
  18.      */
  19.     private $cookie_key 'application_session';
  20.     /**
  21.      * @var SessionInterface - Session courante
  22.      */
  23.     private $session;
  24.     private $session_key;
  25.     /**
  26.      * @var DatabaseSession - Session en base de donnée
  27.      */
  28.     private $database;
  29.     /**
  30.      * Session constructor.
  31.      */
  32.     public function __construct(SessionInterface $original_sessionEntityManagerInterface $emManagerRegistry $manager)
  33.     {
  34.         $this->entity_manager $em;
  35.         $this->manager $manager;
  36.         $this->session null;
  37.         // Créer le manager de session en base de donnée
  38.         $this->database = new DatabaseSession($this$this->entity_manager$this->manager);
  39.         // Nettoyage des sessions périmées
  40.         $this->garbageCollector();
  41.         // Récupère la session enregistrée si elle existe
  42.         if (isset($_COOKIE[$this->cookie_key])) {
  43.             // Récupère la session depuis la base de donnée
  44.             $entity $this->database->get($_COOKIE[$this->cookie_key]);
  45.             // Reformat de la session
  46.             if ($entity != null) {
  47.                 $this->session $this->unserialize($entity);
  48.             }
  49.         }
  50.         // Création d'une nouvelle session
  51.         if (!isset($_COOKIE[$this->cookie_key]) || $this->session == null) {
  52.             $original_session->clear();
  53.             // Création d'une session normal
  54.             $this->session = new \Symfony\Component\HttpFoundation\Session\Session();
  55.         }
  56.         // Démarrage de la session si elle n'existe pas
  57.         if (!$this->session->isStarted() && !isset($_SESSION)) {
  58.             $this->session->start();
  59.         }
  60.         // Sauvegarde de l'identifiant en cookie
  61.         if (!isset($_COOKIE[$this->cookie_key])) {
  62.             setcookie($this->cookie_key$this->getSessionKey(), time() + $this->getExpiration(), '/'''falsetrue);
  63.         }
  64.         $this->session_key $this->getSessionKey();
  65.         // Sauvegarde en base de donnée
  66.         $this->database->persist();
  67.     }
  68.     /**
  69.      * Récupère une donnée de la session
  70.      */
  71.     public function get($key)
  72.     {
  73.         return $this->session->get($key);
  74.     }
  75.     /**
  76.      * Vérifie l'existance d'une donnée de la session
  77.      */
  78.     public function has($key)
  79.     {
  80.         return $this->session->has($key);
  81.     }
  82.     /**
  83.      * Ajoute une donnée en session et l'enregistre
  84.      */
  85.     public function set($key$value)
  86.     {
  87.         if (method_exists($value'serialize')) {
  88.             $value $value->serialize();
  89.         }
  90.         $this->session->set($key$value);
  91.         // Enregistre en base de donnée
  92.         $this->database->persist();
  93.     }
  94.     /**
  95.      * Supprime une donnée en session et l'enresitre
  96.      */
  97.     public function clear($key)
  98.     {
  99.         $this->session->remove($key);
  100.         // Supprime en base de donnée
  101.         $this->database->persist();
  102.     }
  103.     public function garbageCollector()
  104.     {
  105.         $sessions $this->entity_manager->getRepository(\Core\Entity\Session::class)->findExpiredSession();
  106.         foreach ($sessions as $session) {
  107.             $this->entity_manager->remove($session);
  108.         }
  109.         $this->entity_manager->flush();
  110.     }
  111.     public function serialize()
  112.     {
  113.         $content = [];
  114.         foreach ($_SESSION['_sf2_attributes'] as $key => $val) {
  115.             if ($key[0] != '_') {
  116.                 if (method_exists($val'serialize')) {
  117.                     $val $val->serialize();
  118.                 }
  119.                 $content[$key] = $val;
  120.             }
  121.         }
  122.         return json_encode($content);
  123.     }
  124.     public function unserialize($entity)
  125.     {
  126.         $content json_decode($entity->getSessionContent());
  127.         $session = new \Symfony\Component\HttpFoundation\Session\Session();
  128.         foreach ($content as $key => $val) {
  129.             $session->set($key$val);
  130.         }
  131.         return $session;
  132.     }
  133.     public function getExpiration()
  134.     {
  135.         return 24 60 60;
  136.     }
  137.     public function getSessionKey()
  138.     {
  139.         if (isset($_COOKIE[$this->cookie_key])) {
  140.             return $_COOKIE[$this->cookie_key];
  141.         }
  142.         return $this->session->getId();
  143.     }
  144.     public function getUserIp()
  145.     {
  146.         if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  147.             return $_SERVER['HTTP_CLIENT_IP'];
  148.         } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  149.             return $_SERVER['HTTP_X_FORWARDED_FOR'];
  150.         } else {
  151.             return $_SERVER['REMOTE_ADDR'];
  152.         }
  153.     }
  154.     /**
  155.      * Supprime une session
  156.      */
  157.     public function destroy()
  158.     {
  159.         // Supprime en base de donnée
  160.         $this->database->destroy();
  161.         $this->session->clear();
  162.         // Suppression du cookie
  163.         setcookie($this->cookie_key''1);
  164.     }
  165.     /**
  166.      * Retourne l'adresse IP de la session courante
  167.      */
  168.     public function getIPAdress()
  169.     {
  170.         if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  171.             return $_SERVER['HTTP_CLIENT_IP'];
  172.         } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  173.             return $_SERVER['HTTP_X_FORWARDED_FOR'];
  174.         } else {
  175.             return $_SERVER['REMOTE_ADDR'];
  176.         }
  177.     }
  178. }