2 // Own namespace (watch out: core)
3 namespace CoreFramework\EntryPoint;
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Helper\Application\ApplicationHelper;
8 use CoreFramework\Loader\ClassLoader;
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
16 * But good little boys and girls would always initialize their variables... ;-)
18 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
37 final class ApplicationEntryPoint {
41 private static $corePath = '';
44 * The application's emergency exit
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
51 * @todo This method is old code and needs heavy rewrite and should be moved to ApplicationHelper
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
60 // This method shall not be called twice
61 $GLOBALS['app_die_called'] = TRUE;
64 if (empty($message)) {
65 // No message provided
66 $message = 'No message provided.';
69 // Get config instance
70 $configInstance = FrameworkConfiguration::getSelfInstance();
72 // Do we have debug installation?
73 if (($configInstance->getConfigEntry('product_install_mode') == 'productive') || ($silentMode === TRUE)) {
79 $tpl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('html_template_class');
80 $languageInstance = LanguageSystem::getSelfInstance();
82 // Initialize template instance here to avoid warnings in IDE
83 $templateInstance = NULL;
85 // Get response instance
86 $responseInstance = ApplicationHelper::getSelfInstance()->getResponseInstance();
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
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>",
100 // Get and prepare backtrace for output
101 $backtraceArray = debug_backtrace();
103 foreach ($backtraceArray as $key => $trace) {
104 // Set missing array elements
105 if (!isset($trace['file'])) {
106 $trace['file'] = __FILE__;
108 if (!isset($trace['line'])) {
109 $trace['line'] = __LINE__;
111 if (!isset($trace['args'])) {
112 $trace['args'] = array();
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']),
120 count($trace['args'])
124 // Init application instance
125 $applicationInstance = NULL;
127 // Is the class there?
128 if (class_exists('CoreFramework\Helper\Application\ApplicationHelper')) {
129 // Get application instance
130 $applicationInstance = ApplicationHelper::getSelfInstance();
132 // Assign application data
133 $templateInstance->assignApplicationData($applicationInstance);
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'));
148 $templateInstance->loadCodeTemplate('emergency_exit');
150 // Compile the template
151 $templateInstance->compileTemplate();
153 // Compile all variables
154 $templateInstance->compileVariables();
156 // Transfer data to response
157 $templateInstance->transferToResponse($responseInstance);
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());
169 // Output message and die
170 exit(sprintf('[Main:] Emergency exit reached: <span class="emergency_span">%s</span>',
177 * Determines the correct absolute path for all includes only once per run.
178 * Other calls of this method are being "cached".
180 * @return $corePath Base path (core) for all includes
182 protected static final function detectCorePath () {
184 if (empty(self::$corePath)) {
185 // Auto-detect our core path
186 self::$corePath = str_replace("\\", '/', dirname(__FILE__));
190 return self::$corePath;
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
201 public static final function main () {
202 // Load config file, this provides $cfg
203 require(self::detectCorePath() . '/inc/config.php');
205 // Get a new configuration instance
206 $cfg = FrameworkConfiguration::getSelfInstance();
208 // Load bootstrap class
209 require($cfg->getConfigEntry('base_path') . 'inc/bootstrap/class_BootstrapFramework.php');
211 // ----- Below is deprecated -----
213 // Load all include files
214 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
216 // Include the application selector
217 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
221 // Developer mode active? Comment out if no dev!
222 define('DEVELOPER', TRUE);
224 // Log all exceptions (only debug! This option can create large error logs)
225 //define('LOG_EXCEPTIONS', TRUE);
227 //xdebug_start_trace();
229 // Call above main() method
230 ApplicationEntryPoint::main();