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(
48 /* $m->setProperty("ID",
49 AOOSMODEL_TYPE_INTEGER,
50 AOOSMODEL_PROP_FROM_DATABASE|
51 AOOSMODEL_PROP_UNIQUE|
52 AOOSMODEL_PROP_DATA_INCREASING);*/
53 $m->setProperties("NAME",
54 AOOSMODEL_TYPE_STRING
,
55 AOOSMODEL_FLAG_FROM_DATABASE
);
56 $m->setProperties("DEPS",
57 AOOSMODEL_TYPE_STRING
,
58 AOOSMODEL_FLAG_FROM_DATABASE
);
59 $m->setProperties("INSTANCE",
60 AOOSMODEL_TYPE_UNKNOWN
);
63 $this->log("Created AOOSCore");
67 * Logs the string with a timestamp
68 * @param $str The string to log
70 public function log($str)
72 $this->_log
[] = array(
74 "TIMESTAMP" => microtime()
79 * Returns a string containing the log
81 public function log2str() {
83 foreach ($this->_log
as $num => $entry) {
84 $str .= "<strong>".$num.":</strong> ".$entry["STRING"]." at ".$entry["TIMESTAMP"]."<br />";
91 * @param $e The exception
93 public function setException($e)
95 $this->_exceptions
[$e->getName()] = $e;
96 if ($e->getEcode() == 0)
105 * Prints all exceptions
107 public function printExceptions()
109 $str = "<div class=\"Exceptions\">";
110 foreach ($this->_exceptions
as $e)
119 * Returns the value of the the given setting. Throws an AOOSException if the setting key isn't found.
120 * @param $name Name of the setting
121 * @param $module This is used to fetch per-module settings. If null is given a global setting is fetched.
124 public function getSetting($name, $module = null)
126 $a = $this->_settings
;
127 if ($module != null && $this->getModule($module))
131 if (!in_array($name, array_keys($a)))
133 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
140 * Used by AOOSModule to get translated string. SHOULD NOT BE ACCESSED MANUALLY! In order to translate a string use
141 * the AOOSModule::tr() function. Throws an AOOSException if the string isn't found.
142 * @param $string The string to translate
143 * @param $module This is used to fetch translated strings per-module. If a module name is given the translated
144 * string will be fetched from this module. If null is given, the string will be fetched from /lang.php
147 public function getTranslatedString($string, $module = null)
149 $a = $this->_transStrings
;
150 if ($module !== null && $this->getModule($module)) {
153 if (!in_array($string, array_keys($a))) {
159 public function tr($string) {
160 return $this->getTranslatedString($string);
164 * Loads $module. If the module couldn't be loaded an AOOSException is thrown. If the module is already loaded it is
165 * reloaded. If the module is successfully loaded the newly created instance is returned.
166 * @param $module Name of the module
170 public function loadModule($module) {
171 $dir = $this->getSetting("module_dir").$module."/";
173 throw new AOOSException($this, $this->getTranslatedString("unknown_module"));
175 $path = $dir.$module.$this->getSetting("extension");
176 if (!file_exists($path)) {
177 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"), "File error");
180 $deps = call_user_func(array($module, "dependencies"));
181 foreach ($deps as $dep) {
182 if (!$this->getModule($dep)) {
184 $this->loadModule($dep);
185 } catch (AOOSException
$e) {
186 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"));
191 $instance = new $module($this);
192 if (($dm = $instance->dataModelDefinition()) !== 0) {
193 $instance->setDataModel($dm);
195 $this->modules()->appendRow(array("NAME" => $module, "INSTANCE" => $instance));
196 $instance->setName($module);
198 $sf = $dir."settings".$this->getSetting("extension");
199 if (file_exists($sf)) {
201 $this->_settings
[$module] = $settings;
204 $lf = $dir."lang".$this->getSetting("extension");
205 if (file_exists($lf)) {
207 $this->_transStrings
[$module] = $lang;
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
220 public function getModule($module) {
221 $row = $this->modules()->find(array("NAME" => $module));
222 return $row && $row->INSTANCE ?
$row->INSTANCE
: $this->loadModule($module);
226 * Returns the AOOSModel containing all module names, dependencies and instances.
229 public function modules()
231 return $this->_modules
;
235 * Simply returs a string containing "AOOSCore".
236 * \internal This could be better
238 public function __toString()
244 * Connects the signal $signal of $sender to the slot $slot of $reciever
245 * @param $sender The sender
246 * @param $signal The signal name
247 * @param $reciever The reciever
248 * @param $slot The slot