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