Logout procedure basicly finished, login area splitted in main and action-dependent...
[shipsimu.git] / application / selector / class_ApplicationSelector.php
1 <?php
2 /**
3  * The application selector class.
4  *
5  * Please remember that this include file is being loaded *before* the class
6  * loader is loading classes from "exceptions", "interfaces" and "main"!
7  *
8  * @author              Roland Haeder <webmaster@ship-simu.org>
9  * @version             0.0.0
10  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
11  * @license             GNU GPL 3.0 or any newer version
12  * @link                http://www.ship-simu.org
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 class ApplicationSelector extends BaseFrameworkSystem {
28         /**
29          * An ArrayObject for all found applications
30          */
31         private $foundApps = null;
32
33         /**
34          * An array object for all loaded application templates (selector_app-name.tpl)
35          */
36         private $loadedTemplates = null;
37
38         /**
39          * The application selector's own template engine handler
40          */
41         private $selectorTplEngine = null;
42
43         /**
44          * A list of items we shall ignore while reading from directories
45          */
46         private $dirIgnoreList = array(
47                 ".",
48                 "..",
49                 ".htaccess",
50                 ".svn"
51         );
52
53         /**
54          * The protected constructor. No direct instances can be created from this.
55          *
56          * @return      void
57          */
58         protected function __construct() {
59                 // Call parent constructor
60                 parent::__construct(__CLASS__);
61
62                 // Set description
63                 $this->setObjectDescription("Applikationswechsler");
64
65                 // Create unique ID number
66                 $this->generateUniqueId();
67
68                 // Remove system array and thousand seperator
69                 $this->removeSystemArray();
70                 $this->removeNumberFormaters();
71
72                 // Initialize the array lists
73                 $this->initializeAppsList();
74                 $this->initializeTemplatesList();
75         }
76
77         /**
78          * Create a prepared instance of ApplicationSelector
79          *
80          * @param               $langInstance           The language sub-system: LanguageSystem
81          * @param               $fileIOInstance The file I/O instance
82          * @return              $selInstance            An instance of ApplicationSelector
83          */
84         public final static function createApplicationSelector (ManageableLanguage $langInstance, FileIoHandler $fileIOInstance) {
85                 // Get a new instance
86                 $selInstance = new ApplicationSelector();
87
88                 // Get all applications
89                 $selInstance->readApplicationDirectory();
90
91                 // Set language and file I/O instances
92                 $selInstance->setLanguageInstance($langInstance);
93                 $selInstance->setFileIoInstance($fileIOInstance);
94
95                 // Return the prepared instance
96                 return $selInstance;
97         }
98
99         /**
100          * Initialize the application list
101          *
102          * @return      void
103          */
104         private function initializeAppsList () {
105                 $this->foundApps = new FrameworkArrayObject("FakedFoundApplications");
106         }
107
108         /**
109          * Initialize the loaded templates list
110          *
111          * @return      void
112          */
113         private function initializeTemplatesList () {
114                 $this->loadedTemplates = new FrameworkArrayObject("FakedLoadedTemplates");
115         }
116
117         /**
118          * Load the init.php script of an application and append the application
119          * instance to $foundApps
120          *
121          * @param               $initScript     The FQFN of init.php
122          * @param               $appName                The application's Uni* name
123          * @return      void
124          * @throws      AppVarIsNotSetException If 'app' is not set
125          * @throws      NullPointerException    If 'app' is null
126          * @throws      NoObjectException               If 'app' is not an object
127          * @throws      MissingMethodException  If a required method is missing
128          */
129         private function loadInitScript ($initScript, $appName) {
130                 // Is it a file and readable?
131                 if ((is_file($initScript)) && (is_readable($initScript))) {
132                         // Then include it
133                         include ($initScript);
134
135                         // We now should have $app re-defined!
136                         if (!isset($app)) {
137                                 // This application shall not be loaded
138                                 return;
139                         } elseif (is_null($app)) {
140                                 // The class instance is null
141                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
142                         } elseif (!is_object($app)) {
143                                 // Not an object
144                                 throw new NoObjectException($app, self::EXCEPTION_IS_NO_OBJECT);
145                         } elseif (!method_exists($app, $this->getConfigInstance()->readConfig('entry_method'))) {
146                                 // Method not found!
147                                 throw new MissingMethodException(array($app, $this->getConfigInstance()->readConfig('entry_method')), self::EXCEPTION_MISSING_METHOD);
148                         }
149
150                         // Add the current instance to the list
151                         $this->foundApps->append($app);
152
153                 } // END - if ((is_file(...
154         }
155
156         /**
157          * Setter for the selector's template engine instance
158          *
159          * @param               $tplEngine      An instance of TemplateEngine
160          */
161         private final function setSelectorTemplateEngine (CompileableTemplate $tplEngine) {
162                 $this->selectorTplEngine = $tplEngine;
163         }
164
165         /**
166          * Getter for the selector's template engine instance
167          *
168          * @return      $selectTplEngine        The selector's template engine
169          */
170         private final function getSelectorTemplateEngine () {
171                 return $this->selectorTplEngine;
172         }
173
174         /**
175          * Getter for the $loadedTemplates array object
176          *
177          * @return      $loadedTemplates        An array object holding all loaded
178          *                                                      application templates
179          */
180         private final function getLoadedTemplates () {
181                 return $this->loadedTemplates;
182         }
183
184         /**
185          * Method for compatiblity with prepareTemplateEngine()
186          *
187          * @return      $shortName      This selector's short name
188          */
189         public function getAppShortName() {
190                 $shortName = $this->getConfigInstance()->readConfig('selector_path');
191                 return $shortName;
192         }
193
194         /**
195          * Add a directory/file to the ignore list
196          *
197          * @param               $ignoreItem     The file/directory we shall ignore
198          * @return      void
199          */
200         public function addDirIgnoreList ($ignoreItem) {
201                 // Cast and add it
202                 $ignoreItem = (string) $ignoreItem;
203                 $this->dirIgnoreList[] = $ignoreItem;
204         }
205
206         /**
207          * Read the base path for all applications (application/) and create a
208          * list of all found applications
209          *
210          * @return      void
211          */
212         public function readApplicationDirectory () {
213                 // Generate the base path for all applications
214                 $appBasePath = sprintf("%s%s/",
215                         PATH,
216                         $this->getConfigInstance()->readConfig('application_path')
217                 );
218
219                 // Add the selector path to the ignore list
220                 $this->addDirIgnoreList($this->getConfigInstance()->readConfig('selector_path'));
221
222                 // Get a directory pointer for the application path
223                 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
224
225                 // Backup and remove the 'app' from global name space
226                 /*$appBackup = $app;
227                 unset($app);*/
228
229                 // Read all directories&files except some parts
230                 while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
231                         // Generate FQFN for the application name (or better directory name)
232                         $fqfn = sprintf("%s%s", $appBasePath, $appName);
233
234                         // Is this a readable directory? (files will be ignored silently)
235                         if ((is_dir($fqfn)) && (is_readable($fqfn))) {
236                                 // Then get the init.php script for analyzing
237                                 $initScript = sprintf("%s/init%s", $fqfn, $this->getConfigInstance()->readConfig('php_extension'));
238
239                                 // Load the application's init.php script and append the
240                                 // application to the ArrayObject
241                                 $this->loadInitScript($initScript, $appName);
242
243                         } // END - if ((is_dir(...
244
245                 } // END - while
246
247                 // Close directory pointer
248                 $dirInstance->closeDirectory();
249
250                 // Restore old 'app' from backup
251                 //$app = $appBackup;
252         }
253
254         /**
255          * Load all templates for found applications in previous scan
256          *
257          * @return      void
258          */
259         public function loadApplicationTemplates () {
260                 // Iterate through all applications
261                 for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
262                         // Get current application
263                         $appInstance = $idx->current();
264
265                         // Prepare the template engine for the current template
266                         $tplEngine = $this->prepareTemplateEngine($appInstance);
267
268                         // Try to load the web template
269                         $tplEngine->loadWebTemplate(sprintf("%s_%s",
270                                 $this->getConfigInstance()->readConfig('tpl_selector_prefix'),
271                                 strtolower($appInstance->getAppShortName())
272                         ));
273
274                         // Remember this template and the application for later usage
275                         $this->loadedTemplates->append(array(
276                                 'template_class'   => $tplEngine,
277                                 'app_instance' => $appInstance
278                         ));
279                 }
280
281                 // Re-initialize the application list to avoid double loading
282                 $this->initializeAppsList();
283         }
284
285         /**
286          * Removes $dirIgnoreList from the object to save some memory
287          *
288          * @return      void
289          */
290         public final function removeDirIgnoreList () {
291                 unset($this->dirIgnoreList);
292         }
293
294         /**
295          * Loads the selector's own main template. This step is not linking the
296          * application's templates into the main one.
297          *
298          * @return      void
299          */
300         public function loadSelectorTemplate () {
301                 // Prepare the template engine
302                 $tplEngine = $this->prepareTemplateEngine($this);
303
304                 // Load the selector's template
305                 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_main_tpl'));
306
307                 // Now store it in the class
308                 $this->setSelectorTemplateEngine($tplEngine);
309         }
310
311         /**
312          * Inserts all loaded application templates into the selector's template
313          *
314          * @return      void
315          * @throws      NullPointerException            If $curr is null
316          * @throws      NoArrayException                        If $curr is not an array
317          * @throws      InvalidArrayCountException      If $curr contains an
318          *                                                                              unexpected count of elements
319          * @throws      MissingArrayElementsException   If $curr is missing expected
320          *                                                                                      array elements
321          */
322         public function insertApplicationTemplates () {
323                 // First prepare the instance
324                 $tplEngine = $this->prepareTemplateEngine($this);
325
326                 // Load template which shall later hold all application templates
327                 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_apps_tpl'));
328
329                 // Add all loaded application templates together
330                 $dummy = "";
331                 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
332                         // Get current item from array object
333                         $curr = $idx->current();
334
335                         // Do some sanity checks on the loaded item
336                         if (is_null($curr)) {
337                                 // $curr is null
338                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
339                         } elseif (!is_array($curr)) {
340                                 // Not an array
341                                 throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
342                         } elseif (count($curr) != 2) {
343                                 // Not expected count of entries
344                                 throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
345                         } elseif (!isset($curr['template_class']) || (!isset($curr['app_instance']))) {
346                                 // Expected entries missing
347                                 throw new MissingArrayElementsException(array($this, "curr", array('template_class', 'app_instance')), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
348                         }
349                         die("<pre>".print_r($curr, true)."</pre>");
350
351                 } // END - for
352         }
353 }
354
355 // [EOF]
356 ?>