6 * The core class of AOOS
8 * @author Sebastian Skejø
13 private $_exceptions = array();
14 private $_modules = null;
15 private $_settings = array();
16 private $_transStrings = array();
17 private $_log = array();
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 require("settings.php");
26 $this->_settings
= $settings; /* Set in settings.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(
40 $m->setProperties("NAME", AOOSMODEL_TYPE_STRING
);
41 $m->setProperties("INSTANCE", AOOSMODEL_TYPE_UNKNOWN
);
44 $this->log("Created AOOSCore");
48 * Logs the string with a timestamp
49 * @param $str The string to log
51 public function log($str)
53 $this->_log
[] = array(
55 "TIMESTAMP" => microtime()
60 * Returns a string containing the log
62 public function log2str() {
64 foreach ($this->_log
as $num => $entry) {
65 $str .= "<strong>".$num.":</strong> ".$entry["STRING"]." at ".$entry["TIMESTAMP"]."<br />";
72 * @param $e The exception
74 public function setException($e)
76 $this->_exceptions
[$e->getName()] = $e;
77 if ($e->getEcode() == 0)
86 * Prints all exceptions
88 public function printExceptions()
90 $str = "<div class=\"Exceptions\">";
91 foreach ($this->_exceptions
as $e)
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.
105 public function getSetting($name, $module = null)
107 $a = $this->_settings
;
108 if ($module != null) {
111 if (!in_array($name, array_keys($a))) {
112 throw new AOOSException($this, "Settingkey not found", "Key: ".$name, true, 2);
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
125 public function getTranslatedString($string, $module = null)
127 $a = $this->_transStrings
;
128 if ($module !== null) {
131 if (!in_array($string, array_keys($a))) {
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
148 public function loadModule($module) {
149 // Is module dir existing?
150 $dir = $this->getSetting("module_dir").$module."/";
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");
161 // Check dependencies
162 $deps = call_user_func(array($module, "dependencies"));
163 foreach ($deps as $dep) {
164 if (!$this->getModule($dep)) {
166 $this->loadModule($dep);
167 } catch (AOOSException
$e) {
168 throw new AOOSException($this, $this->getTranslatedString("module_unloadable"));
174 // Load module settings
175 $sf = $dir."settings".$this->getSetting("extension");
176 if (file_exists($sf)) {
178 $this->_settings
[$module] = $settings;
181 // Load module strings
182 $lf = $dir."lang".$this->getSetting("extension");
183 if (file_exists($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);
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
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
228 * Returns the AOOSModel containing all module names, dependencies and instances.
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() {