Continued:
[core.git] / application / tests / class_ApplicationHelper.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Helper\Application;
4
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7 use CoreFramework\Registry\Registerable;
8
9 /**
10  * A class holding general data about the application and some methods for
11  * the management including the entry point.
12  *
13  * E.g.:
14  *
15  * index.php?app=my_app
16  *
17  * You need to create a folder in the folder "application" named "my_app"
18  * (without the quotes) and create a include file called
19  * class_ApplicationHelper.php. You have to write the same class for your
20  * application and implement the same interface called ManageableApplication
21  * because this class include file will be searched for.
22  *
23  * It is good when you avoid more GET parameters to keep URLs short and sweet.
24  * But sometimes you need some GET paramerers e.g. for your imprint or info page
25  * or other linked pages which you have to create and state some informations.
26  *
27  * Please remember that this include file is being loaded *before* the class
28  * loader is loading classes from "exceptions", "interfaces" and "main"!
29  *
30  * @author              Roland Haeder <webmaster@shipsimu.org>
31  * @version             0.0
32  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
33  * @license             GNU GPL 3.0 or any newer version
34  *
35  * This program is free software: you can redistribute it and/or modify
36  * it under the terms of the GNU General Public License as published by
37  * the Free Software Foundation, either version 3 of the License, or
38  * (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
47  */
48 class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplication, Registerable {
49         /**
50          * The version number of this application
51          */
52         private $appVersion = '';
53
54         /**
55          * The human-readable name for this application
56          */
57         private $appName = '';
58
59         /**
60          * The short uni*-like name for this application
61          */
62         private $shortName = '';
63
64         /**
65          * An instance of this class
66          */
67         private static $selfInstance = NULL;
68
69         /**
70          * Private constructor
71          *
72          * @return      void
73          */
74         protected function __construct () {
75                 // Call parent constructor
76                 parent::__construct(__CLASS__);
77         }
78
79         /**
80          * Getter for an instance of this class
81          *
82          * @return      $selfInstance   An instance of this class
83          */
84         public static final function getSelfInstance () {
85                 // Is the instance there?
86                 if (is_null(self::$selfInstance)) {
87                         self::$selfInstance = new ApplicationHelper();
88                 } // END - if
89
90                 // Return the instance
91                 return self::$selfInstance;
92         }
93
94         /**
95          * Getter for the version number
96          *
97          * @return      $appVersion     The application's version number
98          */
99         public final function getAppVersion () {
100                 return $this->appVersion;
101         }
102         /**
103          * Setter for the version number
104          *
105          * @param       $appVersion     The application's version number
106          * @return      void
107          */
108         public final function setAppVersion ($appVersion) {
109                 // Cast and set it
110                 $this->appVersion = (string) $appVersion;
111         }
112
113         /**
114          * Getter for human-readable name
115          *
116          * @return      $appName        The application's human-readable name
117          */
118         public final function getAppName () {
119                 return $this->appName;
120         }
121
122         /**
123          * Setter for human-readable name
124          *
125          * @param       $appName        The application's human-readable name
126          * @return      void
127          */
128         public final function setAppName ($appName) {
129                 // Cast and set it
130                 $this->appName = (string) $appName;;
131         }
132
133         /**
134          * Getter for short uni*-like name
135          *
136          * @return      $shortName      The application's short uni*-like name
137          */
138         public final function getAppShortName () {
139                 return $this->shortName;
140         }
141
142         /**
143          * Setter for short uni*-like name
144          *
145          * @param       $shortName      The application's short uni*-like name
146          * @return      void
147          */
148         public final function setAppShortName ($shortName) {
149                 // Cast and set it
150                 $this->shortName = (string) $shortName;
151         }
152
153         /**
154          * Launches the test suite
155          *
156          * @return      void
157          */
158         public final function entryPoint () {
159                 // Set this application in registry
160                 Registry::getRegistry()->addInstance('app', $this);
161
162                 // Default response is console
163                 $response = self::getResponseTypeFromSystem();
164                 $responseType = self::getResponseTypeFromSystem();
165
166                 // Create a new request object
167                 $requestInstance = ObjectFactory::createObjectByName(self::convertToClassName($response) . 'Request');
168
169                 // Remember request instance here
170                 $this->setRequestInstance($requestInstance);
171
172                 // Do we have another response?
173                 if ($requestInstance->isRequestElementSet('request')) {
174                         // Then use it
175                         $response = strtolower($requestInstance->getRequestElement('request'));
176                         $responseType = $response;
177                 } // END - if
178
179                 // ... and a new response object
180                 $responseClass = sprintf('%sResponse', self::convertToClassName($response));
181                 $responseInstance = ObjectFactory::createObjectByName($responseClass, array($this));
182
183                 // Remember response instance here
184                 $this->setResponseInstance($responseInstance);
185
186                 // Get the parameter from the request
187                 $commandName = $requestInstance->getRequestElement('command');
188
189                 // If it is null then get default command
190                 if (is_null($commandName)) {
191                         // Get default command
192                         $commandName = $responseInstance->determineDefaultCommand();
193
194                         // Set it in request
195                         $requestInstance->setRequestElement('command', $commandName);
196                 } // END - if
197
198                 // Get a controller resolver
199                 $resolverClass = self::convertToClassName($this->getAppShortName() . '_' . $responseType . '_controller_resolver');
200                 $resolverInstance = ObjectFactory::createObjectByName($resolverClass, array($commandName, $this));
201
202                 // Get a controller instance as well
203                 $this->setControllerInstance($resolverInstance->resolveController());
204
205                 // Launch the test suite here
206                 $this->getControllerInstance()->handleRequest($requestInstance, $responseInstance);
207
208                 // -------------------------- Shutdown phase --------------------------
209                 // Shutting down the hub by saying "good bye" to all connected peers
210                 // and other hubs, flushing all queues and caches.
211                 self::createDebugInstance(__CLASS__)->debugOutput('MAIN: Shutdown in progress, main loop exited.');
212                 $this->getControllerInstance()->executeShutdownFilters($requestInstance, $responseInstance);
213                 self::createDebugInstance(__CLASS__)->debugOutput('MAIN: Shutdown completed. (This is the last line.)');
214         }
215
216         /**
217          * Handle the indexed array of fatal messages and puts them out in an
218          * acceptable fasion
219          *
220          * @param       $messageList    An array of fatal messages
221          * @return      void
222          */
223         public function handleFatalMessages (array $messageList) {
224                 // Walk through all messages
225                 foreach ($messageList as $message) {
226                         exit(__METHOD__ . ':MSG:' . $message);
227                 } // END - foreach
228         }
229
230         /**
231          * Builds the master template's name
232          *
233          * @return      $masterTemplateName             Name of the master template
234          */
235         public function buildMasterTemplateName () {
236                 return 'node_main';
237         }
238
239         /**
240          * Assigns extra application-depending data
241          *
242          * @param       $templateInstance       An instance of a CompileableTemplate
243          * @return      void
244          * @todo        Nothing to add?
245          */
246         public function assignExtraTemplateData (CompileableTemplate $templateInstance) {
247                 $this->partialStub('Unfinished method. templateInstance=' . $templateInstance->__toString());
248         }
249
250 }