]> git.mxchange.org Git - city.git/blob - application/city/class_ApplicationHelper.php
Continued:
[city.git] / application / city / 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\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 - 2017 City 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          * The version number of this application
56          */
57         private $appVersion = '';
58
59         /**
60          * The human-readable name for this application
61          */
62         private $appName = '';
63
64         /**
65          * The short uni*-like name for this application
66          */
67         private $shortName = '';
68
69         /**
70          * An instance of this class
71          */
72         private static $selfInstance = NULL;
73
74         /**
75          * Private constructor
76          *
77          * @return      void
78          */
79         protected function __construct () {
80                 // Call parent constructor
81                 parent::__construct(__CLASS__);
82         }
83
84         /**
85          * Getter for an instance of this class
86          *
87          * @return      $selfInstance   An instance of this class
88          */
89         public static final function getSelfInstance () {
90                 // Is the instance there?
91                 if (is_null(self::$selfInstance)) {
92                         self::$selfInstance = new ApplicationHelper();
93                 } // END - if
94
95                 // Return the instance
96                 return self::$selfInstance;
97         }
98
99         /**
100          * Getter for the version number
101          *
102          * @return      $appVersion     The application's version number
103          */
104         public final function getAppVersion () {
105                 return $this->appVersion;
106         }
107         /**
108          * Setter for the version number
109          *
110          * @param       $appVersion     The application's version number
111          * @return      void
112          */
113         public final function setAppVersion ($appVersion) {
114                 // Cast and set it
115                 $this->appVersion = (string) $appVersion;
116         }
117
118         /**
119          * Getter for human-readable name
120          *
121          * @return      $appName        The application's human-readable name
122          */
123         public final function getAppName () {
124                 return $this->appName;
125         }
126
127         /**
128          * Setter for human-readable name
129          *
130          * @param       $appName        The application's human-readable name
131          * @return      void
132          */
133         public final function setAppName ($appName) {
134                 // Cast and set it
135                 $this->appName = (string) $appName;;
136         }
137
138         /**
139          * Getter for short uni*-like name
140          *
141          * @return      $shortName      The application's short uni*-like name
142          */
143         public final function getAppShortName () {
144                 return $this->shortName;
145         }
146
147         /**
148          * Setter for short uni*-like name
149          *
150          * @param       $shortName      The application's short uni*-like name
151          * @return      void
152          */
153         public final function setAppShortName ($shortName) {
154                 // Cast and set it
155                 $this->shortName = (string) $shortName;
156         }
157
158         /**
159          * 1) Setups application data
160          *
161          * @return      void
162          */
163         public function setupApplicationData () {
164                 // Set all application data
165                 $this->setAppName('City Growth Simulation');
166                 $this->setAppVersion('0.0.0');
167                 $this->setAppShortName('city');
168         }
169
170         /**
171          * 2) Does initial stuff before starting the application
172          *
173          * @return      void
174          */
175         public function initApplication () {
176                 // Initialize output system
177                 ApplicationHelper::createDebugInstance('ApplicationHelper');
178
179                 /*
180                  * This application needs a database connection then simply call init
181                  * method.
182                  */
183                 FrameworkBootstrap::initDatabaseInstance();
184         }
185
186         /**
187          * 3) Launches the application
188          *
189          * @return      void
190          */
191         public function launchApplication () {
192                 // Get request/response instances
193                 $requestInstance  = FrameworkBootstrap::getRequestInstance();
194                 $responseInstance = FrameworkBootstrap::getResponseInstance();
195
196                 // Get the parameter from the request
197                 $commandName = $requestInstance->getRequestElement('command');
198
199                 // If it is null then get default command
200                 if (is_null($commandName)) {
201                         // Get default command
202                         $commandName = $responseInstance->determineDefaultCommand();
203
204                         // Set it in request
205                         $requestInstance->setRequestElement('command', $commandName);
206                 } // END - if
207
208                 // Get a controller resolver
209                 $resolverClass = sprintf(
210                         'Org\Mxchange\City\Resolver\Controller\%s',
211                         self::convertToClassName(sprintf(
212                                 '%s_%s_controller_resolver',
213                                 $this->getAppShortName(),
214                                 FrameworkBootstrap::getRequestTypeFromSystem()
215                         ))
216                 );
217                 $resolverInstance = ObjectFactory::createObjectByName($resolverClass, array($commandName));
218
219                 // Get a controller instance as well
220                 $this->setControllerInstance($resolverInstance->resolveController());
221
222                 // Initialize language system
223                 $languageInstance = ObjectFactory::createObjectByConfiguredName('language_system_class');
224
225                 // And set it here
226                 $this->setLanguageInstance($languageInstance);
227
228                 // Is html request?
229                 if (FrameworkBootstrap::getRequestTypeFromSystem() == 'html') {
230                         // Init web output instance
231                         $this->initWebOutputInstance();
232                 }
233
234                 // Launch the application here
235                 $this->getControllerInstance()->handleRequest($requestInstance, $responseInstance);
236
237                 // Is console request?
238                 if (FrameworkBootstrap::getRequestTypeFromSystem() == 'console') {
239                         // -------------------------- Shutdown phase --------------------------
240                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MAIN: Shutdown in progress ...');
241                         $this->getControllerInstance()->executeShutdownFilters($requestInstance, $responseInstance);
242                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MAIN: Shutdown completed. (This is the last line.)');
243                 }
244         }
245
246         /**
247          * Handle the indexed array of fatal messages and puts them out in an
248          * acceptable fasion
249          *
250          * @param       $messageList    An array of fatal messages
251          * @return      void
252          */
253         public function handleFatalMessages (array $messageList) {
254                 // Walk through all messages
255                 foreach ($messageList as $message) {
256                         exit(__METHOD__ . ':MSG:' . $message);
257                 } // END - foreach
258         }
259
260         /**
261          * Builds the master template's name
262          *
263          * @return      $masterTemplateName             Name of the master template
264          */
265         public function buildMasterTemplateName () {
266                 return 'city_main';
267         }
268
269         /**
270          * Assigns extra application-depending data
271          *
272          * @param       $templateInstance       An instance of a CompileableTemplate
273          * @return      void
274          * @todo        Nothing to add?
275          */
276         public function assignExtraTemplateData (CompileableTemplate $templateInstance) {
277                 $this->partialStub('Unfinished method. templateInstance=' . $templateInstance->__toString());
278         }
279
280 }