Rewrite of initInstance(), more eval() rewritten to call_user_func_array()
[shipsimu.git] / index.php
1 <?php
2 //xdebug_start_trace();
3 /**
4  * The main class with the entry point to the whole application. This class
5  * "emulates" Java(tm)'s entry point call. Additionally it covers local
6  * variables from outside access to prevent possible attacks on uninitialized
7  * local variables.
8  *
9  * But good little boys and girls would always initialize their variables... ;-)
10  *
11  * @author              Roland Haeder <webmaster@mxchange.org>
12  * @version             0.0.0
13  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.ship-simu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class ApplicationEntryPoint {
31         /**
32          * The instances we want to remove after all is done
33          *
34          * @return      void
35          */
36         private static $instances = array (
37                 'cfg',  // The configuration system
38                 'loader',       // The class loader system
39                 'debug',  // Debug output
40                 'db',     // Database layer
41                 'io',     // Base I/O system (local file [or network])
42                 'engine', // Template engine ( for ApplicationEntryPoint::app_die() )
43                 'lang',   // Language sub-system
44                 'app',    // The ApplicationHelper instance
45         );
46
47         /**
48          * The application's emergency exit
49          *
50          * @param               $message        The optional message we shall output on exit
51          * @return      void
52          */
53         public static function app_die ($message = "") {
54                 // Is this method already called?
55                 if (defined('EMERGENCY_EXIT_CALLED')) {
56                         // Then output the text directly
57                         die($message);
58                 }
59
60                 // This method shall not be called twice
61                 define('EMERGENCY_EXIT_CALLED', true);
62
63                 // Is a message set?
64                 if (empty($message)) {
65                         // No message provided
66                         $message = "No message provided!";
67                 }
68
69                 // Get some instances
70                 $tpl = FrameworkConfiguration::getInstance()->readConfig('tpl_engine');
71                 $lang = LanguageSystem::getInstance();
72                 $io = FileIoHandler::getInstance();
73
74                 // Is the template engine loaded?
75                 if ((class_exists($tpl)) && (is_object($lang)) && (is_object($io))) {
76                         // Use the template engine for putting out (nicer look) the message
77                         try {
78                                 // Get the template instance from our object factory
79                                 $tplEngine = ObjectFactory::createObjectByConfiguredName('tpl_engine', array(FrameworkConfiguration::getInstance()->readConfig('tpl_base_path'), $lang, $io));
80                         } catch (BasePathIsEmptyException $e) {
81                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
82                                         $e->getMessage()
83                                 ));
84                         } catch (InvalidBasePathStringException $e) {
85                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
86                                         $e->getMessage()
87                                 ));
88                         } catch (BasePathIsNoDirectoryException $e) {
89                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
90                                         $e->getMessage()
91                                 ));
92                         } catch (BasePathReadProtectedException $e) {
93                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
94                                         $e->getMessage()
95                                 ));
96                         }
97
98                         // Backtrace holen und aufbereiten
99                         $backtraceArray = debug_backtrace();
100                         $backtrace = "";
101                         foreach ($backtraceArray as $key=>$trace) {
102                                 if (!isset($trace['file'])) $trace['file'] = __FILE__;
103                                 if (!isset($trace['line'])) $trace['line'] = 5;
104                                 if (!isset($trace['args'])) $trace['args'] = array();
105                                 $backtrace .= "<span class=\"backtrace_file\">".basename($trace['file'])."</span>:".$trace['line'].", <span class=\"backtrace_function\">".$trace['function']."(".count($trace['args']).")</span><br />";
106                         }
107
108                         // Assign variables
109                         $tplEngine->assignVariable('message', $message);
110                         $tplEngine->assignVariable('backtrace', $backtrace);
111                         $tplEngine->assignVariable('total_objects', ObjectFactory::getTotal());
112
113                         // Load the template
114                         $tplEngine->loadCodeTemplate('emergency_exit');
115
116                         // Compile the template
117                         $tplEngine->compileTemplate();
118
119                         // Compile all variables
120                         $tplEngine->compileVariables();
121
122                         // Output all
123                         $tplEngine->output();
124
125                         // Good bye...
126                         exit();
127                 } else {
128                         // Output message and die
129                         die(sprintf("[Main:] Emergency exit reached: <span class=\"emergency_span\">%s</span>",
130                                 $message
131                         ));
132                 }
133         }
134
135         /**
136          * The application's main entry point. This class isolates some local
137          * variables which shall not become visible to outside because of security
138          * concerns. We are doing this here to "emulate" the well-known entry
139          * point in Java(tm).
140          *
141          * @return      void
142          */
143         public static function main () {
144                 // Some non-global common arrays we need...
145                 global $_SERVER;
146
147                 // Load config file
148                 require(dirname(__FILE__) . '/inc/config.php');
149
150                 // Load all include files
151                 require(PATH . 'inc/includes.php');
152
153                 // Load all framework classes
154                 require(PATH . 'inc/classes.php');
155
156                 // Include the application selector
157                 require(PATH . 'inc/selector.php');
158
159         } // END - main()
160
161 } // END - class
162
163 // Do not remove the following line:
164 ApplicationEntryPoint::main();
165
166 // [EOF]
167 ?>