index.php
[AOOS.git] / AOOSCore.php
blob7af1828d0e9ce2b79e3d7087214bf36c8c18d375
1 <?php
3 /**
4 * AOOSCore
6 * The core class of AOOS
8 * @author Sebastian Skejø
9 */
11 class AOOSCore
13 private $_exceptions = array();
14 private $_modules = null;
15 private $_settings = array();
16 private $_transStrings = array();
17 private $_log = array();
19 /**
20 * Default constructor. An AOOSException is thrown if the language defined in $settings["lang"] isn't found the the
21 * array keys of the translated strings defined in lang.php.
23 public function __construct()
25 // Settings
26 require("settings.php");
27 $this->_settings = $settings; /* Set in settings.php */
28 $l = $this->getSetting("lang");
30 // Translated strings
31 require("lang.php");
32 if (!in_array($l, array_keys($lang))) /* $lang set in lang.php */
34 throw new AOOSException($this, "Language not found in lang.php. You will not be able to translate strings");
36 else
38 $this->_transStrings = $lang[$l];
41 $m = new AOOSModel($this);
42 $m->setColumnIndex(array(
43 "NAME",
44 "DEPS",
45 "INSTANCE"
46 ));
47 $m->setProperty("NAME",
48 AOOSMODEL_TYPE_STRING,
49 AOOSMODEL_PROP_FROM_DATABASE);
50 $m->setProperty("DEPS",
51 AOOSMODEL_TYPE_STRING,
52 AOOSMODEL_PROP_FROM_DATABASE);
53 $m->setProperty("INSTANCE",
54 AOOSMODEL_TYPE_UNKNOWN,0);
55 $m->setTable($this->getSetting("module_table"));
56 $m->setSource($this->getSetting("default_storage_type"));
57 $m->populate();
58 // $m->populate();
59 $this->_modules = $m;
61 $this->log("Created AOOSCore");
64 /**
65 * Logs the string with a timestamp
66 * @param $str The string to log
68 public function log($str)
70 $this->_log[] = array(
71 "STRING" => $str,
72 "TIMESTAMP" => microtime()
76 /**
77 * Returns a string containing the log
79 public function log2str() {
80 $str = "";
81 foreach ($this->_log as $num => $entry) {
82 $str .= "<strong>".$num.":</strong> ".$entry["STRING"]." at ".$entry["TIMESTAMP"]."<br />";
84 return $str;
87 /**
88 * Sets an exception
89 * @param $e The exception
91 public function setException($e)
93 $this->_exceptions[$e->getName()] = $e;
94 if ($e->getEcode() == 0)
96 exit($e);
99 return true;
103 * Prints all exceptions
105 public function printExceptions()
107 $str = "<div class=\"Exceptions\">";
108 foreach ($this->_exceptions as $e)
110 $str .= $e;
112 $str .= "</div>";
113 print $str;
117 * Returns the value of the the given setting. Throws an AOOSException if the setting key isn't found.
118 * @param $name Name of the setting
119 * @param $module This is used to fetch per-module settings. If null is given a global setting is fetched.
120 * @return string
122 public function getSetting($name, $module = null)
124 $a = $this->_settings;
125 if ($module != null && $this->getModule($module))
127 $a = $a[$module];
129 if (!in_array($name, array_keys($a)))
131 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
132 return false;
134 return $a[$name];
138 * Used by AOOSModule to get translated string. SHOULD NOT BE ACCESSED MANUALLY! In order to translate a string use
139 * the AOOSModule::tr() function. Throws an AOOSException if the string isn't found.
140 * @param $string The string to translate
141 * @param $module This is used to fetch translated strings per-module. If a module name is given the translated
142 * string will be fetched from this module. If null is given, the string will be fetched from /lang.php
143 * @return string
145 public function getTranslatedString($string, $module = null)
147 $a = $this->_transStrings;
148 if ($module != null && $this->getModule($module)) {
149 $a = $a[$module];
151 if (!in_array($string, array_keys($a))) {
152 throw new AOOSLangException($this, $string);
153 return $string;
155 return $a[$string];
159 * Loads $module. If the module couldn't be loaded an AOOSException is thrown. If the module is already loaded it is
160 * reloaded. If the module is loaded the newly created instance is returned.
161 * @param $module Name of the module
162 * @return AOOSModule
163 * @sa getModule
165 public function loadModule($module) {
166 $this->_modules->populate();
167 $m = $this->modules();
168 if (false === ($row = $m->find(array("NAME" => $module)))) {
169 throw new AOOSException($this, $this->getTranslatedString("unknown_module"), $module);
170 return false;
172 $info = $m->getRow($row);
173 if ($info["DEPS"] != "") {
174 foreach (explode(",",$info["DEPS"]) as $dep) {
175 if (!$this->getModule($dep)) {
176 try {
177 $this->loadModule($dep);
179 catch (AOOSException $e) {
180 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"));
181 return false;
187 $dir = $this->getSetting("module_dir").$module."/";
188 $path = $dir.$module.$this->getSetting("extension");
189 require_once($path);
190 $i = new $module($this);
191 if (($dm = $i->dataModelDefinition()) !== 0) {
192 $i->setDataModel($dm);
193 $i->dataModel()->setSource($this->getSetting("default_storage_type"));
194 $i->dataModel()->setTable($module);
196 $m->setData($row, "INSTANCE", $i);
197 $i->postInitialization();
199 $sf = $dir."settings".$this->getSetting("extension");
200 if (file_exists($sf)) {
201 require($sf);
202 $this->_settings[$module] = $settings;
205 $lf = $dir."lang".$this->getSetting("extension");
206 if (file_exists($lf)) {
207 require($lf);
208 $this->_transStrings[$module] = $lang;
210 return $i;
214 * Returns the instance of the given $module. If this isn't instantiated yet, getModule will try to load it.
215 * an AOOSException if loadModule fails.
216 * @param $module Name of the module
217 * @return AOOSModule|false
218 * @sa loadModule
220 public function getModule($module) {
221 $row = $this->modules()->find(array("NAME" => $module));
222 if ($row === false) {
223 return false;
225 $i = $this->modules()->getColumn("INSTANCE", $row, 1);
226 return is_object($i[0]) ? $i[0] : $this->loadModule($module);
230 * Returns the AOOSModel containing all module names, dependencies and instances.
231 * @return AOOSModel
233 public function modules()
235 return $this->_modules;
239 * Simply returs a string containing "AOOSCore".
240 * \internal This could be better
242 public function __toString()
244 return "AOOSCore";
247 // vim: number