From c76277d5d392fe62701f8c6c3a563e940ae28d90 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 14 Aug 2007 03:38:41 +0000 Subject: [PATCH] Changing the registry part of BSRegister to be a new inner class called BSVariableRegistry * Register.php: (BSRegister::__construct): Instantiate a new instance of BSVariableRegistry (BSRegister::Registry): New method (BSRegister::Register): Removed (BSRegister::Unregister): Removed (BSRegister::Get): Removed (BSRegister::GetType): Removed (BSRegister::GetAll): Removed (BSVariableRegistry): New class * docs/UnitTest/RegisterTest.php: Updated the unit test for the new BSRegister functionality --- Register.php | 166 ++++++++++++++++----------------- docs/UnitTest/RegisterTest.php | 53 ++++------- 2 files changed, 102 insertions(+), 117 deletions(-) diff --git a/Register.php b/Register.php index 053a395..3dbec3f 100644 --- a/Register.php +++ b/Register.php @@ -115,7 +115,7 @@ class BSRegister * The master registry list * @var array */ - private $registry = array(); + private $registry = null; /** * An array of debug messages @@ -128,7 +128,9 @@ class BSRegister * Constructor */ private function __construct() - {} + { + $this->registry = new BSVariableRegistry(); + } // ################################################################### /** @@ -260,87 +262,12 @@ class BSRegister // ################################################################### /** - * 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; } @@ -425,6 +352,79 @@ class BSRegister } } } + + +} + +/** + * 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; + } } /*=====================================================================*\ diff --git a/docs/UnitTest/RegisterTest.php b/docs/UnitTest/RegisterTest.php index db81747..7496d73 100644 --- a/docs/UnitTest/RegisterTest.php +++ b/docs/UnitTest/RegisterTest.php @@ -76,59 +76,44 @@ class RegisterTest extends PHPUnit_Framework_TestCase 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) @@ -139,7 +124,7 @@ class RegisterTest extends PHPUnit_Framework_TestCase { try { - BSRegister::Unregister('keyThatWontExist'); + unset(BSRegister::Registry()->keyThatWontExist); $this->fail('exception expected'); } catch (Exception $e) @@ -149,10 +134,10 @@ class RegisterTest extends PHPUnit_Framework_TestCase 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() -- 2.43.5