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;
9 use CoreFramework\Generic\FrameworkException;
12 * The main class with the entry point to the whole application. This class
13 * "emulates" Java's entry point call. Additionally it covers local
14 * variables from outside access to prevent possible attacks on uninitialized
17 * But good little boys and girls would always initialize their variables... ;-)
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 final class ApplicationEntryPoint {
42 private static $corePath = '';
45 * The application's emergency exit
47 * @param $message The optional message we shall output on exit
48 * @param $code Error code from exception
49 * @param $extraData Extra information from exceptions
50 * @param $silentMode Whether silent mode is turned on
52 * @todo This method is old code and needs heavy rewrite and should be moved to ApplicationHelper
54 public static final function app_exit ($message = '', $code = FALSE, $extraData = '', $silentMode = FALSE) {
55 // Is this method already called?
56 if (isset($GLOBALS['app_die_called'])) {
57 // Then output the text directly
61 // This method shall not be called twice
62 $GLOBALS['app_die_called'] = TRUE;
65 if (empty($message)) {
66 // No message provided
67 $message = 'No message provided.';
70 // Get config instance
71 $configInstance = FrameworkConfiguration::getSelfInstance();
73 // Do we have debug installation?
74 if (($configInstance->getConfigEntry('product_install_mode') == 'productive') || ($silentMode === TRUE)) {
80 $tpl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('html_template_class');
81 $languageInstance = LanguageSystem::getSelfInstance();
83 // Initialize template instance here to avoid warnings in IDE
84 $templateInstance = NULL;
86 // Get response instance
87 $responseInstance = ApplicationHelper::getSelfInstance()->getResponseInstance();
89 // Is the template engine loaded?
90 if ((class_exists($tpl)) && (is_object($languageInstance))) {
91 // Use the template engine for putting out (nicer look) the message
93 // Get the template instance from our object factory
94 $templateInstance = ObjectFactory::createObjectByName($tpl);
95 } catch (FrameworkException $e) {
96 exit(sprintf("[Main:] Could not initialize template engine for reason: <span class=\"exception_reason\">%s</span>",
101 // Get and prepare backtrace for output
102 $backtraceArray = debug_backtrace();
104 foreach ($backtraceArray as $key => $trace) {
105 // Set missing array elements
106 if (!isset($trace['file'])) {
107 $trace['file'] = __FILE__;
109 if (!isset($trace['line'])) {
110 $trace['line'] = __LINE__;
112 if (!isset($trace['args'])) {
113 $trace['args'] = array();
116 // Add the traceback path to the final output
117 $backtrace .= sprintf("<span class=\"backtrace_file\">%s</span>:%d, <span class=\"backtrace_function\">%s(%d)</span><br />\n",
118 basename($trace['file']),
121 count($trace['args'])
125 // Init application instance
126 $applicationInstance = NULL;
128 // Is the class there?
129 if (class_exists('CoreFramework\Helper\Application\ApplicationHelper')) {
130 // Get application instance
131 $applicationInstance = ApplicationHelper::getSelfInstance();
133 // Assign application data
134 $templateInstance->assignApplicationData($applicationInstance);
140 $templateInstance->assignVariable('message', $message);
141 $templateInstance->assignVariable('code', $code);
142 $templateInstance->assignVariable('extra', $extraData);
143 $templateInstance->assignVariable('backtrace', $backtrace);
144 $templateInstance->assignVariable('total_includes', ClassLoader::getSelfInstance()->getTotal());
145 $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
146 $templateInstance->assignVariable('title', $languageInstance->getMessage('emergency_exit_title'));
149 $templateInstance->loadCodeTemplate('emergency_exit');
151 // Compile the template
152 $templateInstance->compileTemplate();
154 // Compile all variables
155 $templateInstance->compileVariables();
157 // Transfer data to response
158 $templateInstance->transferToResponse($responseInstance);
160 // Flush the response
161 $responseInstance->flushBuffer();
162 } catch (FileNotFoundException $e) {
163 // Even the template 'emergency_exit' wasn't found so output both message
164 exit($message . ', exception: ' . $e->getMessage());
170 // Output message and die
171 exit(sprintf('[Main:] Emergency exit reached: <span class="emergency_span">%s</span>',
178 * Determines the correct absolute path for all includes only once per run.
179 * Other calls of this method are being "cached".
181 * @return $corePath Base path (core) for all includes
183 protected static final function detectCorePath () {
185 if (empty(self::$corePath)) {
186 // Auto-detect our core path
187 self::$corePath = str_replace("\\", '/', dirname(__FILE__));
191 return self::$corePath;
195 * The framework's main entry point. This class isolates some local
196 * variables which shall not become visible to outside because of security
197 * concerns. This is done here to "emulate" the well-known entry point in
202 public static final function main () {
203 // Load config file, this provides $cfg
204 require(self::detectCorePath() . '/inc/config.php');
206 // Get a new configuration instance
207 $cfg = FrameworkConfiguration::getSelfInstance();
209 // Load bootstrap class
210 require($cfg->getConfigEntry('base_path') . 'inc/bootstrap/class_BootstrapFramework.php');
212 // ----- Below is deprecated -----
214 // Load all include files
215 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
217 // Include the application selector
218 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
222 // Developer mode active? Comment out if no dev!
223 define('DEVELOPER', TRUE);
225 // Log all exceptions (only debug! This option can create large error logs)
226 //define('LOG_EXCEPTIONS', TRUE);
228 //xdebug_start_trace();
230 // Call above main() method
231 ApplicationEntryPoint::main();