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