Defined __sleep and __wakeup
[AOOS.git] / AOOSCore.php
blob490ab7370f55d1f2d04bf4ed3a531928ecae06a2
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 = new AOOSStorageDevice($this);
47 $s->setStorageType("mysql");
48 $s->setTable("modules");
49 $this->_modules = $s->selectModel("*");
50 $this->_modules->setEscape(false);
52 $this->log("Created AOOSCore");
55 public function __sleep() {
56 print __function__."() called!<br />";
57 $_SESSION["modules"] = serialize($this->_modules);
58 $this->_modules = null;
59 return array("_exceptions", "_modules", "_settings");
62 public function __wakeup() {
63 $this->_modules = unserialize($_SESSION["modules"]);
64 $_SESSION["modules"] = false;
67 public function log($str)
69 $this->_log[] = array(
70 "STRING" => $str,
71 "TIMESTAMP" => microtime()
75 /**
76 * Sets an exception
77 * @param AOOSException $e The exception
79 public function setException($e)
81 $this->_exceptions[$e->getName()] = $e;
82 if ($e->getEcode() == 0)
84 exit($e);
87 return true;
90 public function printExceptions()
92 $str = "<div class=\"Exceptions\">";
93 foreach ($this->_exceptions as $e)
95 $str .= $e;
97 $str .= "</div>";
98 print $str;
101 public function getSetting($name, $module = "no")
103 $a = $this->_settings;
104 if ($module != "no" && $this->isLoaded($module))
106 $a = $a[$module];
108 if (!in_array($name, array_keys($a)))
110 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
111 return false;
113 return $a[$name];
116 public function getTranslatedString($string, $module = "no")
118 $a = $this->_transStrings;
119 if ($module != "no" && $this->isLoaded($module))
121 $a = $a[$module];
123 if (!in_array($string, array_keys($a)))
125 throw new AOOSException($this, "Translated string not found", "String name: ".$string, true, 2);
126 return $string;
128 return $a[$string];
131 public function modules()
133 return $this->_modules;
136 public function loadModule($name)
138 $this->log("Loading ".$name);
139 $m = $this->modules();
140 if (($r = $m->getRowFromValue("name", $name)) === false)
142 $this->log("Failed!");
143 throw new AOOSException($this, $this->getTranslatedString("module_not_loadable"), "", true, 1);
144 return false;
147 $row = $m->getRow($r);
148 $dir = $this->getSetting("module_dir")."/".$row["name"]."/";
149 $filename = $row["name"].$this->getSetting("extension");
151 $deps = explode(",", $row["deps"]);
152 if (!empty($deps[0]))
154 foreach($deps as $dep)
156 if (!$this->isLoaded($dep))
158 try
160 $this->loadModule($dep);
162 catch(AOOSException $e)
164 throw new AOOSException($this, $this->getTranslatedString("deps_not_loadable"), $row["name"], true, 1);
165 return false;
171 if ($row["instance"])
173 $this->log("Done!");
174 return $row["instance"];
176 else {
177 require_once($dir.$filename);
178 if (file_exists($dir."settings".$this->getSetting("extension")))
180 require_once($dir."settings".$this->getSetting("extension"));
181 $this->_settings[$row["name"]] = $settings;
183 if (file_exists($dir."lang".$this->getSetting("extension")))
185 require_once($dir."lang".$this->getSetting("extension"));
186 $this->_transStrings[$row["name"]] = $lang;
188 $i = new $row["name"]($this);
189 $m->setData($i, $r, "instance");
190 $this->log("Done!");
191 return $i;
196 * Returns true if $module is loaded, otherwise return false.
197 * @param string $name Module name
198 * @return bool
200 public function isLoaded($name)
202 $m = $this->modules();
203 if (($row = $m->getRowFromValue("name", $name)) === false)
205 // TODO: throw exception
206 return false;
209 return true;
213 * Returns an instance to the given $module
214 * @param string $module The name of the module
215 * @return false|AOOSModule
217 public function getModule($module)
219 $m = $this->modules();
220 if (($row = $m->getRowFromValue("name", $module)) === false)
222 throw new AOOSException($this, $this->getTranslatedString("module_not_found"), "", true, 1);
223 return false;
225 return $this->loadModule($module);
228 public function __toString()
230 return "AOOSCore";
234 public function newStorageDevice() {
235 $obj = new AOOSStorageDevice($this);
236 $obj->setType($this->getSetting("default_storage_type"));
237 return $obj;