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