From 4364c587ba6e79537c0eaf973eda99a94348dda4 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 25 Nov 2006 21:42:51 +0000 Subject: [PATCH] Removing BSLoader and we'll just go with a singleton instance of BSRegister --- Input.php | 8 +- Loader.php | 377 ------------------------------------- Register.php | 238 ++++++++++++++++++++++- UnitTest/AllTests.php | 1 - UnitTest/FunctionsTest.php | 2 +- UnitTest/LoaderTest.php | 60 ------ UnitTest/RegisterTest.php | 28 ++- 7 files changed, 264 insertions(+), 450 deletions(-) delete mode 100644 Loader.php delete mode 100644 UnitTest/LoaderTest.php diff --git a/Input.php b/Input.php index 79c6bed..be697e4 100644 --- a/Input.php +++ b/Input.php @@ -109,8 +109,8 @@ class BSInput set_magic_quotes_runtime(0); // some debug info that's always useful - BSLoader::GetRegister()->debug('magic_quotes_gpc = ' . $this->magicquotes); - BSLoader::GetRegister()->debug('register_globals = ' . ini_get('register_globals')); + BSRegister::Instance()->debug('magic_quotes_gpc = ' . $this->magicquotes); + BSRegister::Instance()->debug('register_globals = ' . ini_get('register_globals')); $this->sanitizeInputData(); @@ -404,11 +404,11 @@ class BSInput trigger_error('No external hosts are allowed to POST to this application'); exit; } - BSLoader::GetRegister()->debug('remote post check = ok'); + BSRegister::Instance()->debug('remote post check = ok'); } else { - BSLoader::GetRegister()->debug('remote post check = FAILED'); + BSRegister::Instance()->debug('remote post check = FAILED'); } } } diff --git a/Loader.php b/Loader.php deleted file mode 100644 index 810c735..0000000 --- a/Loader.php +++ /dev/null @@ -1,377 +0,0 @@ - $_val) - { - if (isset(${$_key})) - { - unset(${$_key}); - } - } - } - } -} - -/** -* System Loader -* -* This class contains static methods that are used to create new registers and -* load modules. -* -* @author Blue Static -* @copyright Copyright ©2002 - [#]year[#], Blue Static -* @version $Revision$ -* @package ISSO -* -*/ -class BSLoader -{ - /** - * Singleton instance - * @var object - */ - private static $instance; - - /** - * Array of all the registers - * @var array - */ - private $registers = array(); - - /** - * The main register - * @var object - */ - private $main; - - // ################################################################### - /** - * Constructor - */ - private function __construct() {} - - // ################################################################### - /** - * Returns the shared instance of the BSLoader singleton class. - * - * @return object The BSLoader shared instance - */ - private static function SharedInstance() - { - if (!self::$instance) - { - self::$instance = new BSLoader; - set_error_handler(array(self::$instance, 'errorHandler')); - } - return self::$instance; - } - - // ################################################################### - /** - * Creates a new BSRegister instance and returns it. - * - * @return object New register - */ - public static function NewRegister() - { - require_once('ISSO/Register.php'); - - self::SharedInstance()->registers[] = $return = new BSRegister(); - - return $return; - } - - // ################################################################### - /** - * Returns the array of all the registers - * - * @return array The array of all the registers - */ - public static function GetAllRegisters() - { - return self::SharedInstance()->registers; - } - - // ################################################################### - /** - * Sets the main register - * - * @param object New main register - */ - public static function SetRegister($register) - { - if (get_class($register) != 'BSRegister') - { - trigger_error('BSLoader::SetRegister() was not passed a BSRegister object'); - return; - } - - self::SharedInstance()->main = $register; - } - - // ################################################################### - /** - * Gets the main register if no argument is passed, or an arbitrary - * one if the index of a register is passed. - * - * @param integer Register index - * - * @return object Specified register - */ - public static function &GetRegister($index = null) - { - if (is_null($index)) - { - if (!isset(self::SharedInstance()->main)) - { - trigger_error('Cannot fetch the main register because it has not been set with BSLoader::SetRegister()'); - return; - } - return self::SharedInstance()->main; - } - else - { - if (!isset(self::SharedInstance()->registers["$index"])) - { - trigger_error('Invalid register index passed to BSLoader::GetRegister()'); - return; - } - return self::SharedInstance()->registers["$index"]; - } - } - - // ################################################################### - /** - * Determines if there's a main register set. If not, returns FALSE. - * - * @return bool Is there a main register set? - */ - public static function HasRegister() - { - return (isset(self::SharedInstance()->main) AND self::SharedInstance()->main instanceof BSRegister); - } - - // ################################################################### - /** - * Loads the specified module. - * - * @param string Module name - * - * @return object Instantiated module - */ - public static function LoadModule($name) - { - @include_once("ISSO/$name.php"); - - $class = "BS$name"; - - if (!class_exists($class)) - { - trigger_error('Specifed module does not conform to the ISSO specification, or the class is missing'); - return; - } - - return new $class; - } - - // ################################################################### - /** - * Prints an ISSO message - * - * @param string The title of the message - * @param string The content of the message - * @param integer Type of message to be printed - * @param bool Return the output? - * @param bool Show the debug stack? - * @param integer Message width - * - * @return mixed Output or null - */ - public static function Message($title, $message, $type, $return = false, $stack = true, $width = 500) - { - switch ($type) - { - // Message - case 1: - $prefix = 'Message'; - $color = '#669900'; - $font = '#000000'; - break; - - // Warning - case 2: - $prefix = 'Warning'; - $color = '#003399'; - $font = '#FFFFFF'; - break; - - case 3: - $prefix = 'Error'; - $color = '#990000'; - $font = '#EFEFEF'; - break; - } - - echo "\n
\n"; - echo "\n\n\t\n"; - echo "\n\n\t\n"; - if ($stack AND self::HasRegister() AND self::GetRegister()->getDebug()) - { - echo "\n\n\t\n"; - } - echo "\n
$prefix: $title
$message
Debug Stack:
";
-			debug_print_backtrace();
-			echo "
\n
\n"; - } - - // ################################################################### - /** - * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE, - * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE - * - * @param integer Error number - * @param string Error message string - * @param string File that contains the error - * @param string The line number of the error - * @param string The active symbol table at which point the error occurred - */ - private function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) - { - $level = ini_get('error_reporting'); - - switch ($errno) - { - // So we don't need to specify the error type in trigger_error(), all E_USER_NOTICEs - // are fatal and automatically kill the script - case E_USER_NOTICE: - $title = 'Fatal'; - $mode = 3; - break; - - // A non-fatal but important warning - case E_WARNING: - $title = 'Warning'; - $mode = 2; - if (!($level & E_WARNING)) - { - return; - } - break; - - // A non-fatal notice that should all but be ignored unless in dev environments - case E_NOTICE: - case E_STRICT: - $title = 'Notice'; - $mode = 1; - if (!($level & E_NOTICE)) - { - return; - } - break; - - case E_USER_WARNING: - case E_USER_NOTICE: - default: - trigger_error('The only error types supported are E_USER_NOTICE (fatal), E_WARNING, and E_NOTICE'); - break; - } - - $errstr .= " in $errfile on line $errline"; - - $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr); - - self::Message($title, $errstr, $mode); - - if ($errno == E_USER_NOTICE) - { - exit; - } - } - - // ################################################################### - /** - * Creates a table that explains the error reporting levels and their - * state - */ - public static function ExplainErrorReporting() - { - $levels = array( - 'E_ERROR' => E_ERROR, - 'E_WARNING' => E_WARNING, - 'E_PARSE' => E_PARSE, - 'E_NOTICE' => E_NOTICE, - 'E_CORE_ERROR' => E_CORE_ERROR, - 'E_CORE_WARNING' => E_CORE_WARNING, - 'E_COMPILE_ERROR' => E_COMPILE_ERROR, - 'E_COMPILE_WARNING' => E_COMPILE_WARNING, - 'E_USER_ERROR' => E_USER_ERROR, - 'E_USER_WARNING' => E_USER_WARNING, - 'E_USER_NOTICE' => E_USER_NOTICE, - 'E_ALL' => E_ALL, - 'E_STRICT' => E_STRICT - ); - - $table = ''; - - foreach ($levels AS $name => $value) - { - $table .= ' - - - - '; - } - - $table .= ' -
' . $name . '' . (ini_get('error_reporting') & $value) . '
'; - - self::Message('Error Reporting', $table, 1); - } -} - -/*=====================================================================*\ -|| ################################################################### -|| # $HeadURL$ -|| # $Id$ -|| ################################################################### -\*=====================================================================*/ -?> \ No newline at end of file diff --git a/Register.php b/Register.php index e84889e..5801b1d 100644 --- a/Register.php +++ b/Register.php @@ -25,6 +25,32 @@ * @package ISSO */ +// we need PHP5 to run +if (!function_exists('stripos')) +{ + trigger_error('You need PHP version 5.0.0 or newer to run ISSO', E_USER_ERROR); + exit; +} + +// get rid of register_globals +if ((bool)ini_get('register_globals') === true) +{ + $superglobals = array('_GET', '_COOKIE', '_FILES', '_POST', '_SERVER', '_ENV'); + foreach ($superglobals AS $global) + { + if (is_array(${$global})) + { + foreach (${$global} AS $_key => $_val) + { + if (isset(${$_key})) + { + unset(${$_key}); + } + } + } + } +} + require_once('ISSO/Functions.php'); /** @@ -32,7 +58,8 @@ require_once('ISSO/Functions.php'); * * This is an ISSO registry class. It holds all of the ISSO system variables as well * as serving as an object registry that is avaliable in the global scope to prevent -* globalization of variables. +* globalization of variables. There can only be one instance of this existing +* at any given time. * * @author Blue Static * @copyright Copyright ©2002 - [#]year[#], Blue Static @@ -42,6 +69,12 @@ require_once('ISSO/Functions.php'); */ class BSRegister { + /** + * Instance of this class + * @var object + */ + private static $instance; + /** * Application name * @var string @@ -84,6 +117,32 @@ class BSRegister */ private $debugInfo = array(); + // ################################################################### + /** + * Constructor: set the error handler + */ + private function __construct() + { + set_error_handler(array(self::$instance, '_errorHandler')); + } + + // ################################################################### + /** + * Returns the single instance of the register + * + * @param string A string param + * + * @return object BSRegister instance + */ + public static function Instance() + { + if (self::$instance == null) + { + self::$instance = new BSRegister(); + } + return self::$instance; + } + // ################################################################### /** * Sets the application name @@ -276,6 +335,183 @@ class BSRegister $this->debugInfo[] = $msg; } } + + // ################################################################### + /** + * Loads the specified module. + * + * @param string Module name + * + * @return object Instantiated module + */ + public static function LoadModule($name) + { + @include_once("ISSO/$name.php"); + + $class = "BS$name"; + + if (!class_exists($class)) + { + trigger_error('Specifed module does not conform to the ISSO specification, or the class is missing'); + return; + } + + return new $class; + } + + // ################################################################### + /** + * Prints an ISSO message + * + * @param string The title of the message + * @param string The content of the message + * @param integer Type of message to be printed + * @param bool Return the output? + * @param bool Show the debug stack? + * @param integer Message width + * + * @return mixed Output or null + */ + public static function Message($title, $message, $type, $return = false, $stack = true, $width = 500) + { + switch ($type) + { + // Message + case 1: + $prefix = 'Message'; + $color = '#669900'; + $font = '#000000'; + break; + + // Warning + case 2: + $prefix = 'Warning'; + $color = '#003399'; + $font = '#FFFFFF'; + break; + + case 3: + $prefix = 'Error'; + $color = '#990000'; + $font = '#EFEFEF'; + break; + } + + echo "\n
\n"; + echo "\n\n\t\n"; + echo "\n\n\t\n"; + if ($stack AND self::Instance()->getDebug()) + { + echo "\n\n\t\n"; + } + echo "\n
$prefix: $title
$message
Debug Stack:
";
+			debug_print_backtrace();
+			echo "
\n
\n"; + } + + // ################################################################### + /** + * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE, + * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE + * + * @param integer Error number + * @param string Error message string + * @param string File that contains the error + * @param string The line number of the error + * @param string The active symbol table at which point the error occurred + */ + private function _errorHandler($errno, $errstr, $errfile, $errline, $errcontext) + { + $level = ini_get('error_reporting'); + + switch ($errno) + { + // So we don't need to specify the error type in trigger_error(), all E_USER_NOTICEs + // are fatal and automatically kill the script + case E_USER_NOTICE: + $title = 'Fatal'; + $mode = 3; + break; + + // A non-fatal but important warning + case E_WARNING: + $title = 'Warning'; + $mode = 2; + if (!($level & E_WARNING)) + { + return; + } + break; + + // A non-fatal notice that should all but be ignored unless in dev environments + case E_NOTICE: + case E_STRICT: + $title = 'Notice'; + $mode = 1; + if (!($level & E_NOTICE)) + { + return; + } + break; + + case E_USER_WARNING: + case E_USER_NOTICE: + default: + trigger_error('The only error types supported are E_USER_NOTICE (fatal), E_WARNING, and E_NOTICE'); + break; + } + + $errstr .= " in $errfile on line $errline"; + + $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr); + + self::Message($title, $errstr, $mode); + + if ($errno == E_USER_NOTICE) + { + exit; + } + } + + // ################################################################### + /** + * Creates a table that explains the error reporting levels and their + * state + */ + public static function ExplainErrorReporting() + { + $levels = array( + 'E_ERROR' => E_ERROR, + 'E_WARNING' => E_WARNING, + 'E_PARSE' => E_PARSE, + 'E_NOTICE' => E_NOTICE, + 'E_CORE_ERROR' => E_CORE_ERROR, + 'E_CORE_WARNING' => E_CORE_WARNING, + 'E_COMPILE_ERROR' => E_COMPILE_ERROR, + 'E_COMPILE_WARNING' => E_COMPILE_WARNING, + 'E_USER_ERROR' => E_USER_ERROR, + 'E_USER_WARNING' => E_USER_WARNING, + 'E_USER_NOTICE' => E_USER_NOTICE, + 'E_ALL' => E_ALL, + 'E_STRICT' => E_STRICT + ); + + $table = ''; + + foreach ($levels AS $name => $value) + { + $table .= ' + + + + '; + } + + $table .= ' +
' . $name . '' . (ini_get('error_reporting') & $value) . '
'; + + self::Message('Error Reporting', $table, 1); + } } /*=====================================================================*\ diff --git a/UnitTest/AllTests.php b/UnitTest/AllTests.php index 60e14a3..1bfc54e 100644 --- a/UnitTest/AllTests.php +++ b/UnitTest/AllTests.php @@ -8,7 +8,6 @@ require_once('../UnitTestReport.php'); $test = new GroupTest('All Tests'); -$test->addTestFile('LoaderTest.php'); $test->addTestFile('RegisterTest.php'); $test->addTestFile('FunctionsTest.php'); diff --git a/UnitTest/FunctionsTest.php b/UnitTest/FunctionsTest.php index bff9b26..ef67bdc 100644 --- a/UnitTest/FunctionsTest.php +++ b/UnitTest/FunctionsTest.php @@ -1,7 +1,7 @@ instance = BSLoader::NewRegister(); - - $this->assertEqual(get_class($this->instance), 'BSRegister'); - } - - public function testGetAllRegisters() - { - $this->assertIsA(BSLoader::GetAllRegisters(), 'array', 'Register array is not an array'); - $this->assertEqual(sizeof(BSLoader::GetAllRegisters()), 1, 'Register array is wrong size'); - } - - public function testSetGetRegister() - { - BSLoader::SetRegister($this->instance); - - $this->assertReference(BSLoader::GetRegister(), $this->instance); - } - - public function testRegisterAfterMakeNew() - { - BSLoader::NewRegister(); - - $this->assertEqual(get_class(BSLoader::GetRegister(1)), 'BSRegister', 'Failed to get arbitrary register'); - $this->assertEqual(sizeof(BSLoader::GetAllRegisters()), 2, 'Sizeof register array is wrong'); - $this->assertReference($this->instance, BSLoader::GetRegister(), 'Main register does not match instance'); - } - - public function testLoadModule() - { - $this->assertEqual(get_class(BSLoader::LoadModule('Input')), 'BSInput'); - } - - public function testLoadBadModule() - { - BSLoader::LoadModule('nonExistentModule'); - $this->assertError(); - } -} - -?> \ No newline at end of file diff --git a/UnitTest/RegisterTest.php b/UnitTest/RegisterTest.php index 21c38b8..0de7f60 100644 --- a/UnitTest/RegisterTest.php +++ b/UnitTest/RegisterTest.php @@ -1,7 +1,7 @@ fixture = BSLoader::NewRegister(); + $this->fixture = BSRegister::Instance(); + } + + public function testNewRegister() + { + $this->assertTrue($this->fixture instanceof BSRegister); + $this->assertReference($this->fixture, BSRegister::Instance()); + } + + public function testLoadModule() + { + $this->assertTrue(BSRegister::LoadModule('Input') instanceof BSInput); + } + + public function testLoadBadModule() + { + BSRegister::LoadModule('nonExistentModule'); + $this->assertError(); } public function testSetGetAppPath() @@ -62,10 +79,9 @@ class RegisterTest extends UnitTestCase public function testGetAll() { - $new = BSLoader::NewRegister(); - $new->register('test', 1); - $this->assertIsA($new->getAll(), 'array'); - $this->assertEqual(sizeof($new->getAll()), 1); + $this->fixture->register('test', 1); + $this->assertIsA($this->fixture->getAll(), 'array'); + $this->assertEqual(sizeof($this->fixture->getAll()), 2); } public function testOverWriteRegister() -- 2.22.5