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