6 * The core class of AOOS
8 * @author Sebastian Skejø
13 private $_exceptions = array();
14 private $_modules = null;
15 private $_settings = array();
16 private $_transStrings = array();
17 private $_log = array();
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()
26 require("settings.php");
27 $this->_settings
= $settings; /* Set in settings.php */
28 $l = $this->getSetting("lang");
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");
38 $this->_transStrings
= $lang[$l];
41 $m = new AOOSModel($this);
42 $m->setColumnIndex(array(
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"));
61 $this->log("Created AOOSCore");
65 * Logs the string with a timestamp
66 * @param $str The string to log
68 public function log($str)
70 $this->_log
[] = array(
72 "TIMESTAMP" => microtime()
77 * Returns a string containing the log
79 public function log2str() {
81 foreach ($this->_log
as $num => $entry) {
82 $str .= "<strong>".$num.":</strong> ".$entry["STRING"]." at ".$entry["TIMESTAMP"]."<br />";
89 * @param $e The exception
91 public function setException($e)
93 $this->_exceptions
[$e->getName()] = $e;
94 if ($e->getEcode() == 0)
103 * Prints all exceptions
105 public function printExceptions()
107 $str = "<div class=\"Exceptions\">";
108 foreach ($this->_exceptions
as $e)
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.
122 public function getSetting($name, $module = null)
124 $a = $this->_settings
;
125 if ($module != null && $this->getModule($module))
129 if (!in_array($name, array_keys($a)))
131 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
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
145 public function getTranslatedString($string, $module = null)
147 $a = $this->_transStrings
;
148 if ($module !== null && $this->getModule($module)) {
151 if (!in_array($string, array_keys($a))) {
152 throw new AOOSLangException($this, $string);
158 public function tr($string) {
159 return $this->getTranslatedString($string);
163 * Loads $module. If the module couldn't be loaded an AOOSException is thrown. If the module is already loaded it is
164 * reloaded. If the module is successfully loaded the newly created instance is returned.
165 * @param $module Name of the module
169 public function loadModule($module) {
170 // $this->_modules->populate();
171 $m = $this->modules();
172 if (false === ($row = $m->find(array("NAME" => $module)))) {
173 throw new AOOSException($this, $this->getTranslatedString("unknown_module"), $module);
176 if ($row["DEPS"] != "") {
177 foreach (explode(",",$row["DEPS"]) as $dep) {
178 if (!$this->getModule($dep)) {
180 $this->loadModule($dep);
182 catch (AOOSException
$e) {
183 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"));
190 $dir = $this->getSetting("module_dir").$module."/";
191 $path = $dir.$module.$this->getSetting("extension");
193 $i = new $module($this);
194 if (($dm = $i->dataModelDefinition()) !== 0) {
195 $i->setDataModel($dm);
196 $i->dataModel()->setSource($this->getSetting("default_storage_type"));
197 $i->dataModel()->setTable($module);
199 $m->setDataRowSingle($row, "INSTANCE", $i);
200 $i->setName($module);
201 $i->postInitialization();
203 $sf = $dir."settings".$this->getSetting("extension");
204 if (file_exists($sf)) {
206 $this->_settings
[$module] = $settings;
209 $lf = $dir."lang".$this->getSetting("extension");
210 if (file_exists($lf)) {
212 $this->_transStrings
[$module] = $lang;
218 * Returns the instance of the given $module. If this isn't instantiated yet, getModule will try to load it.
219 * an AOOSException if loadModule fails.
220 * @param $module Name of the module
221 * @return AOOSModule|false
224 public function getModule($module) {
225 $row = $this->modules()->find(array("NAME" => $module));
226 if ($row === false) {
229 return $row->INSTANCE ?
$row->INSTANCE
: $this->loadModule($module);
233 * Returns the AOOSModel containing all module names, dependencies and instances.
236 public function modules()
238 return $this->_modules
;
242 * Simply returs a string containing "AOOSCore".
243 * \internal This could be better
245 public function __toString()