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