generateUniqueId() and more useless/deprecated methods removed, code speed-up, link...
[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                 // Remove system array and thousand seperator
63                 $this->removeSystemArray();
64                 $this->removeNumberFormaters();
65
66                 // Initialize the array lists
67                 $this->initializeAppsList();
68                 $this->initializeTemplatesList();
69         }
70
71         /**
72          * Create a prepared instance of ApplicationSelector
73          *
74          * @param       $langInstance           The language sub-system: LanguageSystem
75          * @param       $fileIOInstance         The file I/O instance
76          * @return      $selInstance            An instance of ApplicationSelector
77          */
78         public final static function createApplicationSelector (ManageableLanguage $langInstance, FileIoHandler $fileIOInstance) {
79                 // Get a new instance
80                 $selInstance = new ApplicationSelector();
81
82                 // Get all applications
83                 $selInstance->readApplicationDirectory();
84
85                 // Set language and file I/O instances
86                 $selInstance->setLanguageInstance($langInstance);
87                 $selInstance->setFileIoInstance($fileIOInstance);
88
89                 // Return the prepared instance
90                 return $selInstance;
91         }
92
93         /**
94          * Initialize the application list
95          *
96          * @return      void
97          */
98         private function initializeAppsList () {
99                 $this->foundApps = new FrameworkArrayObject("FakedFoundApplications");
100         }
101
102         /**
103          * Initialize the loaded templates list
104          *
105          * @return      void
106          */
107         private function initializeTemplatesList () {
108                 $this->loadedTemplates = new FrameworkArrayObject("FakedLoadedTemplates");
109         }
110
111         /**
112          * Load the init.php script of an application and append the application
113          * instance to $foundApps
114          *
115          * @param       $initScript     The FQFN of init.php
116          * @param       $appName                The application's Uni* name
117          * @return      void
118          */
119         private function loadInitScript ($initScript, $appName) {
120                 // Is it a file and readable?
121                 if ((is_file($initScript)) && (is_readable($initScript))) {
122                         // Then include it
123                         include ($initScript);
124
125                         // Add the current instance to the list
126                         $this->foundApps->append($app);
127
128                 } // END - if ((is_file(...
129         }
130
131         /**
132          * Setter for the selector's template engine instance
133          *
134          * @param       $templateInstance       An instance of TemplateEngine
135          * @return      void
136          */
137         private final function setSelectorTemplateEngine (CompileableTemplate $templateInstance) {
138                 $this->selectorTplEngine = $templateInstance;
139         }
140
141         /**
142          * Getter for the selector's template engine instance
143          *
144          * @return      $selectTplEngine        The selector's template engine
145          */
146         private final function getSelectorTemplateEngine () {
147                 return $this->selectorTplEngine;
148         }
149
150         /**
151          * Getter for the $loadedTemplates array object
152          *
153          * @return      $loadedTemplates        An array object holding all loaded
154          *                                                      application templates
155          */
156         private final function getLoadedTemplates () {
157                 return $this->loadedTemplates;
158         }
159
160         /**
161          * Method for compatiblity with prepareTemplateInstance()
162          *
163          * @return      $shortName      This selector's short name
164          */
165         public function getAppShortName() {
166                 $shortName = $this->getConfigInstance()->readConfig('selector_path');
167                 return $shortName;
168         }
169
170         /**
171          * Add a directory/file to the ignore list
172          *
173          * @param               $ignoreItem     The file/directory we shall ignore
174          * @return      void
175          */
176         public function addDirIgnoreList ($ignoreItem) {
177                 // Cast and add it
178                 $ignoreItem = (string) $ignoreItem;
179                 $this->dirIgnoreList[] = $ignoreItem;
180         }
181
182         /**
183          * Read the base path for all applications (application/) and create a
184          * list of all found applications
185          *
186          * @return      void
187          */
188         public function readApplicationDirectory () {
189                 // Generate the base path for all applications
190                 $appBasePath = sprintf("%s%s/",
191                         PATH,
192                         $this->getConfigInstance()->readConfig('application_path')
193                 );
194
195                 // Add the selector path to the ignore list
196                 $this->addDirIgnoreList($this->getConfigInstance()->readConfig('selector_path'));
197
198                 // Get a directory pointer for the application path
199                 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
200
201                 // Backup and remove the 'app' from global name space
202                 /*$appBackup = $app;
203                 unset($app);*/
204
205                 // Read all directories&files except some parts
206                 while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
207                         // Generate FQFN for the application name (or better directory name)
208                         $fqfn = sprintf("%s%s", $appBasePath, $appName);
209
210                         // Is this a readable directory? (files will be ignored silently)
211                         if ((is_dir($fqfn)) && (is_readable($fqfn))) {
212                                 // Then get the init.php script for analyzing
213                                 $initScript = sprintf("%s/init%s", $fqfn, $this->getConfigInstance()->readConfig('php_extension'));
214
215                                 // Load the application's init.php script and append the
216                                 // application to the ArrayObject
217                                 $this->loadInitScript($initScript, $appName);
218
219                         } // END - if ((is_dir(...
220
221                 } // END - while
222
223                 // Close directory pointer
224                 $dirInstance->closeDirectory();
225
226                 // Restore old 'app' from backup
227                 //$app = $appBackup;
228         }
229
230         /**
231          * Load all templates for found applications in previous scan
232          *
233          * @return      void
234          */
235         public function loadApplicationTemplates () {
236                 // Iterate through all applications
237                 for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
238                         // Get current application
239                         $appInstance = $idx->current();
240
241                         // Prepare the template engine for the current template
242                         $templateInstance = $this->prepareTemplateInstance($appInstance);
243
244                         // Try to load the web template
245                         $templateInstance->loadWebTemplate(sprintf("%s_%s",
246                                 $this->getConfigInstance()->readConfig('tpl_selector_prefix'),
247                                 strtolower($appInstance->getAppShortName())
248                         ));
249
250                         // Remember this template and the application for later usage
251                         $this->loadedTemplates->append(array(
252                                 'template_class'   => $templateInstance,
253                                 'app_instance' => $appInstance
254                         ));
255                 }
256
257                 // Re-initialize the application list to avoid double loading
258                 $this->initializeAppsList();
259         }
260
261         /**
262          * Removes $dirIgnoreList from the object to save some memory
263          *
264          * @return      void
265          */
266         public final function removeDirIgnoreList () {
267                 unset($this->dirIgnoreList);
268         }
269
270         /**
271          * Loads the selector's own main template. This step is not linking the
272          * application's templates into the main one.
273          *
274          * @return      void
275          */
276         public function loadSelectorTemplate () {
277                 // Prepare the template engine
278                 $templateInstance = $this->prepareTemplateInstance($this);
279
280                 // Load the selector's template
281                 $templateInstance->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_main_tpl'));
282
283                 // Now store it in the class
284                 $this->setSelectorTemplateEngine($templateInstance);
285         }
286
287         /**
288          * Inserts all loaded application templates into the selector's template
289          *
290          * @return      void
291          * @throws      NullPointerException            If $curr is null
292          * @throws      NoArrayException                        If $curr is not an array
293          * @throws      InvalidArrayCountException      If $curr contains an
294          *                                                                              unexpected count of elements
295          * @throws      MissingArrayElementsException   If $curr is missing expected
296          *                                                                                      array elements
297          */
298         public function insertApplicationTemplates () {
299                 // First prepare the instance
300                 $templateInstance = $this->prepareTemplateInstance($this);
301
302                 // Load template which shall later hold all application templates
303                 $templateInstance->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_apps_tpl'));
304
305                 // Add all loaded application templates together
306                 $dummy = "";
307                 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
308                         // Get current item from array object
309                         $curr = $idx->current();
310
311                         // Do some sanity checks on the loaded item
312                         if (is_null($curr)) {
313                                 // $curr is null
314                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
315                         } elseif (!is_array($curr)) {
316                                 // Not an array
317                                 throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
318                         } elseif (count($curr) != 2) {
319                                 // Not expected count of entries
320                                 throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
321                         } elseif (!isset($curr['template_class']) || (!isset($curr['app_instance']))) {
322                                 // Expected entries missing
323                                 throw new MissingArrayElementsException(array($this, "curr", array("template_class", "app_instance")), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
324                         }
325                         die("<pre>".print_r($curr, true)."</pre>");
326
327                 } // END - for
328         }
329 }
330
331 // [EOF]
332 ?>