ManiaLib 1.0b1 API Reference

Source for file SessionEngine.class.php

Documentation is available at SessionEngine.class.php

  1. <?php
  2. /**
  3.  * @author Maxime Raoust
  4.  * @copyright 2009-2010 NADEO
  5.  * @package ManiaLib
  6.  */
  7.  
  8. /**
  9.  * Session engine
  10.  * Helps handling PHP sessions
  11.  * @package ManiaLib
  12.  */
  13. final class SessionEngine
  14. {
  15.     /**
  16.      * Session identifier name. Used as parameter name for transporting the
  17.      * session Id in the URL when the client doesn't support cookies.
  18.      * @ignore
  19.      * 
  20.      */
  21.     const SIDName 'maniasid';
  22.     /**
  23.      * @ignore
  24.      */
  25.     protected static $instance;
  26.  
  27.     /**
  28.      * Gets the instance
  29.      * @return SessionEngine 
  30.      */
  31.     public static function getInstance()
  32.     {
  33.         if (!self::$instance)
  34.         {
  35.             $class = __CLASS__;
  36.             self::$instance new $class;
  37.         }
  38.         return self::$instance;
  39.     }
  40.     
  41.     /**
  42.      * @ignore
  43.      */
  44.     protected function __construct()
  45.     {
  46.         session_name(self::SIDName);
  47.         session_start();
  48.     }
  49.  
  50.     /**
  51.      * Sets a session var
  52.      * @param string 
  53.      * @param mixed 
  54.      */
  55.     function set($name$value null)
  56.     {
  57.         $_SESSION[$name$value;
  58.     }
  59.  
  60.     /**
  61.      * Deletes a session var
  62.      * @param string 
  63.      */
  64.     function delete($name)
  65.     {
  66.         unset ($_SESSION[$name]);
  67.     }
  68.  
  69.     /**
  70.      * Gets a session var, or the default value if nothing was found
  71.      * @param string The name of the variable
  72.      * @param mixed The default value
  73.      * @return mixed 
  74.      */
  75.     function get($name$default null)
  76.     {
  77.         return array_key_exists($name$_SESSION$_SESSION[$name$default;
  78.     }
  79.     
  80.     /**
  81.      * Gets a session var, throws an exception if not found
  82.      * @param string The name of the variable
  83.      * @return mixed 
  84.      */
  85.     function getStrict($name)
  86.     {
  87.         if(array_key_exists($name$_SESSION))
  88.         {
  89.             return $_SESSION[$name];
  90.         }
  91.         throw new SessionException('Session variable "'.$name.'" not found');
  92.     }
  93.  
  94.     /**
  95.      * Checks if the specified session var exists
  96.      * @return boolean 
  97.      */
  98.     function exists($name)
  99.     {
  100.         return array_key_exists($name$_SESSION);
  101.     }
  102. }
  103.  
  104. /**
  105.  * @package ManiaLib
  106.  * @ignore
  107.  */
  108. class SessionException extends Exception {}
  109.  
  110. ?>