Imported ReflectionException
[core.git] / index.php
1 <?php
2 // Own namespace (watch out: core)
3 namespace CoreFramework\EntryPoint;
4
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Helper\Application\ApplicationHelper;
8 use CoreFramework\Loader\ClassLoader;
9
10 /**
11  * The main class with the entry point to the whole application. This class
12  * "emulates" Java's entry point call. Additionally it covers local
13  * variables from outside access to prevent possible attacks on uninitialized
14  * local variables.
15  *
16  * But good little boys and girls would always initialize their variables... ;-)
17  *
18  * @author              Roland Haeder <webmaster@shipsimu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.shipsimu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program. If not, see <http://www.gnu.org/licenses/>.
36  */
37 final class ApplicationEntryPoint {
38         /**
39          * Core path
40          */
41         private static $corePath = '';
42
43         /**
44          * The application's emergency exit
45          *
46          * @param       $message                The optional message we shall output on exit
47          * @param       $code                   Error code from exception
48          * @param       $extraData              Extra information from exceptions
49          * @param       $silentMode             Whether silent mode is turned on
50          * @return      void
51          * @todo        This method is old code and needs heavy rewrite and should be moved to ApplicationHelper
52          */
53         public static final function app_exit ($message = '', $code = FALSE, $extraData = '', $silentMode = FALSE) {
54                 // Is this method already called?
55                 if (isset($GLOBALS['app_die_called'])) {
56                         // Then output the text directly
57                         exit($message);
58                 } // END - if
59
60                 // This method shall not be called twice
61                 $GLOBALS['app_die_called'] = TRUE;
62
63                 // Is a message set?
64                 if (empty($message)) {
65                         // No message provided
66                         $message = 'No message provided.';
67                 } // END - if
68
69                 // Get config instance
70                 $configInstance = FrameworkConfiguration::getSelfInstance();
71
72                 // Do we have debug installation?
73                 if (($configInstance->getConfigEntry('product_install_mode') == 'productive') || ($silentMode === TRUE)) {
74                         // Abort here
75                         exit();
76                 } // END - if
77
78                 // Get some instances
79                 $tpl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('html_template_class');
80                 $languageInstance = LanguageSystem::getSelfInstance();
81
82                 // Initialize template instance here to avoid warnings in IDE
83                 $templateInstance = NULL;
84
85                 // Get response instance
86                 $responseInstance = ApplicationHelper::getSelfInstance()->getResponseInstance();
87
88                 // Is the template engine loaded?
89                 if ((class_exists($tpl)) && (is_object($languageInstance))) {
90                         // Use the template engine for putting out (nicer look) the message
91                         try {
92                                 // Get the template instance from our object factory
93                                 $templateInstance = ObjectFactory::createObjectByName($tpl);
94                         } catch (FrameworkException $e) {
95                                 exit(sprintf("[Main:] Could not initialize template engine for reason: <span class=\"exception_reason\">%s</span>",
96                                         $e->getMessage()
97                                 ));
98                         }
99
100                         // Get and prepare backtrace for output
101                         $backtraceArray = debug_backtrace();
102                         $backtrace = '';
103                         foreach ($backtraceArray as $key => $trace) {
104                                 // Set missing array elements
105                                 if (!isset($trace['file'])) {
106                                         $trace['file'] = __FILE__;
107                                 } // END - if
108                                 if (!isset($trace['line'])) {
109                                         $trace['line'] = __LINE__;
110                                 } // END - if
111                                 if (!isset($trace['args'])) {
112                                         $trace['args'] = array();
113                                 } // END - if
114
115                                 // Add the traceback path to the final output
116                                 $backtrace .= sprintf("<span class=\"backtrace_file\">%s</span>:%d, <span class=\"backtrace_function\">%s(%d)</span><br />\n",
117                                         basename($trace['file']),
118                                         $trace['line'],
119                                         $trace['function'],
120                                         count($trace['args'])
121                                 );
122                         } // END - foreach
123
124                         // Init application instance
125                         $applicationInstance = NULL;
126
127                         // Is the class there?
128                         if (class_exists('CoreFramework\Helper\Application\ApplicationHelper')) {
129                                 // Get application instance
130                                 $applicationInstance = ApplicationHelper::getSelfInstance();
131
132                                 // Assign application data
133                                 $templateInstance->assignApplicationData($applicationInstance);
134                         } // END - if
135
136                         // We only try this
137                         try {
138                                 // Assign variables
139                                 $templateInstance->assignVariable('message', $message);
140                                 $templateInstance->assignVariable('code', $code);
141                                 $templateInstance->assignVariable('extra', $extraData);
142                                 $templateInstance->assignVariable('backtrace', $backtrace);
143                                 $templateInstance->assignVariable('total_includes', ClassLoader::getSelfInstance()->getTotal());
144                                 $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
145                                 $templateInstance->assignVariable('title', $languageInstance->getMessage('emergency_exit_title'));
146
147                                 // Load the template
148                                 $templateInstance->loadCodeTemplate('emergency_exit');
149
150                                 // Compile the template
151                                 $templateInstance->compileTemplate();
152
153                                 // Compile all variables
154                                 $templateInstance->compileVariables();
155
156                                 // Transfer data to response
157                                 $templateInstance->transferToResponse($responseInstance);
158
159                                 // Flush the response
160                                 $responseInstance->flushBuffer();
161                         } catch (FileNotFoundException $e) {
162                                 // Even the template 'emergency_exit' wasn't found so output both message
163                                 exit($message . ', exception: ' . $e->getMessage());
164                         }
165
166                         // Good bye...
167                         exit();
168                 } else {
169                         // Output message and die
170                         exit(sprintf('[Main:] Emergency exit reached: <span class="emergency_span">%s</span>',
171                                 $message
172                         ));
173                 }
174         }
175
176         /**
177          * Determines the correct absolute path for all includes only once per run.
178          * Other calls of this method are being "cached".
179          *
180          * @return      $corePath       Base path (core) for all includes
181          */
182         protected static final function detectCorePath () {
183                 // Is it not set?
184                 if (empty(self::$corePath)) {
185                         // Auto-detect our core path
186                         self::$corePath = str_replace("\\", '/', dirname(__FILE__));
187                 } // END - if
188
189                 // Return it
190                 return self::$corePath;
191         }
192
193         /**
194          * The framework's main entry point. This class isolates some local
195          * variables which shall not become visible to outside because of security
196          * concerns. This is done here to "emulate" the well-known entry point in
197          * Java.
198          *
199          * @return      void
200          */
201         public static final function main () {
202                 // Load config file, this provides $cfg
203                 require(self::detectCorePath() . '/inc/config.php');
204
205                 // Get a new configuration instance
206                 $cfg = FrameworkConfiguration::getSelfInstance();
207
208                 // Load bootstrap class
209                 require($cfg->getConfigEntry('base_path') . 'inc/bootstrap/class_BootstrapFramework.php');
210
211                 // ----- Below is deprecated -----
212
213                 // Load all include files
214                 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
215
216                 // Include the application selector
217                 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
218         }
219 }
220
221 // Developer mode active? Comment out if no dev!
222 define('DEVELOPER', TRUE);
223
224 // Log all exceptions (only debug! This option can create large error logs)
225 //define('LOG_EXCEPTIONS', TRUE);
226
227 //xdebug_start_trace();
228
229 // Call above main() method
230 ApplicationEntryPoint::main();