Installer: minor fixes.
[AOOS.git] / AOOSCore.php
blob9fac15ae2d104c365b9267c32051c9f7a094091b
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->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);
61 $this->_modules = $m;
63 $this->log("Created AOOSCore");
66 /**
67 * Logs the string with a timestamp
68 * @param $str The string to log
70 public function log($str)
72 $this->_log[] = array(
73 "STRING" => $str,
74 "TIMESTAMP" => microtime()
78 /**
79 * Returns a string containing the log
81 public function log2str() {
82 $str = "";
83 foreach ($this->_log as $num => $entry) {
84 $str .= "<strong>".$num.":</strong> ".$entry["STRING"]." at ".$entry["TIMESTAMP"]."<br />";
86 return $str;
89 /**
90 * Sets an exception
91 * @param $e The exception
93 public function setException($e)
95 $this->_exceptions[$e->getName()] = $e;
96 if ($e->getEcode() == 0)
98 exit($e);
101 return true;
105 * Prints all exceptions
107 public function printExceptions()
109 $str = "<div class=\"Exceptions\">";
110 foreach ($this->_exceptions as $e)
112 $str .= $e;
114 $str .= "</div>";
115 print $str;
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.
122 * @return string
124 public function getSetting($name, $module = null)
126 $a = $this->_settings;
127 if ($module != null && $this->getModule($module))
129 $a = $a[$module];
131 if (!in_array($name, array_keys($a)))
133 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
134 return false;
136 return $a[$name];
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
145 * @return string
147 public function getTranslatedString($string, $module = null)
149 $a = $this->_transStrings;
150 if ($module !== null && $this->getModule($module)) {
151 $a = $a[$module];
153 if (!in_array($string, array_keys($a))) {
154 return $string;
156 return $a[$string];
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
167 * @return AOOSModule
168 * @sa getModule
170 public function loadModule($module) {
171 $dir = $this->getSetting("module_dir").$module."/";
172 if (!is_dir($dir)) {
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");
179 require_once($path);
180 $deps = call_user_func(array($module, "dependencies"));
181 foreach ($deps as $dep) {
182 if (!$this->getModule($dep)) {
183 try {
184 $this->loadModule($dep);
185 } catch (AOOSException $e) {
186 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"));
187 return false;
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)) {
200 require($sf);
201 $this->_settings[$module] = $settings;
204 $lf = $dir."lang".$this->getSetting("extension");
205 if (file_exists($lf)) {
206 require($lf);
207 $this->_transStrings[$module] = $lang;
210 return $instance;
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 return $row && $row->INSTANCE ? $row->INSTANCE : $this->loadModule($module);
226 * Returns the AOOSModel containing all module names, dependencies and instances.
227 * @return AOOSModel
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()
240 return "AOOSCore";
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
252 // vim: number