Typo
[AOOS.git] / AOOSCore.php
blob3f95a7c5eee8f7d78904b20ae014bc564d3a19a8
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() {
24 // Settings
25 require("settings.php");
26 $this->_settings = $settings; /* Set in settings.php */
28 // Translated strings
29 require("lang.php");
30 $l = $this->getSetting("lang");
31 if (in_array($l, array_keys($lang))) /* $lang set in lang.php */
32 $this->_transStrings = $lang[$l];
35 $m = new AOOSModel($this);
36 $m->setColumnIndex(array(
37 "NAME",
38 "INSTANCE"
39 ));
40 $m->setProperties("NAME", AOOSMODEL_TYPE_STRING);
41 $m->setProperties("INSTANCE", AOOSMODEL_TYPE_UNKNOWN);
42 $this->_modules = $m;
44 $this->log("Created AOOSCore");
47 /**
48 * Logs the string with a timestamp
49 * @param $str The string to log
51 public function log($str)
53 $this->_log[] = array(
54 "STRING" => $str,
55 "TIMESTAMP" => microtime()
59 /**
60 * Returns a string containing the log
62 public function log2str() {
63 $str = "";
64 foreach ($this->_log as $num => $entry) {
65 $str .= "<strong>".$num.":</strong> ".$entry["STRING"]." at ".$entry["TIMESTAMP"]."<br />";
67 return $str;
70 /**
71 * Sets an exception
72 * @param $e The exception
74 public function setException($e)
76 $this->_exceptions[$e->getName()] = $e;
77 if ($e->getEcode() == 0)
79 exit($e);
82 return true;
85 /**
86 * Prints all exceptions
88 public function printExceptions()
90 $str = "<div class=\"Exceptions\">";
91 foreach ($this->_exceptions as $e)
93 $str .= $e;
95 $str .= "</div>";
96 print $str;
99 /**
100 * Returns the value of the the given setting. Throws an AOOSException if the setting key isn't found.
101 * @param $name Name of the setting
102 * @param $module This is used to fetch per-module settings. If null is given a global setting is fetched.
103 * @return string
105 public function getSetting($name, $module = null)
107 $a = $this->_settings;
108 if ($module != null) {
109 $a = $a[$module];
111 if (!in_array($name, array_keys($a))) {
112 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
114 return $a[$name];
118 * Used by AOOSModule to get translated string. SHOULD NOT BE ACCESSED MANUALLY! In order to translate a string use
119 * the AOOSModule::tr() function. Throws an AOOSException if the string isn't found.
120 * @param $string The string to translate
121 * @param $module This is used to fetch translated strings per-module. If a module name is given the translated
122 * string will be fetched from this module. If null is given, the string will be fetched from /lang.php
123 * @return string
125 public function getTranslatedString($string, $module = null)
127 $a = $this->_transStrings;
128 if ($module !== null) {
129 $a = $a[$module];
131 if (!in_array($string, array_keys($a))) {
132 return $string;
134 return $a[$string];
137 public function tr($string) {
138 return $this->getTranslatedString($string);
142 * Loads $module. If the module couldn't be loaded an AOOSException is thrown. If the module is already loaded it is
143 * reloaded. If the module is successfully loaded the newly created instance is returned.
144 * @param $module Name of the module
145 * @return AOOSModule
146 * @sa getModule
148 public function loadModule($module) {
149 // Is module dir existing?
150 $dir = $this->getSetting("module_dir").$module."/";
151 if (!is_dir($dir)) {
152 throw new AOOSException($this, $this->getTranslatedString("unknown_module"));
154 // Is module file existing?
155 $path = $dir.$module.$this->getSetting("extension");
156 if (!file_exists($path)) {
157 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"), "File error");
159 require_once($path);
161 // Check dependencies
162 $deps = call_user_func(array($module, "dependencies"));
163 foreach ($deps as $dep) {
164 if (!$this->getModule($dep)) {
165 try {
166 $this->loadModule($dep);
167 } catch (AOOSException $e) {
168 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"));
169 return false;
174 // Load module settings
175 $sf = $dir."settings".$this->getSetting("extension");
176 if (file_exists($sf)) {
177 require($sf);
178 $this->_settings[$module] = $settings;
181 // Load module strings
182 $lf = $dir."lang".$this->getSetting("extension");
183 if (file_exists($lf)) {
184 require($lf);
185 $this->_transStrings[$module] = $lang;
188 // Create module instance
189 $instance = new $module($this);
190 if (($dm = $instance->dataModelDefinition()) !== 0) {
191 $instance->setDataModel($dm);
193 $this->modules()->appendRow(array("NAME" => $module, "INSTANCE" => $instance));
194 $instance->setName($module);
196 // Classes and files used in module
197 self::register($module, $path);
198 foreach ($instance->files() as $class => $path) {
199 self::register($class, $dir.$path);
202 return $instance;
206 * Returns the instance of the given $module. If this isn't instantiated yet, getModule will try to load it.
207 * an AOOSException if loadModule fails.
208 * @param $module Name of the module
209 * @return AOOSModule|false
210 * @sa loadModule
212 public function getModule($module) {
213 $row = $this->modules()->find(array("NAME" => $module));
214 return $row && $row->INSTANCE ? $row->INSTANCE : $this->loadModule($module);
218 * Registers a path to a certain class
219 * @param $class Class name
220 * @param $path The path relative to the root directory
222 static public function register($class, $path) {
223 $_SESSION["files"][$path] = $class; // Use paths as keys so that we can handle similarly named classes from different modules
224 return true;
228 * Returns the AOOSModel containing all module names, dependencies and instances.
229 * @return AOOSModel
231 public function modules()
233 return $this->_modules;
237 * Simply returs a string containing "AOOSCore".
238 * \internal This could be better
240 public function __toString() {
241 return "AOOSCore";
244 // vim: number