From 33a7b4d2a212cb0a5fe50f3d14037b49c8b8e25e Mon Sep 17 00:00:00 2001 From: sebastian Date: Fri, 12 Feb 2010 13:44:16 +0000 Subject: [PATCH] #2360 Autoload / auto-initialization of classes / objects --- .../src/modules/htmlUI/var/ui_base.inc.php | 84 ++++++++++++++----- campcaster/src/modules/htmlUI/var/ui_conf.php | 13 +-- 2 files changed, 65 insertions(+), 32 deletions(-) diff --git a/campcaster/src/modules/htmlUI/var/ui_base.inc.php b/campcaster/src/modules/htmlUI/var/ui_base.inc.php index faae0caad..5e917e211 100644 --- a/campcaster/src/modules/htmlUI/var/ui_base.inc.php +++ b/campcaster/src/modules/htmlUI/var/ui_base.inc.php @@ -106,6 +106,17 @@ function _getNumArr($start, $end, $step=1) return $arr; } // fn _getNumArr +function __autoload($p_className) +{ + foreach (uiBase::$m_classMapping as $item) { + if (strtolower($p_className) == $item['class']) { + $class_filename = $item['file']; + require_once(dirname(__FILE__).'/'.$class_filename); + break; + } + } +} + /** * HTML User Interface module @@ -125,17 +136,17 @@ class uiBase /** * @var uiScratchPad */ - public $SCRATCHPAD; + private $SCRATCHPAD; /** * @var uiSearch */ - public $SEARCH; + private $SEARCH; /** * @var uiBrowse */ - public $BROWSE; + private $BROWSE; /** * @todo loading HUBBROWSE on every page load slows things down @@ -143,37 +154,37 @@ class uiBase * * @var uiHubBrowse */ - public $HUBBROWSE; + private $HUBBROWSE; /** * @var uiHubSearch */ - public $HUBSEARCH; + private $HUBSEARCH; /** * @var uiPlaylist */ - public $PLAYLIST; + private $PLAYLIST; /** * @var uiScheduler */ - public $SCHEDULER; + private $SCHEDULER; /** * @var uiSubjects */ - public $SUBJECTS; + private $SUBJECTS; /** * @var uiExchange */ - public $EXCHANGE; + private $EXCHANGE; /** * @var uiTransfers */ - public $TRANSFERS; + private $TRANSFERS; /** * @var string @@ -207,6 +218,28 @@ class uiBase * @var string */ public $alertMsg; + + /** + * This mapping keeps relation between uiBase::properties, + * class names and filenames and is used in + * __autoload() and uiBase::__get() functions. + * + * @var array + */ + public static $m_classMapping = array( + 'SCRATCHPAD' => array('class' => 'uiscratchpad', 'file' => 'ui_scratchpad.class.php'), + 'SEARCH' => array('class' => 'uisearch', 'file' => 'ui_search.class.php'), + 'BROWSE' => array('class' => 'uibrowse', 'file' => 'ui_browse.class.php'), + 'HUBBROWSE' => array('class' => 'uihubbrowse', 'file' => 'ui_hubBrowse.class.php'), + 'HUBSEARCH' => array('class' => 'uihubsearch', 'file' => 'ui_hubSearch.class.php'), + 'PLAYLIST' => array('class' => 'uiplaylist', 'file' => 'ui_playlist.class.php'), + 'SCHEDULER' => array('class' => 'uischeduler', 'file' => 'ui_scheduler.class.php'), + 'SUBJECTS' => array('class' => 'uisubjects', 'file' => 'ui_subjects.class.php'), + 'EXCHANGE' => array('class' => 'uiexchange', 'file' => 'ui_exchange.class.php'), + 'TRANSFERS' => array('class' => 'uitransfers', 'file' => 'ui_transfers.class.php'), + 'CALENDAR' => array('class' => 'uicalendar', 'file' => 'ui_calendar.class.php'), + 'JSCOM' => array('class' => 'jscom', 'file' => 'ui_jscom.php') + ); /** @@ -248,16 +281,27 @@ class uiBase public function init() { $this->STATIONPREFS =& $_SESSION[UI_STATIONINFO_SESSNAME]; - $this->SCRATCHPAD = new uiScratchPad($this); - $this->SEARCH = new uiSearch($this); - $this->BROWSE = new uiBrowse($this); - $this->HUBBROWSE = new uiHubBrowse($this); - $this->HUBSEARCH = new uiHubSearch($this); - $this->PLAYLIST = new uiPlaylist($this); - $this->SCHEDULER = new uiScheduler($this); - $this->SUBJECTS = new uiSubjects($this); - $this->EXCHANGE = new uiExchange($this); - $this->TRANSFERS = new uiTransfers($this); + } + + /** + * Dynamically initialize uiBase properties, + * which keep objects (uiBase->SEARCH, $uiBase->BROWSE etc) + * + * @param unknown_type $p_class + * @return unknown + */ + public function __get($p_class) + { + if (strtoupper($p_class !== $p_class)) { + return; + } + + if (!is_object($this->$p_class)) { + if ($class_name = uiBase::$m_classMapping[$p_class]['class']) { + $this->$p_class = new $class_name($this); + } + } + return $this->$p_class; } diff --git a/campcaster/src/modules/htmlUI/var/ui_conf.php b/campcaster/src/modules/htmlUI/var/ui_conf.php index bff8f993a..2df935105 100644 --- a/campcaster/src/modules/htmlUI/var/ui_conf.php +++ b/campcaster/src/modules/htmlUI/var/ui_conf.php @@ -149,21 +149,10 @@ $CC_CONFIG = array_merge($CC_CONFIG, ), ) ); + require_once(dirname(__FILE__).'/ui_base.inc.php'); -require_once(dirname(__FILE__).'/ui_scratchpad.class.php'); -require_once(dirname(__FILE__).'/ui_playlist.class.php'); -require_once(dirname(__FILE__).'/ui_search.class.php'); -require_once(dirname(__FILE__).'/ui_browse.class.php'); -require_once(dirname(__FILE__).'/ui_hubBrowse.class.php'); -require_once(dirname(__FILE__).'/ui_hubSearch.class.php'); -require_once(dirname(__FILE__).'/ui_transfers.class.php'); require_once('../../../storageServer/var/GreenBox.php'); require_once(dirname(__FILE__).'/formmask/generic.inc.php'); -require_once(dirname(__FILE__).'/ui_calendar.class.php'); -require_once(dirname(__FILE__).'/ui_scheduler.class.php'); -require_once(dirname(__FILE__).'/ui_subjects.class.php'); -require_once(dirname(__FILE__).'/ui_jscom.php'); -require_once(dirname(__FILE__).'/ui_exchange.class.php'); require_once('DB.php'); require_once('HTML/QuickForm.php');