2012-05-03 13:26:09 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MiniMVC
|
|
|
|
*
|
|
|
|
* Convention-based micro-framework for PHP
|
|
|
|
*
|
|
|
|
* @package miniMVC
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2011 - 2012
|
2012-05-23 12:01:13 -04:00
|
|
|
* @link https://github.com/aviat4ion/miniMVC
|
2012-07-03 13:33:36 -04:00
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
2012-05-03 13:26:09 -04:00
|
|
|
*/
|
2012-05-17 16:22:39 -04:00
|
|
|
|
2012-05-14 14:35:57 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-05-03 13:26:09 -04:00
|
|
|
|
2012-05-22 11:11:36 -04:00
|
|
|
namespace miniMVC;
|
|
|
|
|
2012-05-03 13:26:09 -04:00
|
|
|
/**
|
|
|
|
* Class to improve handling of PHP sessions
|
|
|
|
*
|
|
|
|
* @package miniMVC
|
|
|
|
* @subpackage Libraries
|
|
|
|
*/
|
2012-05-21 14:29:36 -04:00
|
|
|
class Session {
|
2012-05-03 13:26:09 -04:00
|
|
|
|
2012-05-03 16:07:40 -04:00
|
|
|
/**
|
|
|
|
* Reference to session superglobal
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-07-03 13:33:36 -04:00
|
|
|
protected $sess = array();
|
2012-05-03 16:07:40 -04:00
|
|
|
|
2012-07-03 13:33:36 -04:00
|
|
|
/**
|
|
|
|
* Reference to current instance
|
|
|
|
*
|
|
|
|
* @var Session
|
|
|
|
*/
|
|
|
|
protected static $instance;
|
|
|
|
|
2012-05-03 13:26:09 -04:00
|
|
|
/**
|
|
|
|
* Start a session
|
|
|
|
*/
|
|
|
|
protected function __construct()
|
|
|
|
{
|
|
|
|
session_start();
|
2012-07-03 13:33:36 -04:00
|
|
|
|
2012-05-03 16:07:40 -04:00
|
|
|
// Save a reference to the session for later access
|
2012-07-03 13:33:36 -04:00
|
|
|
$_SESSION['MM_SESSION'] = (isset($_SESSION['MM_SESSION'])) ?: array();
|
2012-05-17 16:22:39 -04:00
|
|
|
$this->sess =& $_SESSION['MM_SESSION'];
|
2012-05-03 13:26:09 -04:00
|
|
|
}
|
2012-07-03 13:33:36 -04:00
|
|
|
|
2012-05-03 13:26:09 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a session value
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $val
|
|
|
|
* @return void
|
2012-07-03 13:33:36 -04:00
|
|
|
*/
|
2012-05-03 13:26:09 -04:00
|
|
|
public function __set($key, $val)
|
|
|
|
{
|
2012-05-03 16:07:40 -04:00
|
|
|
$this->sess[$key] = $val;
|
2012-05-03 13:26:09 -04:00
|
|
|
}
|
2012-07-03 13:33:36 -04:00
|
|
|
|
2012-05-03 13:26:09 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive a session value
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($key)
|
|
|
|
{
|
2012-05-03 16:07:40 -04:00
|
|
|
return $this->sess[$key];
|
2012-05-03 13:26:09 -04:00
|
|
|
}
|
2012-07-03 13:33:36 -04:00
|
|
|
|
2012-05-03 13:26:09 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a session
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function destroy()
|
|
|
|
{
|
|
|
|
sess_destroy();
|
|
|
|
}
|
2012-07-03 13:33:36 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton getter function
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public static function &get_instance()
|
|
|
|
{
|
|
|
|
if ( ! isset(self::$instance))
|
|
|
|
{
|
|
|
|
$class = __CLASS__;
|
|
|
|
|
|
|
|
self::$instance = new $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic function called when cloning an object
|
|
|
|
*/
|
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
trigger_error('Clone is not allowed.', E_USER_ERROR);
|
|
|
|
}
|
2012-05-03 13:26:09 -04:00
|
|
|
}
|
2012-05-17 16:22:39 -04:00
|
|
|
|
|
|
|
// End of session.php
|