2 // Own namespace (watch out: core)
3 namespace CoreFramework\EntryPoint;
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Factory\ObjectFactory;
8 use CoreFramework\Helper\Application\ApplicationHelper;
9 use CoreFramework\Localization\LanguageSystem;
10 use CoreFramework\Loader\ClassLoader;
11 use CoreFramework\Generic\FrameworkException;
17 * The main class with the entry point to the whole application. This class
18 * "emulates" Java's entry point call. Additionally it covers local
19 * variables from outside access to prevent possible attacks on uninitialized
22 * But good little boys and girls would always initialize their variables... ;-)
24 * @author Roland Haeder <webmaster@shipsimu.org>
26 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
30 * This program is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 final class ApplicationEntryPoint {
47 private static $frameworkPath = '';
50 * The application's emergency exit
52 * @param $message The optional message we shall output on exit
53 * @param $code Error code from exception
54 * @param $extraData Extra information from exceptions
55 * @param $silentMode Whether silent mode is turned on
57 * @todo This method is old code and needs heavy rewrite and should be moved to ApplicationHelper
59 public static final function app_exit ($message = '', $code = FALSE, $extraData = '', $silentMode = FALSE) {
60 // Is this method already called?
61 if (isset($GLOBALS['app_die_called'])) {
62 // Then output the text directly
66 // This method shall not be called twice
67 $GLOBALS['app_die_called'] = TRUE;
70 if (empty($message)) {
71 // No message provided
72 $message = 'No message provided.';
75 // Get config instance
76 $configInstance = FrameworkConfiguration::getSelfInstance();
78 // Do we have debug installation?
79 if (($configInstance->getConfigEntry('product_install_mode') == 'productive') || ($silentMode === TRUE)) {
85 $tpl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('html_template_class');
86 $languageInstance = LanguageSystem::getSelfInstance();
88 // Initialize template instance here to avoid warnings in IDE
89 $templateInstance = NULL;
91 // Get response instance
92 $responseInstance = ApplicationHelper::getSelfInstance()->getResponseInstance();
94 // Is the template engine loaded?
95 if ((class_exists($tpl)) && (is_object($languageInstance))) {
96 // Use the template engine for putting out (nicer look) the message
98 // Get the template instance from our object factory
99 $templateInstance = ObjectFactory::createObjectByName($tpl);
100 } catch (FrameworkException $e) {
101 exit(sprintf("[Main:] Could not initialize template engine for reason: <span class=\"exception_reason\">%s</span>",
106 // Get and prepare backtrace for output
107 $backtraceArray = debug_backtrace();
109 foreach ($backtraceArray as $key => $trace) {
110 // Set missing array elements
111 if (!isset($trace['file'])) {
112 $trace['file'] = __FILE__;
114 if (!isset($trace['line'])) {
115 $trace['line'] = __LINE__;
117 if (!isset($trace['args'])) {
118 $trace['args'] = array();
121 // Add the traceback path to the final output
122 $backtrace .= sprintf("<span class=\"backtrace_file\">%s</span>:%d, <span class=\"backtrace_function\">%s(%d)</span><br />\n",
123 basename($trace['file']),
126 count($trace['args'])
130 // Init application instance
131 $applicationInstance = NULL;
133 // Is the class there?
134 if (class_exists('CoreFramework\Helper\Application\ApplicationHelper')) {
135 // Get application instance
136 $applicationInstance = ApplicationHelper::getSelfInstance();
138 // Assign application data
139 $templateInstance->assignApplicationData($applicationInstance);
145 $templateInstance->assignVariable('message', $message);
146 $templateInstance->assignVariable('code', $code);
147 $templateInstance->assignVariable('extra', $extraData);
148 $templateInstance->assignVariable('backtrace', $backtrace);
149 $templateInstance->assignVariable('total_includes', ClassLoader::getSelfInstance()->getTotal());
150 $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
151 $templateInstance->assignVariable('title', $languageInstance->getMessage('emergency_exit_title'));
154 $templateInstance->loadCodeTemplate('emergency_exit');
156 // Compile the template
157 $templateInstance->compileTemplate();
159 // Compile all variables
160 $templateInstance->compileVariables();
162 // Transfer data to response
163 $templateInstance->transferToResponse($responseInstance);
165 // Flush the response
166 $responseInstance->flushBuffer();
167 } catch (FileNotFoundException $e) {
168 // Even the template 'emergency_exit' wasn't found so output both message
169 exit($message . ', exception: ' . $e->getMessage());
175 // Output message and die
176 exit(sprintf('[Main:] Emergency exit reached: <span class="emergency_span">%s</span>',
183 * Determines the correct absolute path for the framework. A set of common
184 * paths is being tested (first most common for applications, second when
185 * core tests are being executed and third/forth if the framework has been
188 * @return $frameworkPath Path for framework
190 public static final function detectFrameworkPath () {
192 if (empty(self::$frameworkPath)) {
193 // Auto-detect core path (first application-common)
194 foreach (array('core', '.', '/usr/local/share/php/core', '/usr/share/php/core') as $possiblePath) {
195 // Create full path for testing
196 $realPath = realpath($possiblePath);
199 //* NOISY-DEBUG: */ printf('[%s:%d]: realPath[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($realPath), $realPath);
202 if ($realPath === FALSE) {
207 // First create full-qualified file name (FQFN) to framework/config.inc.php
209 '%s%sframework%sconfig.inc.php',
217 /* NOISY-DEBUG: */ printf('[%s:%d]: fqfn=%s' . PHP_EOL, __METHOD__, __LINE__, $fqfn);
220 if (is_readable($fqfn)) {
222 self::$frameworkPath = $realPath . '/framework/';
230 if (!is_dir(self::$frameworkPath)) {
232 throw new Exception('Cannot find framework.');
237 return self::$frameworkPath;
241 * The framework's main entry point. This class isolates some local
242 * variables which shall not become visible to outside because of security
243 * concerns. This is done here to "emulate" the well-known entry point in
248 public static final function main () {
249 // Load bootstrap file
250 require(self::detectFrameworkPath() . 'bootstrap/bootstrap.inc.php');
252 // Get a new configuration instance
253 $cfg = FrameworkConfiguration::getSelfInstance();
257 // Developer mode active? Comment out if no dev!
258 define('DEVELOPER', TRUE);
260 // Log all exceptions (only debug! This option can create large error logs)
261 //define('LOG_EXCEPTIONS', TRUE);
263 //xdebug_start_trace();
265 // Call above main() method
266 ApplicationEntryPoint::main();