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