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