* The master registry list
* @var array
*/
- private $registry = array();
+ private $registry = null;
/**
* An array of debug messages
* Constructor
*/
private function __construct()
- {}
+ {
+ $this->registry = new BSVariableRegistry();
+ }
// ###################################################################
/**
// ###################################################################
/**
- * Registers a value in the master registry. You cannot overwrite
- * values. You must first unregister() them if you wish to do so.
- *
- * @param string Registry key
- * @param mixed Value to register
- */
- public static function Register($key, $value)
- {
- if (isset(self::_Instance()->registry["$key"]))
- {
- throw new Exception('Cannot overwrite a key in the registry');
- return;
- }
-
- self::_Instance()->registry["$key"] = $value;
- }
-
- // ###################################################################
- /**
- * Unregisters a value from the registry. This removes all traces of
- * it from this object.
- *
- * @param string Registry key
- */
- public static function Unregister($key)
- {
- if (!isset(self::_Instance()->registry["$key"]))
- {
- throw new Exception('You cannot unregister a key that does not exist');
- return;
- }
-
- unset(self::_Instance()->registry["$key"]);
- }
-
- // ###################################################################
- /**
- * This gets a value from the registry with a specific key
- *
- * @param string The key
- *
- * @return mixed Value in the registry for key
- */
- public static function Get($key)
- {
- if (!isset(self::_Instance()->registry["$key"]))
- {
- throw new Exception('Cannot access the registry with a non-existent key');
- return;
- }
-
- return self::_Instance()->registry["$key"];
- }
-
- // ###################################################################
- /**
- * Returns the first object of a specified class type
- *
- * @param string Class name
- *
- * @return object Object in the registry of that type
- */
- public static function GetType($class)
- {
- $class = 'BS' . $class;
- foreach (self::_Instance()->registry AS $key => $object)
- {
- if ($object instanceof $class)
- {
- return $object;
- }
- }
- }
-
- // ###################################################################
- /**
- * Returns the entire registry stack
- *
- * @return array Complete registry
- */
- public static function GetAll()
+ * Returns the registry object without it ever being able to be cleared
+ * or unset
+ *
+ * @return StdClass
+ */
+ public static function Registry()
{
return self::_Instance()->registry;
}
}
}
}
+
+
+}
+
+/**
+ * Variable Registry
+ *
+ * This class can be used to store unlimited number of properties. It is a (make believe)
+ * inner class to BSRegister and cannot be instantiated outside of it.
+ *
+ * @author Blue Static
+ * @copyright Copyright (c)2002 - [#]year[#], Blue Static
+ * @version $Id$
+ * @package ISSO
+ *
+ */
+class BSVariableRegistry
+{
+ // ###################################################################
+ /**
+ * Constructor: only able to be called by BSRegister
+ */
+ public function __construct()
+ {
+ $bt = debug_backtrace();
+ if (!isset($bt[1]['class']) OR $bt[1]['class'] != 'BSRegister')
+ {
+ exit('You cannot instantiate ' . __CLASS__ . ' directly. It is an internal class.');
+ }
+ }
+
+ // ###################################################################
+ /**
+ * If you try and get a non-existent property, throw a new exception
+ */
+ public function __get($name)
+ {
+ throw new Exception('Failed to get nonexistent property "' . $name . '"');
+ }
+
+ // ###################################################################
+ /**
+ * Returns the first value of the given type in the variable registry
+ *
+ * @return mixed
+ */
+ public function getType($class)
+ {
+ $class = 'BS' . $class;
+ foreach ($this AS $object)
+ {
+ if ($object instanceof $class)
+ {
+ return $object;
+ }
+ }
+ }
+
+ // ###################################################################
+ /**
+ * Returns all the objects in the registry by iterating over it
+ *
+ * @return array
+ */
+ public function allObjects()
+ {
+ $registry = array();
+ foreach ($this AS $key => $value)
+ {
+ $registry[$key] = $value;
+ }
+ return $registry;
+ }
}
/*=====================================================================*\
public function testRegisterValue()
{
- BSRegister::Register('someKey', 'someValue');
+ BSRegister::Registry()->someKey = 'someValue';
- $registry = BSRegister::GetAll();
- $this->assertEquals($registry['someKey'], 'someValue');
+ $registry = BSRegister::Registry();
+ $this->assertEquals($registry->someKey, 'someValue');
}
public function testGetAll()
{
- BSRegister::Register('test', 1);
- $this->assertType('array', BSRegister::GetAll());
- $this->assertEquals(sizeof(BSRegister::GetAll()), 2);
+ BSRegister::Registry()->test = 1;
+ $this->assertType('array', BSRegister::Registry()->allObjects());
+ $this->assertSame(sizeof(BSRegister::Registry()->allObjects()), 2);
}
public function testOverWriteRegister()
{
- try
- {
- BSRegister::Register('valueToOverWrite', 1);
- BSRegister::Register('valueToOverWrite', 2);
- $this->fail('exception expected');
- }
- catch (Exception $e)
- {
- $this->assertEquals(1, BSRegister::Get('valueToOverWrite'));
- }
+ BSRegister::Registry()->valueToOverWrite = 1;
+ BSRegister::Registry()->valueToOverWrite = 2;
+ $this->assertEquals(2, BSRegister::Registry()->valueToOverWrite);
}
public function testGet()
{
- BSRegister::Register('testGet', 123);
- $this->assertEquals(123, BSRegister::Get('testGet'));
+ BSRegister::Registry()->testGet = 123;
+ $this->assertEquals(123, BSRegister::Registry()->testGet);
}
public function testUnregister()
{
- try
- {
- BSRegister::Register('testUnregister', 1);
- BSRegister::Unregister('testUnregister');
- BSRegister::Register('testUnregister', 2);
- $this->fail('exception expected');
- }
- catch (Exception $e)
- {
- $this->assertEquals(2, BSRegister::Get('testUnregister'));
- }
+ BSRegister::Registry()->testUnregister = 1;
+ unset(BSRegister::Registry()->testUnregister);
+ $this->assertObjectNotHasAttribute('testUnregister', BSRegister::Registry());
}
public function testGetNoExist()
{
try
{
- BSRegister::Get('doesNotExist');
+ BSRegister::Registry()->doesNotExist;
$this->fail('exception expected');
}
catch (Exception $e)
{
try
{
- BSRegister::Unregister('keyThatWontExist');
+ unset(BSRegister::Registry()->keyThatWontExist);
$this->fail('exception expected');
}
catch (Exception $e)
public function testGetType()
{
$input = BSRegister::LoadModule('Input');
- BSRegister::Register('input', $input);
- $this->assertSame($input, BSRegister::GetType('Input'));
+ BSRegister::Registry()->input = $input;
+ $this->assertSame($input, BSRegister::Registry()->getType('Input'));
- $this->assertEquals(null, BSRegister::GetType('Date'));
+ $this->assertEquals(null, BSRegister::Registry()->getType('Date'));
}
public function testRequiredModules()