From 55b8d42b6422f24196078c8adb6a2a59861d0ef4 Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 28 Jan 2010 13:45:42 +0000 Subject: Lazy load on modules. --- utils/modules.php | 50 ++++++++++++++++++++++++++++++++++++------- utils/modules/discography.php | 2 ++ utils/modules/events.php | 2 ++ utils/modules/gallery.php | 2 ++ utils/modules/guestbook.php | 3 +++ utils/modules/icons.php | 3 +++ utils/modules/members.php | 2 ++ utils/modules/news.php | 35 ++++++++++++++++++++---------- utils/modules/pages.php | 14 +++--------- 9 files changed, 83 insertions(+), 30 deletions(-) diff --git a/utils/modules.php b/utils/modules.php index 4c34228..54682a4 100644 --- a/utils/modules.php +++ b/utils/modules.php @@ -2,14 +2,48 @@ $modules = array(); -/** - * Iterate all defined modules, and load them into the global array. - * FIXME: Make this lazy (load on demand) - */ -foreach($MODULES as $modulename) { - include_once($MODULES_DIR . "/" . $modulename . ".php"); - $module = call_user_func($modulename . "_init"); - $modules[$modulename] = $module; +function loadModule($modulename) +{ + global $MODULES_DIR; + global $modules; + + $modulefile = $MODULES_DIR . "/" . $modulename . ".php"; + if(!$modules[$modulename]) { + if(file_exists($modulefile)) { + include_once($modulefile); + $module = call_user_func($modulename . "_init"); + $modules[$modulename] = $module; + } else { + // Module does not exist. + } + } +} + +function getModuleParams($code) +{ + return explode(",", $code); +} + +function runModule($code) +{ + global $modules; + + $str = ""; + + $m = explode("?", $code); + $module = $m[0]; + $params = getModuleParams($m[1]); + + loadModule($module); + + if($modules[$module]) { + $str .= $modules[$module]->run($params); + } else { + $str .= "

CMS ERROR: Could not find module:"; + $str .= " [[" . $modulecode . "]]

\n"; + } + + return $str; } ?> diff --git a/utils/modules/discography.php b/utils/modules/discography.php index 478fdc0..fe5a353 100644 --- a/utils/modules/discography.php +++ b/utils/modules/discography.php @@ -1,6 +1,8 @@ \n"; $str .= "
" . htmlspecialchars_decode($this->title, ENT_QUOTES) . "
\n"; - $str .= "
" . date("D M jS Y G:i", $this->time) . "
\n"; - $str .= "
By: " . $users->findUser($this->userid)->userid . "
\n"; + $str .= "
" . + date("D M jS Y G:i", $this->time) . "
\n"; + $str .= "
By: " . + $users->findUser($this->userid)->userid . "
\n"; $str .= "
\n"; - if($this->icon) $str .= " \"icon\"prefix.$icon->file . "\"/>\n"; + if($this->icon) $str .= " \"icon\"prefix.$icon->file . "\"/>\n"; $str .= " ".$content. "\n"; $str .= "
\n"; $str .= "\n"; @@ -56,7 +61,8 @@ class NewsEntry { return $this->title; } - public function NewsEntry($title, $time, $category, $description, $userid, $icon) + public function NewsEntry($title, $time, $category, + $description, $userid, $icon) { $this->title = $title; $this->time = $time; @@ -84,16 +90,20 @@ class News { switch($action) { case "add": - $n = new NewsEntry($vars["title"], DateTimeEdit::toTimestamp($vars, "time"), - $vars["category"], $vars["description"], $UID, $vars["icon"]); + $n = new NewsEntry($vars["title"], + DateTimeEdit::toTimestamp($vars, "time"), + $vars["category"], $vars["description"], + $UID, $vars["icon"]); echo "\"" .$n->title . "\" has now been added."; $this->add($n); $this->write(); break; case "preview": - $n = new NewsEntry($vars["title"], DateTimeEdit::toTimestamp($vars, "time"), - $vars["category"], $vars["description"], $UID, $vars["icon"]); + $n = new NewsEntry($vars["title"], + DateTimeEdit::toTimestamp($vars, "time"), + $vars["category"], $vars["description"], + $UID, $vars["icon"]); echo "
\n"; echo "
\n"; echo $n->show(); @@ -120,9 +130,11 @@ class News { $form = new Form("preview"); $form->addWidget(new LineEdit("Title", "title", $title)); $form->addWidget(new DateTimeEdit("Time", "time", $time)); - $form->addWidget(new ComboBox("Category", "category", $category, array("Main" => "main", "Site" => "site"))); + $form->addWidget(new ComboBox("Category", "category", $category, + array("Main" => "main", "Site" => "site"))); $form->addWidget(new TextEdit("Description", "description", $description)); - $form->addWidget(new ImageComboBox("Icon", "icon", $icon, new Icons($ICONS_DIR."/"))); + $form->addWidget(new ImageComboBox("Icon", "icon", $icon, + new Icons($ICONS_DIR."/"))); $form->addWidget(new Button("Post news")); $form->render(); break; @@ -146,7 +158,8 @@ class News { break; case "preview": - $n = new NewsEntry($vars["title"], DatetimeEdit::toTimestamp($vars, "time"), $vars["category"], $vars["description"], $UID, $vars["icon"]); + $n = new NewsEntry($vars["title"], DatetimeEdit::toTimestamp($vars, "time"), + $vars["category"], $vars["description"], $UID, $vars["icon"]); echo "
\n"; echo "
\n"; echo $n->show(); diff --git a/utils/modules/pages.php b/utils/modules/pages.php index 99c8142..d3ee0aa 100644 --- a/utils/modules/pages.php +++ b/utils/modules/pages.php @@ -1,6 +1,8 @@ run($params); - } else { - $modulestr = "

CMS ERROR: Could not find module: [[" . $modulecode . "]]

"; - } - - $str = str_replace("[[" . $modulecode . "]]", $modulestr, $str); - + $str = str_replace("[[" . $modulecode . "]]", runModule($modulecode), $str); } } -- cgit v1.2.3