<?php
/**
 * MiniMVC
 *
 * Convention-based micro-framework for PHP
 *
 * @package		miniMVC
 * @author 		Timothy J. Warren
 * @copyright	Copyright (c) 2011 - 2012
 * @link 		https://github.com/timw4mail/miniMVC
 * @license 	http://philsturgeon.co.uk/code/dbad-license 
 */
 
// --------------------------------------------------------------------------
/**
 * Define a session id to namespace sessions
 */
define(SESS_ID, 'MM_SESSION');
// --------------------------------------------------------------------------

/**
 * Class to improve handling of PHP sessions
 *
 * @package miniMVC
 * @subpackage Libraries
 */
class MM_Session{

	// Combine traits for more dynamic behavior
	// but use Singleton for __invoke, so calling
	// that method does not create a new instance
	use JSObject, Singleton
	{
		Singleton::__invoke insteadof JSObject;
	}
	
	/**
	 * Reference to session superglobal
	 *
	 * @var array
	 */
	protected $sess = [];
	
	/**
	 * Start a session
	 */
	protected function __construct()
	{
		session_start();
		
		// Save a reference to the session for later access
		$_SESSION[SESS_ID] = (isset($_SESSION[SESS_ID]) ?: [];
		$this->sess =& $_SESSION[SESS_ID];
	}
	
	// --------------------------------------------------------------------------

	/**
	 * Set a session value
	 *
	 * @param string $key
	 * @param mixed $val
	 * @return void
	 */ 
	public function __set($key, $val)
	{
		$this->sess[$key] = $val;
	}
	
	// --------------------------------------------------------------------------

	/**
	 * Retreive a session value
	 *
	 * @param string $key
	 * @return mixed
	 */
	public function __get($key)
	{
		return $this->sess[$key];
	}
	
	// --------------------------------------------------------------------------

	/**
	 * Destroy a session
	 *
	 * @return void
	 */
	public function destroy()
	{
		sess_destroy();
	}
}
// End of php_session.php