AOOSException.php: Proper check of file and line
[AOOS.git] / AOOSCore.php
blobf4bdeb3bdb848ffb143c60cecf5a1f3d89cee71c
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 "ID",
44 "NAME",
45 "DEPS",
46 "INSTANCE"
47 ));
48 $m->setProperty("ID",
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"));
63 $m->populate();
64 // $m->populate();
65 $this->_modules = $m;
67 $this->log("Created AOOSCore");
70 /**
71 * Logs the string with a timestamp
72 * @param $str The string to log
74 public function log($str)
76 $this->_log[] = array(
77 "STRING" => $str,
78 "TIMESTAMP" => microtime()
82 /**
83 * Returns a string containing the log
85 public function log2str() {
86 $str = "";
87 foreach ($this->_log as $num => $entry) {
88 $str .= "<strong>".$num.":</strong> ".$entry["STRING"]." at ".$entry["TIMESTAMP"]."<br />";
90 return $str;
93 /**
94 * Sets an exception
95 * @param $e The exception
97 public function setException($e)
99 $this->_exceptions[$e->getName()] = $e;
100 if ($e->getEcode() == 0)
102 exit($e);
105 return true;
109 * Prints all exceptions
111 public function printExceptions()
113 $str = "<div class=\"Exceptions\">";
114 foreach ($this->_exceptions as $e)
116 $str .= $e;
118 $str .= "</div>";
119 print $str;
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.
126 * @return string
128 public function getSetting($name, $module = null)
130 $a = $this->_settings;
131 if ($module != null && $this->getModule($module))
133 $a = $a[$module];
135 if (!in_array($name, array_keys($a)))
137 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
138 return false;
140 return $a[$name];
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
149 * @return string
151 public function getTranslatedString($string, $module = null)
153 $a = $this->_transStrings;
154 if ($module !== null && $this->getModule($module)) {
155 $a = $a[$module];
157 if (!in_array($string, array_keys($a))) {
158 throw new AOOSLangException($this, $string);
159 return $string;
161 return $a[$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
172 * @return AOOSModule
173 * @sa getModule
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);
180 return false;
182 $deps = AOOSModel::stripQuotes($row["DEPS"]);
183 if ($deps != "") {
184 foreach (explode(",",$deps) as $dep) {
185 if (!$this->getModule($dep)) {
186 try {
187 $this->loadModule($dep);
189 catch (AOOSException $e) {
190 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"));
191 return false;
197 $dir = $this->getSetting("module_dir").$module."/";
198 $path = $dir.$module.$this->getSetting("extension");
199 require_once($path);
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);
209 $sf = $dir."settings".$this->getSetting("extension");
210 if (file_exists($sf)) {
211 require($sf);
212 $this->_settings[$module] = $settings;
215 $lf = $dir."lang".$this->getSetting("extension");
216 if (file_exists($lf)) {
217 require($lf);
218 $this->_transStrings[$module] = $lang;
221 $i->postInitialization();
222 return $i;
226 * Returns the instance of the given $module. If this isn't instantiated yet, getModule will try to load it.
227 * an AOOSException if loadModule fails.
228 * @param $module Name of the module
229 * @return AOOSModule|false
230 * @sa loadModule
232 public function getModule($module) {
233 $row = $this->modules()->find(array("NAME" => $module));
234 if ($row === false) {
235 return false;
237 return $row->INSTANCE ? $row->INSTANCE : $this->loadModule($module);
241 * Returns the AOOSModel containing all module names, dependencies and instances.
242 * @return AOOSModel
244 public function modules()
246 return $this->_modules;
250 * Simply returs a string containing "AOOSCore".
251 * \internal This could be better
253 public function __toString()
255 return "AOOSCore";
259 * Connects the signal $signal of $sender to the slot $slot of $reciever
260 * @param $sender The sender
261 * @param $signal The signal name
262 * @param $reciever The reciever
263 * @param $slot The slot
267 // vim: number