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