Removing the BSVariableRegistry and instead making public static vars in BSApp for...
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 13 Jan 2008 21:37:31 +0000 (13:37 -0800)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 13 Jan 2008 21:37:31 +0000 (13:37 -0800)
* 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
App.php
Input.php
Installer.php
Mail.php
Pagination.php
Template.php

diff --git a/Api.php b/Api.php
index 227240e01f4e49f6224cd0425cb4edb3b28b4bbf..20f8488539ec99e825dd9dca7cc0a87e7e31d032 100644 (file)
--- 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 f11756640c49d90b711e78fa949395ef40b32337..d894a7b7e480fa475b232bbe96a8d583f0630c99 100644 (file)
--- 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 = '<select><option>Debug Notices (' . sizeof(self::_instance()->debugInfo) . ')</option>';
-               foreach (self::_instance()->debugInfo AS $notice)
+               $output = '<select><option>Debug Notices (' . sizeof(self::$debugInfo) . ')</option>';
+               foreach (self::$debugInfo AS $notice)
                {
                        $output .= "<option>--- $notice</option>";
                }
@@ -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
index bfb37e48d32f53e6d60195908876ab6d2488aa32..9530d499abe144c99edcd3d2e8f92a64b6d8eef7 100644 (file)
--- 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)
index 64c1dded808dbce2fa5546cf9f5ef1a986ca62fa..56ed91765045160e551866cf06a55981c844b15e 100644 (file)
@@ -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));
        }
        
        // ###################################################################
index d2849856e7aa0fa8021647cbe8db27d3637d3fc7..ce4392e2fd432aa5024155dd72860ce7a37dcd09 100644 (file)
--- 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');
index a2325f76fd6d4b47313b4b76798054b41e20c84c..6e3951cd989932f05568b782e23cedfeacf19d36 100644 (file)
@@ -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);
        }
        
        // ###################################################################
index d364c471d5f92cfb536bec17294cc622e8f4558d..6fb6314ce750f4e57b9879f9c267862b577846a3 100644 (file)
@@ -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 .= "<br /><div style=\"color: red\" align=\"center\"><strong>Uncached Templates:</strong>" . implode(', ', $tpls) . " )</div>\n";
                        }
                        
-                       if (BSApp::registry()->getType('Db'))
+                       if (BSApp::$db)
                        {
-                               $queries = BSApp::registry()->getType('Db')->getHistory();
+                               $queries = BSApp::$db->getHistory();
                                
                                $debugBlock .= "<br />\n" . '<table cellpadding="4" cellspacing="1" border="0" align="center" width="30%" style="background-color: rgb(60, 60, 60); color: white">' . "\n\t" . '<tr><td><strong>Query Debug</strong></td></tr>';