<?php
namespace Main\Misc;

use \Main\BasicObject;
require_once 'BasicClasses.php';
require_once 'Request.php';
require_once 'Response.php';

// Create a Registry class to store things required across the
// entire application and all the tiers (layers) of the application.
// Effectively, this is equivalent to putting all our global
// variables in one class, although we should try to prevent
// this becoming a dumpling ground for globals.
class Registry extends BasicObject
{
	private static $_instance;		// Singleton pattern, only one object instance of this class required
	
	private $_request;				// Stores Request object (enforced by type hint)
	private $_response;				// Stores Response object (enforect by type hint)
	
	private $_factories = array();	// Store our factories (enforced by type hint)
	
	private function __construct()	// Private to prevent object being instantiated outside class
	{								// i.e. can't do "new" outside this class
		// Do nothing
	}
	
	static public function Instance()		// Static so we use classname itself to create object i.e. Registry::Instance()
	{
		// Check if the object has been created
		// Only one object of this class is required
		// so we only create if it hasn't already 
		// been created.
		if(!isset(self::$_instance))
		{
			self::$_instance = new self();	// Make new instance of the Registry class
			
			// Store the Request and Response objects in the Registry
			// because we only every want one of each
			self::$_instance->_request = new Request();
			self::$_instance->_response = new Response();			
		}
		return self::$_instance;
	}
	
	// Get method
	// Return the Request object
	public function GetRequest()
	{
		return $this->_request;
	}

	// Set method
	// Save the Request object
	public function SetRequest(Request $value)	// Provide type hint so only Request object can be used to set it
	{
		$this->_request = $value;
	}
	
	// Get method
	// Return the Response object
	public function GetResponse()
	{
		return $this->_response;
	}
	
	// Set method
	// Save the Response object
	public function SetResponse(Response $value)	// Provide type hint so only Response object can be used to set it
	{
		$this->_response = $value;
	}
	
	// Read a key/value pair
	public function IndexGetFactory($key)
	{
		if(isset($this->_factories[$key]))
		{
			return $this->_factories[$key];
		}
		return;	// Return undefined
	}
	
	// Write to a key/value pair
	public function IndexSetFactory($key, $value)
	{
		$this->_factories[$key] = $value;
	}
}
?>