Not loaded class files can be in lib/ too
[AOOS.git] / AOOSCore.php
blob2c89c5fdade4ff2a3db3af1807fce2ed8fb8837c
1 <?php
3 require_once("AOOSException.php");
4 require_once("AOOSModel.php");
5 require_once("AOOSStorageDevice.php");
8 /**
9 * AOOSCore
11 * The core class of AOOS
13 * @author Sebastian Skejø
16 class AOOSCore
18 private $_exceptions = array();
19 private $_modules = null;
20 private $_settings = array();
21 private $_transStrings = array();
22 private $_log = array();
24 /**
25 * During constructing a model is built with module names and properties
27 public function __construct()
29 // Settings
30 require_once("config.php");
31 $this->_settings = $settings; /* Set in config.php */
32 $l = $this->getSetting("lang");
34 // Translated strings
35 require_once("lang.php");
36 if (!in_array($l, array_keys($lang))) /* $lang set in lang.php */
38 throw new AOOSException($this, "Language not found in lang.php. You will not be able to translate strings","",true, 1);
40 else
42 $this->_transStrings = $lang[$l];
45 // Let's load modules
46 $s = $this->newStorageDevice();
47 $s->setTable("modules");
48 $this->_modules = $s->selectModel("*");
49 $this->_modules->setEscape(false);
51 $this->log("Created AOOSCore");
54 public function log($str)
56 $this->_log[] = array(
57 "STRING" => $str,
58 "TIMESTAMP" => microtime()
62 /**
63 * Sets an exception
64 * @param AOOSException $e The exception
66 public function setException($e)
68 $this->_exceptions[$e->getName()] = $e;
69 if ($e->getEcode() == 0)
71 exit($e);
74 return true;
77 public function printExceptions()
79 $str = "<div class=\"Exceptions\">";
80 foreach ($this->_exceptions as $e)
82 $str .= $e;
84 $str .= "</div>";
85 print $str;
88 public function getSetting($name, $module = "no")
90 $a = $this->_settings;
91 if ($module != "no" && $this->isLoaded($module))
93 $a = $a[$module];
95 if (!in_array($name, array_keys($a)))
97 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
98 return false;
100 return $a[$name];
103 public function getTranslatedString($string, $module = "no")
105 $a = $this->_transStrings;
106 if ($module != "no" && $this->isLoaded($module))
108 $a = $a[$module];
110 if (!in_array($string, array_keys($a)))
112 throw new AOOSException($this, "Translated string not found", "String name: ".$string, true, 2);
113 return $string;
115 return $a[$string];
118 public function modules()
120 return $this->_modules;
123 public function loadModule($name)
125 $this->log("Loading ".$name);
126 $m = $this->modules();
127 if (($r = $m->getRowFromValue("name", $name)) === false)
129 $this->log("Failed!");
130 throw new AOOSException($this, $this->getTranslatedString("module_not_loadable"), "", true, 1);
131 return false;
134 $row = $m->getRow($r);
135 $dir = $this->getSetting("module_dir")."/".$row["name"]."/";
136 $filename = $row["name"].$this->getSetting("extension");
138 $deps = explode(",", $row["deps"]);
139 if (!empty($deps[0]))
141 foreach($deps as $dep)
143 if (!$this->isLoaded($dep))
145 try
147 $this->loadModule($dep);
149 catch(AOOSException $e)
151 throw new AOOSException($this, $this->getTranslatedString("deps_not_loadable"), $row["name"], true, 1);
152 return false;
158 if ($row["instance"])
160 $this->log("Done!");
161 return $row["instance"];
163 else {
164 require_once($dir.$filename);
165 if (file_exists($dir."settings".$this->getSetting("extension")))
167 require_once($dir."settings".$this->getSetting("extension"));
168 $this->_settings[$row["name"]] = $settings;
170 if (file_exists($dir."lang".$this->getSetting("extension")))
172 require_once($dir."lang".$this->getSetting("extension"));
173 $this->_transStrings[$row["name"]] = $lang;
175 $i = new $row["name"]($this);
176 $m->setData($i, $r, "instance");
177 $this->log("Done!");
178 return $i;
183 * Returns true if $module is loaded, otherwise return false.
184 * @param string $name Module name
185 * @return bool
187 public function isLoaded($name)
189 $m = $this->modules();
190 if (($row = $m->getRowFromValue("name", $name)) === false)
192 // TODO: throw exception
193 return false;
196 return true;
200 * Returns an instance to the given $module
201 * @param string $module The name of the module
202 * @return false|AOOSModule
204 public function getModule($module)
206 $m = $this->modules();
207 if (($row = $m->getRowFromValue("name", $module)) === false)
209 throw new AOOSException($this, $this->getTranslatedString("module_not_found"), "", true, 1);
210 return false;
212 return $this->loadModule($module);
215 public function __toString()
217 return "AOOSCore";
221 public function newStorageDevice() {
222 $obj = new AOOSStorageDevice($this);
223 $obj->setStorageType($this->getSetting("default_storage_type"));
224 return $obj;