From 59fd141e081766718dcf747529f58be3cf818942 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 13 Jan 2008 13:37:31 -0800 Subject: [PATCH] Removing the BSVariableRegistry and instead making public static vars in BSApp for the necessary inter-linking modules * Api.php: No longer use BSApp::registry() but rather just the cvars * App.php: Make the ivars cvars so we no longer need singleton stuff (BSApp::_instance): Removed (BSApp::registry): Removed (BSApp::load_module): Removed, just directly instantiate now (BSApp::required_modules): Removed (BSVariableRegistry): Removed * Input.php: Use cvars instead of the BSApp::registry() * Installer.php: ditto * Mail.php: ditto * Pagination.php: ditto * Template.php: ditto --- Api.php | 36 ++++++---- App.php | 188 +++++++------------------------------------------ Input.php | 2 +- Installer.php | 7 +- Mail.php | 31 ++++---- Pagination.php | 26 ++++--- Template.php | 6 +- 7 files changed, 87 insertions(+), 209 deletions(-) diff --git a/Api.php b/Api.php index 227240e..20f8488 100644 --- a/Api.php +++ b/Api.php @@ -133,13 +133,19 @@ abstract class BSApi */ private $exception = null; - // ################################################################### /** - * Constructor: cannot instantiate class directly - */ + * Constructor + */ public function __construct() { - BSApp::required_modules(array('Db', 'Input')); + if (!BSApp::$input instanceof BSInput) + { + throw new Exception('BSApp::$input is not an instance of BSInput'); + } + if (!BSApp::$db instanceof BSDb) + { + throw new Exception('BSApp::$db is not an instance of BSDb'); + } } // ################################################################### @@ -186,7 +192,7 @@ abstract class BSApi throw new Exception('Field "' . $field . '" is not valid'); } - $this->values["$field"] = ($doclean ? BSApp::registry()->getType('Input')->clean($value, $this->fields["$field"][F_TYPE]) : $value); + $this->values["$field"] = ($doclean ? BSApp::$input->clean($value, $this->fields["$field"][F_TYPE]) : $value); $this->setfields["$field"] = $field; @@ -267,7 +273,7 @@ abstract class BSApi $this->_runActionMethod('pre_fetch', $doPre); - $result = BSApp::registry()->getType('Db')->queryFirst("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}"); + $result = BSApp::$db->queryFirst("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}"); if (!$result) { return false; @@ -301,9 +307,9 @@ abstract class BSApi $values[] = $this->_prepareFieldForSql($field); } - BSApp::registry()->getType('Db')->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")"); + BSApp::$db->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")"); - if (BSApp::registry()->getType('DbPostgreSql')) + if (BSApp::$db instanceof BSDbPostgreSql) { foreach ($this->fields AS $field => $info) { @@ -314,11 +320,11 @@ abstract class BSApi } } - $this->insertid = BSApp::registry()->getType('Db')->insertId($this->prefix . $this->table, $autofield); + $this->insertid = BSApp::$db->insertId($this->prefix . $this->table, $autofield); } else { - $this->insertid = BSApp::registry()->getType('Db')->insertId(); + $this->insertid = BSApp::$db->insertId(); } $this->_runActionMethod('post_insert', $doPost); @@ -348,7 +354,7 @@ abstract class BSApi } $updates = implode(', ', $updates); - BSApp::registry()->getType('Db')->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}"); + BSApp::$db->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}"); $this->_runActionMethod('post_update', $doPost); } @@ -371,7 +377,7 @@ abstract class BSApi $this->_runActionMethod('pre_remove', $doPre); - BSApp::registry()->getType('Db')->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}"); + BSApp::$db->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}"); $this->_runActionMethod('post_remove', $doPost); } @@ -430,7 +436,7 @@ abstract class BSApi if ($type == TYPE_NONE OR $type == TYPE_STR OR $type == TYPE_STRUN) { - return "'" . BSApp::registry()->getType('Db')->escapeString($this->values["$name"]) . "'"; + return "'" . BSApp::$db->escapeString($this->values["$name"]) . "'"; } else if ($type == TYPE_BOOL) { @@ -438,7 +444,7 @@ abstract class BSApi } else if ($type == TYPE_BIN) { - return "'" . BSApp::registry()->getType('Db')->escapeBinary($this->values["$name"]) . "'"; + return "'" . BSApp::$db->escapeBinary($this->values["$name"]) . "'"; } else { @@ -482,7 +488,7 @@ abstract class BSApi */ protected function _verifyIsNotUnique($field) { - $res = BSApp::registry()->getType('Db')->queryFirst("SELECT $field FROM {$this->prefix}{$this->table} WHERE $field = " . $this->_prepareFieldForSql($field) . (empty($this->condition) ? "" : " AND !({$this->condition})")); + $res = BSApp::$db->queryFirst("SELECT $field FROM {$this->prefix}{$this->table} WHERE $field = " . $this->_prepareFieldForSql($field) . (empty($this->condition) ? "" : " AND !({$this->condition})")); if ($res) { $this->_error(new FieldException(sprintf(_('The "%1$s" field must contain a unique value'), $field), $field)); diff --git a/App.php b/App.php index f117566..d894a7b 100644 --- a/App.php +++ b/App.php @@ -68,55 +68,23 @@ require_once(ISSO . '/Functions.php'); */ class BSApp { - /** - * Instance of this class - * @var object - */ - private static $instance; - /** * Debug mode? * @var bool */ - private $debug = false; - - /** - * The master registry list - * @var array - */ - private $registry = null; + private static $debug = false; /** * An array of debug messages * @var array */ - private $debugInfo = array(); + private static $debugInfo = array(); - // ################################################################### /** - * Constructor - */ + * Constructor + */ private function __construct() - { - $this->registry = new BSVariableRegistry(); - } - - // ################################################################### - /** - * Returns the single instance of the register - * - * @param string A string param - * - * @return object BSApp instance - */ - private static function _instance() - { - if (!self::$instance) - { - self::$instance = new BSApp(); - } - return self::$instance; - } + {} // ################################################################### /** @@ -126,7 +94,7 @@ class BSApp */ public static function set_debug($debug) { - self::_instance()->debug = $debug; + self::$debug = $debug; } // ################################################################### @@ -137,19 +105,7 @@ class BSApp */ public static function get_debug() { - return self::_instance()->debug; - } - - // ################################################################### - /** - * Returns the registry object without it ever being able to be cleared - * or unset - * - * @return StdClass - */ - public static function registry() - { - return self::_instance()->registry; + return self::$debug; } // ################################################################### @@ -161,9 +117,9 @@ class BSApp */ public static function debug($msg) { - if (self::_instance()->debug) + if (self::$debug) { - self::_instance()->debugInfo[] = $msg; + self::$debugInfo[] = $msg; } } @@ -175,8 +131,8 @@ class BSApp */ public static function get_debug_list() { - $output = ''; + foreach (self::$debugInfo AS $notice) { $output .= ""; } @@ -184,125 +140,31 @@ class BSApp } // ################################################################### + // modules + /** - * Loads the specified module. - * - * @param string Module name - * - * @return object Instantiated module - */ - public static function load_module($name) - { - if (self::get_debug()) - { - include_once(ISSO . "/$name.php"); - } - else - { - @include_once(ISSO . "/$name.php"); - } - - $class = "BS$name"; - - if (!class_exists($class)) - { - throw new Exception('Specifed module does not conform to the ISSO specification, or the class is missing'); - return; - } - - return new $class; - } - - // ################################################################### - /** - * This function is used by other framework modules to check and see if - * the passed array of module names have been loaded. If not, this will - * throw an error informing the developer that the given modules are - * required in order for the framework to work. - * - * @param array Array of module names to check for loadedness - */ - public static function required_modules($modules) - { - foreach ($modules AS $module) - { - if (self::registry()->getType($module) == null) - { - throw new Exception('The ' . $module . ' module is required in order to use this framework module'); - } - } - } -} - -/** - * Variable Registry - * - * This class can be used to store unlimited number of properties. It is a (make believe) - * inner class to BSApp and cannot be instantiated outside of it. - * - * @author Blue Static - * @copyright Copyright (c)2005 - 2008, Blue Static - * @version $Id$ - * @package ISSO - * - */ -class BSVariableRegistry -{ - // ################################################################### - /** - * Constructor: only able to be called by BSApp + * BSDate + * @var object */ - public function __construct() - { - $bt = debug_backtrace(); - if (!isset($bt[1]['class']) OR $bt[1]['class'] != 'BSApp') - { - exit('You cannot instantiate ' . __CLASS__ . ' directly. It is an internal class.'); - } - } + public static $date; - // ################################################################### /** - * If you try and get a non-existent property, throw a new exception + * BSDb + * @var object */ - public function __get($name) - { - throw new Exception('Failed to get nonexistent property "' . $name . '"'); - } + public static $db; - // ################################################################### /** - * Returns the first value of the given type in the variable registry - * - * @return mixed + * BSInput + * @var object */ - public function getType($class) - { - $class = 'BS' . $class; - foreach ($this AS $object) - { - if ($object instanceof $class) - { - return $object; - } - } - } + public static $input; - // ################################################################### /** - * Returns all the objects in the registry by iterating over it - * - * @return array + * BSTemplate + * @var object */ - public function allObjects() - { - $registry = array(); - foreach ($this AS $key => $value) - { - $registry[$key] = $value; - } - return $registry; - } + public static $template; } ?> \ No newline at end of file diff --git a/Input.php b/Input.php index bfb37e4..9530d49 100644 --- a/Input.php +++ b/Input.php @@ -209,7 +209,7 @@ class BSInput */ public function escape($str, $force = true) { - $db = BSApp::registry()->getType('Db'); + $db = BSApp::$db; if ($this->magicquotes AND !$force) { if ($db) diff --git a/Installer.php b/Installer.php index 64c1dde..56ed917 100644 --- a/Installer.php +++ b/Installer.php @@ -60,7 +60,10 @@ abstract class BSInstaller { $this->fileName = $fileName; - BSApp::required_modules(array('Input')); + if (!BSApp::$input instanceof BSInput) + { + throw new Exception('BSApp::$input is not an instance of BSInput'); + } $methods = get_class_methods($this); foreach ($methods AS $name) @@ -72,7 +75,7 @@ abstract class BSInstaller } natsort($this->steps); - $this->_runStep(BSApp::registry()->getType('Input')->inputClean('step', TYPE_UINT)); + $this->_runStep(BSApp::$input->inputClean('step', TYPE_UINT)); } // ################################################################### diff --git a/Mail.php b/Mail.php index d284985..ce4392e 100644 --- a/Mail.php +++ b/Mail.php @@ -89,6 +89,17 @@ class BSMail * @access public */ private $charset = 'utf-8'; // should we be using iso-8859-1 ? + + /** + * Constructor + */ + public function __construct() + { + if (!BSApp::$input instanceof BSInput) + { + throw new Exception('BSApp::$input is not an instance of BSInput'); + } + } // ################################################################### /** @@ -185,14 +196,6 @@ class BSMail throw new Exception('You need to specify an email address'); } - // load the input sanitizer - $input = BSApp::registry()->getType('Input'); - if ($input == null) - { - BSApp::debug(ISSO . '/Input not loaded, so manually doing so'); - $input = BSApp::load_module('Input'); - } - // make sure we have a mailer // TODO - add support for SMTP if (!@ini_get('sendmail_path')) @@ -203,14 +206,14 @@ class BSMail // sort out the to addresses $address = $this->_fetchFirstLine($address); - $address = trim($input->unsanitize($address)); + $address = trim(BSApp::$input->unsanitize($address)); $name = $this->_fetchFirstLine($name); - $name = trim($input->unsanitize($name)); + $name = trim(BSApp::$input->unsanitize($name)); $tostring = ($name == null ? $address : "\"$name\" <$address>"); // sanitize the from field $from = $this->_fetchFirstLine($this->from); - $from = trim($input->unsanitize($from)); + $from = trim(BSApp::$input->unsanitize($from)); if (empty($from)) { throw new Exception('You need to specify a from email address'); @@ -218,12 +221,12 @@ class BSMail // sanitize the from name $fromName = $this->_fetchFirstLine($this->fromName); - $fromName = ($fromName == '' ? $from : trim($input->unsanitize($fromName))); + $fromName = ($fromName == '' ? $from : trim(BSApp::$input->unsanitize($fromName))); $fromName = $this->_encodeHeaderValue($this->fromName); // sanitize the subject $subject = $this->_fetchFirstLine($this->subject); - $subject = trim($input->unsanitize($subject)); + $subject = trim(BSApp::$input->unsanitize($subject)); if (empty($subject)) { throw new Exception('You need to specify a subject for the message'); @@ -231,7 +234,7 @@ class BSMail // sanitize the body $bodyText = BSFunctions::convert_line_breaks($this->bodyText, $this->delim); - $bodyText = trim($input->unsanitize($bodyText, true)); + $bodyText = trim(BSApp::$input->unsanitize($bodyText, true)); if (empty($bodyText)) { throw new Exception('You need to specify body text before sending the message'); diff --git a/Pagination.php b/Pagination.php index a2325f7..6e3951c 100644 --- a/Pagination.php +++ b/Pagination.php @@ -106,6 +106,17 @@ class BSPagination */ private $pagenavprocessor = ':undefined:'; + /** + * Constructor + */ + public function __construct() + { + if (!BSApp::$input instanceof BSInput) + { + throw new Exception('BSApp::$input is not an instance of BSInput'); + } + } + // ################################################################### /** * Callback public function for the processing of an indivdual page. Needs @@ -238,16 +249,9 @@ class BSPagination */ public function processIncomingData() { - $input = BSApp::GetType('Input'); - if ($input == null) - { - BSApp::debug(ISSO . '/Input not loaded, so manually doing so'); - $input = BSApp::load_module('Input'); - } - - $this->page = $input->inputClean($this->pagevar, TYPE_INT); - $this->perpage = $input->inputClean($this->perpagevar, TYPE_INT); - $this->pagelinks = $input->clean($this->pagelinks, TYPE_INT); + $this->page = BSApp::$input->inputClean($this->pagevar, TYPE_INT); + $this->perpage = BSApp::$input->inputClean($this->perpagevar, TYPE_INT); + $this->pagelinks = BSApp::$input->clean($this->pagelinks, TYPE_INT); if ($this->page <= 0) { @@ -263,7 +267,7 @@ class BSPagination $this->perpage = $this->maxperpage; } - $this->perpage = $input->clean($this->perpage, TYPE_INT); + $this->perpage = BSApp::$input->clean($this->perpage, TYPE_INT); } // ################################################################### diff --git a/Template.php b/Template.php index d364c47..6fb6314 100644 --- a/Template.php +++ b/Template.php @@ -162,7 +162,7 @@ class BSTemplate $dbCache = array(); if ($this->dbCacheTable) { - $db =& BSApp::registry()->getType('Db'); + $db =& BSApp::$db; $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')"); while ($tpl = $cache->fetchArray()) { @@ -262,9 +262,9 @@ class BSTemplate $debugBlock .= "
Uncached Templates:" . implode(', ', $tpls) . " )
\n"; } - if (BSApp::registry()->getType('Db')) + if (BSApp::$db) { - $queries = BSApp::registry()->getType('Db')->getHistory(); + $queries = BSApp::$db->getHistory(); $debugBlock .= "
\n" . '' . "\n\t" . ''; -- 2.22.5
Query Debug