Deps needed to be stripped before exploding
[AOOS.git] / AOOSCore.php
blob7ce0d911793691cdc8895a3b0b29be1ab22f7005
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 "NAME",
44 "DEPS",
45 "INSTANCE"
46 ));
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"));
57 $m->populate();
58 // $m->populate();
59 $this->_modules = $m;
61 $this->log("Created AOOSCore");
64 /**
65 * Logs the string with a timestamp
66 * @param $str The string to log
68 public function log($str)
70 $this->_log[] = array(
71 "STRING" => $str,
72 "TIMESTAMP" => microtime()
76 /**
77 * Returns a string containing the log
79 public function log2str() {
80 $str = "";
81 foreach ($this->_log as $num => $entry) {
82 $str .= "<strong>".$num.":</strong> ".$entry["STRING"]." at ".$entry["TIMESTAMP"]."<br />";
84 return $str;
87 /**
88 * Sets an exception
89 * @param $e The exception
91 public function setException($e)
93 $this->_exceptions[$e->getName()] = $e;
94 if ($e->getEcode() == 0)
96 exit($e);
99 return true;
103 * Prints all exceptions
105 public function printExceptions()
107 $str = "<div class=\"Exceptions\">";
108 foreach ($this->_exceptions as $e)
110 $str .= $e;
112 $str .= "</div>";
113 print $str;
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.
120 * @return string
122 public function getSetting($name, $module = null)
124 $a = $this->_settings;
125 if ($module != null && $this->getModule($module))
127 $a = $a[$module];
129 if (!in_array($name, array_keys($a)))
131 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
132 return false;
134 return $a[$name];
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
143 * @return string
145 public function getTranslatedString($string, $module = null)
147 $a = $this->_transStrings;
148 if ($module !== null && $this->getModule($module)) {
149 $a = $a[$module];
151 if (!in_array($string, array_keys($a))) {
152 throw new AOOSLangException($this, $string);
153 return $string;
155 return $a[$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
166 * @return AOOSModule
167 * @sa getModule
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);
174 return false;
176 $deps = AOOSModel::stripQuotes($row["DEPS"]);
177 if ($deps != "") {
178 foreach (explode(",",$deps) as $dep) {
179 if (!$this->getModule($dep)) {
180 try {
181 $this->loadModule($dep);
183 catch (AOOSException $e) {
184 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"));
185 return false;
191 $dir = $this->getSetting("module_dir").$module."/";
192 $path = $dir.$module.$this->getSetting("extension");
193 require_once($path);
194 $i = new $module($this);
195 if (($dm = $i->dataModelDefinition()) !== 0) {
196 $i->setDataModel($dm);
197 $i->dataModel()->setSource($this->getSetting("default_storage_type"));
198 $i->dataModel()->setTable($module);
200 $m->setDataRowSingle($row, "INSTANCE", $i);
201 $i->setName($module);
202 $i->postInitialization();
204 $sf = $dir."settings".$this->getSetting("extension");
205 if (file_exists($sf)) {
206 require($sf);
207 $this->_settings[$module] = $settings;
210 $lf = $dir."lang".$this->getSetting("extension");
211 if (file_exists($lf)) {
212 require($lf);
213 $this->_transStrings[$module] = $lang;
215 return $i;
219 * Returns the instance of the given $module. If this isn't instantiated yet, getModule will try to load it.
220 * an AOOSException if loadModule fails.
221 * @param $module Name of the module
222 * @return AOOSModule|false
223 * @sa loadModule
225 public function getModule($module) {
226 $row = $this->modules()->find(array("NAME" => $module));
227 if ($row === false) {
228 return false;
230 return $row->INSTANCE ? $row->INSTANCE : $this->loadModule($module);
234 * Returns the AOOSModel containing all module names, dependencies and instances.
235 * @return AOOSModel
237 public function modules()
239 return $this->_modules;
243 * Simply returs a string containing "AOOSCore".
244 * \internal This could be better
246 public function __toString()
248 return "AOOSCore";
252 // vim: number