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(
49 AOOSMODEL_TYPE_INTEGER
,
50 AOOSMODEL_PROP_FROM_DATABASE|
51 AOOSMODEL_PROP_UNIQUE|
52 AOOSMODEL_PROP_DATA_INCREASING
);
53 $m->setProperty("NAME",
54 AOOSMODEL_TYPE_STRING
,
55 AOOSMODEL_PROP_FROM_DATABASE
);
56 $m->setProperty("DEPS",
57 AOOSMODEL_TYPE_STRING
,
58 AOOSMODEL_PROP_FROM_DATABASE
);
59 $m->setProperty("INSTANCE",
60 AOOSMODEL_TYPE_UNKNOWN
,0);
61 $m->setTable($this->getSetting("module_table"));
62 $m->setSource($this->getSetting("default_storage_type"));
67 $this->log("Created AOOSCore");
71 * Logs the string with a timestamp
72 * @param $str The string to log
74 public function log($str)
76 $this->_log
[] = array(
78 "TIMESTAMP" => microtime()
83 * Returns a string containing the log
85 public function log2str() {
87 foreach ($this->_log
as $num => $entry) {
88 $str .= "<strong>".$num.":</strong> ".$entry["STRING"]." at ".$entry["TIMESTAMP"]."<br />";
95 * @param $e The exception
97 public function setException($e)
99 $this->_exceptions
[$e->getName()] = $e;
100 if ($e->getEcode() == 0)
109 * Prints all exceptions
111 public function printExceptions()
113 $str = "<div class=\"Exceptions\">";
114 foreach ($this->_exceptions
as $e)
123 * Returns the value of the the given setting. Throws an AOOSException if the setting key isn't found.
124 * @param $name Name of the setting
125 * @param $module This is used to fetch per-module settings. If null is given a global setting is fetched.
128 public function getSetting($name, $module = null)
130 $a = $this->_settings
;
131 if ($module != null && $this->getModule($module))
135 if (!in_array($name, array_keys($a)))
137 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
144 * Used by AOOSModule to get translated string. SHOULD NOT BE ACCESSED MANUALLY! In order to translate a string use
145 * the AOOSModule::tr() function. Throws an AOOSException if the string isn't found.
146 * @param $string The string to translate
147 * @param $module This is used to fetch translated strings per-module. If a module name is given the translated
148 * string will be fetched from this module. If null is given, the string will be fetched from /lang.php
151 public function getTranslatedString($string, $module = null)
153 $a = $this->_transStrings
;
154 if ($module !== null && $this->getModule($module)) {
157 if (!in_array($string, array_keys($a))) {
158 throw new AOOSLangException($this, $string);
164 public function tr($string) {
165 return $this->getTranslatedString($string);
169 * Loads $module. If the module couldn't be loaded an AOOSException is thrown. If the module is already loaded it is
170 * reloaded. If the module is successfully loaded the newly created instance is returned.
171 * @param $module Name of the module
175 public function loadModule($module) {
176 // $this->_modules->populate();
177 $m = $this->modules();
178 if (false === ($row = $m->find(array("NAME" => $module)))) {
179 throw new AOOSException($this, $this->getTranslatedString("unknown_module"), $module);
182 $deps = AOOSModel
::stripQuotes($row["DEPS"]);
184 foreach (explode(",",$deps) as $dep) {
185 if (!$this->getModule($dep)) {
187 $this->loadModule($dep);
189 catch (AOOSException
$e) {
190 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"));
197 $dir = $this->getSetting("module_dir").$module."/";
198 $path = $dir.$module.$this->getSetting("extension");
200 $i = new $module($this);
201 if (($dm = $i->dataModelDefinition()) !== 0) {
202 $i->setDataModel($dm);
203 $i->dataModel()->setSource($this->getSetting("default_storage_type"));
204 $i->dataModel()->setTable($module);
206 $m->setDataRowSingle($row, "INSTANCE", $i);
207 $i->setName($module);
208 $i->postInitialization();
210 $sf = $dir."settings".$this->getSetting("extension");
211 if (file_exists($sf)) {
213 $this->_settings
[$module] = $settings;
216 $lf = $dir."lang".$this->getSetting("extension");
217 if (file_exists($lf)) {
219 $this->_transStrings
[$module] = $lang;
225 * Returns the instance of the given $module. If this isn't instantiated yet, getModule will try to load it.
226 * an AOOSException if loadModule fails.
227 * @param $module Name of the module
228 * @return AOOSModule|false
231 public function getModule($module) {
232 $row = $this->modules()->find(array("NAME" => $module));
233 if ($row === false) {
236 return $row->INSTANCE ?
$row->INSTANCE
: $this->loadModule($module);
240 * Returns the AOOSModel containing all module names, dependencies and instances.
243 public function modules()
245 return $this->_modules
;
249 * Simply returs a string containing "AOOSCore".
250 * \internal This could be better
252 public function __toString()