]> git.mxchange.org Git - shipsimu.git/blob - application/shipsimu/class_ApplicationHelper.php
Continued:
[shipsimu.git] / application / shipsimu / class_ApplicationHelper.php
1 <?php
2 // Must be this namespace, else the launcher cannot find the class.
3 namespace Org\Mxchange\CoreFramework\Helper\Application;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Application\BaseApplication;
7 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
8 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Loader\ClassLoader;
10 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
11 use Org\Mxchange\CoreFramework\Registry\Registerable;
12 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
13
14 /**
15  * A class holding general data about the application and some methods for
16  * the management including the entry point.
17  *
18  * E.g.:
19  *
20  * index.php?app=my_app
21  *
22  * You need to create a folder in the folder "application" named "my_app"
23  * (without the quotes) and create a include file called
24  * class_ApplicationHelper.php. You have to write the same class for your
25  * application and implement the same interface called ManageableApplication
26  * because this class include file will be searched for.
27  *
28  * It is good when you avoid more GET parameters to keep URLs short and sweet.
29  * But sometimes you need some GET paramerers e.g. for your imprint or info page
30  * or other linked pages which you have to create and state some informations.
31  *
32  * Please remember that this include file is being loaded *before* the class
33  * loader is loading classes from "exceptions", "interfaces" and "main"!
34  *
35  * @author              Roland Haeder <webmaster@shipsimu.org>
36  * @version             0.0
37  * @copyright   Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2022 Ship-Simu Developer Team
38  * @license             GNU GPL 3.0 or any newer version
39  *
40  * This program is free software: you can redistribute it and/or modify
41  * it under the terms of the GNU General Public License as published by
42  * the Free Software Foundation, either version 3 of the License, or
43  * (at your option) any later version.
44  *
45  * This program is distributed in the hope that it will be useful,
46  * but WITHOUT ANY WARRANTY; without even the implied warranty of
47  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48  * GNU General Public License for more details.
49  *
50  * You should have received a copy of the GNU General Public License
51  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
52  */
53 class ApplicationHelper extends BaseApplication implements ManageableApplication, Registerable {
54         /**
55          * Private constructor
56          *
57          * @return      void
58          */
59         private function __construct () {
60                 // Call parent constructor
61                 parent::__construct(__CLASS__);
62         }
63
64         /**
65          * Getter for an instance of this class
66          *
67          * @return      $selfInstance   An instance of this class
68          */
69         public static final function getSelfInstance () {
70                 // Is the instance there?
71                 if (is_null(self::getApplicationInstance())) {
72                         self::setApplicationInstance(new ApplicationHelper());
73                 }
74
75                 // Return the instance
76                 return self::getApplicationInstance();
77         }
78
79         /**
80          * 1) Setups application data
81          *
82          * @return      void
83          */
84         public function setupApplicationData () {
85                 // Set all application data
86                 $this->setAppName('Ship-Simu Shipping Simulator');
87                 $this->setAppVersion('0.0.0');
88                 $this->setAppShortName('shipsimu');
89         }
90
91         /**
92          * 2) Does initial stuff before starting the application
93          *
94          * @return      void
95          */
96         public function initApplication () {
97                 // Get config instance
98                 $cfg = FrameworkBootstrap::getConfigurationInstance();
99
100                 // Initialize output system
101                 ApplicationHelper::createDebugInstance('ApplicationHelper');
102
103                 /*
104                  * This application needs a database connection then simply call init
105                  * method.
106                  */
107                 FrameworkBootstrap::initDatabaseInstance();
108         }
109
110         /**
111          * 3) Launches the application
112          *
113          * @return      void
114          */
115         public function launchApplication () {
116                 // Get request/response instances
117                 $requestInstance  = FrameworkBootstrap::getRequestInstance();
118                 $responseInstance = FrameworkBootstrap::getResponseInstance();
119
120                 // Get the parameter from the request
121                 $commandName = $requestInstance->getRequestElement('command');
122
123                 // If it is null then get default command
124                 if (is_null($commandName)) {
125                         // Get default command
126                         $commandName = $responseInstance->determineDefaultCommand();
127
128                         // Debug message
129                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('commandName[%s]=%s', gettype($commandName), $commandName));
130
131                         // Set it in request
132                         $requestInstance->setRequestElement('command', $commandName);
133                 }
134
135                 // Is the request type 'html' ?
136                 if (FrameworkBootstrap::getRequestTypeFromSystem() == 'html') {
137                         // The language system is needed for this
138                         $languageInstance = ObjectFactory::createObjectByConfiguredName('language_system_class');
139
140                         // And set it here
141                         $this->setLanguageInstance($languageInstance);
142                 }
143
144                 // Configuration entry key
145                 $configEntry = sprintf(
146                         '%s_%s_controller_resolver_class',
147                         $this->getAppShortName(),
148                         FrameworkBootstrap::getRequestTypeFromSystem()
149                 );
150
151                 // Get a controller resolver instance
152                 $resolverInstance = ObjectFactory::createObjectByConfiguredName($configEntry, [
153                         $commandName,
154                 ]);
155
156                 // Get a controller instance as well
157                 $this->setControllerInstance($resolverInstance->resolveController());
158
159                 // Launch the test suite here
160                 $this->getControllerInstance()->handleRequest($requestInstance, $responseInstance);
161
162                 // Only for console requests as this is the actual daemon
163                 if ($requestType == 'console') {
164                         // -------------------------- Shutdown phase --------------------------
165                         // Shutting down the hub by saying "good bye" to all connected peers
166                         // and other hubs, flushing all queues and caches.
167                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MAIN: Shutdown in progress, main loop exited.');
168                         $this->getControllerInstance()->executeShutdownFilters($requestInstance, $responseInstance);
169                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MAIN: Shutdown completed. (This is the last line.)');
170                 }
171         }
172
173         /**
174          * Assigns extra application-depending data
175          *
176          * @param       $templateInstance       An instance of a CompileableTemplate class
177          * @return      void
178          * @todo        Nothing to add?
179          */
180         public function assignExtraTemplateData (CompileableTemplate $templateInstance) {
181         }
182
183         /**
184          * Handle the indexed array of fatal messages and puts them out in an
185          * acceptable fasion
186          *
187          * @param       $messageList    An array of fatal messages
188          * @return      void
189          */
190         public function handleFatalMessages (array $messageList) {
191                 // Walk through all messages
192                 foreach ($messageList as $message) {
193                         exit(__METHOD__ . ':MSG:' . $message);
194                 }
195         }
196
197         /**
198          * Builds the master template's name
199          *
200          * @return      $masterTemplateName             Name of the master template
201          */
202         public function buildMasterTemplateName () {
203                 return 'node_daemon';
204         }
205 }