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