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