3 * The application selector class.
5 * Please remember that this include file is being loaded *before* the class
6 * loader is loading classes from "exceptions", "interfaces" and "main"!
8 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
27 class ApplicationSelector extends BaseFrameworkSystem {
29 * An ArrayObject for all found applications
31 private $foundApps = null;
34 * An array object for all loaded application templates (selector_app-name.tpl)
36 private $loadedTemplates = null;
39 * A list of items we shall ignore while reading from directories
41 private $dirIgnoreList = array(
49 * The protected constructor. No direct instances can be created from this.
53 protected function __construct() {
54 // Call parent constructor
55 parent::__construct(__CLASS__);
57 // Remove system array and thousand seperator
58 $this->removeSystemArray();
59 $this->removeNumberFormaters();
61 // Initialize the array lists
62 $this->initializeAppsList();
63 $this->initializeTemplatesList();
67 * Create a prepared instance of ApplicationSelector
69 * @param $langInstance The language sub-system: LanguageSystem
70 * @param $fileIOInstance The file I/O instance
71 * @return $selInstance An instance of ApplicationSelector
73 public final static function createApplicationSelector (ManageableLanguage $langInstance, FileIoHandler $fileIOInstance) {
75 $selInstance = new ApplicationSelector();
77 // Get all applications
78 $selInstance->readApplicationDirectory();
80 // Set language and file I/O instances
81 $selInstance->setLanguageInstance($langInstance);
82 $selInstance->setFileIoInstance($fileIOInstance);
84 // Return the prepared instance
89 * Initialize the application list
93 private function initializeAppsList () {
94 $this->foundApps = new FrameworkArrayObject("FakedFoundApplications");
98 * Initialize the loaded templates list
102 private function initializeTemplatesList () {
103 $this->loadedTemplates = new FrameworkArrayObject("FakedLoadedTemplates");
107 * Load the data.php script of an application and append the application
108 * instance to $foundApps
110 * @param $appData The FQFN of data.php
111 * @param $appName The application's Uni* name
114 private function loadApplicationData ($appData, $appName) {
115 // Is it a file and readable?
116 if ((is_file($appData)) && (is_readable($appData))) {
120 // Add the current instance to the list
121 $this->foundApps->append($app);
122 } // END - if ((is_file(...
126 * Getter for the $loadedTemplates array object
128 * @return $loadedTemplates An array object holding all loaded
129 * application templates
131 private final function getLoadedTemplates () {
132 return $this->loadedTemplates;
136 * Method for compatiblity with prepareTemplateInstance()
138 * @return $shortName This selector's short name
140 public function getAppShortName() {
141 $shortName = $this->getConfigInstance()->readConfig('selector_path');
146 * Add a directory/file to the ignore list
148 * @param $ignoreItem The file/directory we shall ignore
151 public function addDirIgnoreList ($ignoreItem) {
153 $this->dirIgnoreList[] = (string) $ignoreItem;
157 * Read the base path for all applications (application/) and create a
158 * list of all found applications
162 public function readApplicationDirectory () {
163 // Generate the base path for all applications
164 $appBasePath = $this->getConfigInstance()->readConfig('application_path');
166 // Add the selector path to the ignore list
167 $this->addDirIgnoreList($this->getConfigInstance()->readConfig('selector_path'));
169 // Get a directory pointer for the application path
170 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
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);
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);
182 // Load the application's data.php script and append the
183 // application to the ArrayObject
184 $this->loadApplicationData($appData, $appName);
188 // Close directory pointer
189 $dirInstance->closeDirectory();
193 * Load all templates for found applications in previous scan
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();
203 // Prepare the template engine for the current template
204 $templateInstance = $this->prepareTemplateInstance($appInstance);
206 // Try to load the web template
207 $templateInstance->loadWebTemplate(sprintf("%s_%s",
208 $this->getConfigInstance()->readConfig('tpl_selector_prefix'),
209 strtolower($appInstance->getAppShortName())
212 // Remember this template and the application for later usage
213 $this->loadedTemplates->append(array(
214 'template_class' => $templateInstance,
215 'app_instance' => $appInstance
219 // Re-initialize the application list to avoid double loading
220 $this->initializeAppsList();
224 * Removes $dirIgnoreList from the object to save some memory
228 public final function removeDirIgnoreList () {
229 unset($this->dirIgnoreList);
233 * Loads the selector's own main template. This step is not linking the
234 * application's templates into the main one.
238 public function loadSelectorTemplate () {
239 // Prepare the template engine
240 $templateInstance = $this->prepareTemplateInstance($this);
242 // Load the selector's template
243 $templateInstance->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_main_tpl'));
245 // Now store it in the class, we need this later on final compilation of available applications
246 $this->setTemplateInstance($templateInstance);
250 * Inserts all loaded application templates into the selector's template
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
259 * @todo Finish handling all applications here
261 public function insertApplicationTemplates () {
262 // First prepare the instance
263 $templateInstance = $this->prepareTemplateInstance($this);
265 // Load template which shall later hold all application templates
266 $templateInstance->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_apps_tpl'));
268 // Add all loaded application templates together
270 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
271 // Get current item from array object
272 $curr = $idx->current();
274 // Do some sanity checks on the loaded item
275 if (is_null($curr)) {
277 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
278 } elseif (!is_array($curr)) {
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);
290 die(__METHOD__."()<pre>".print_r($curr, true)."</pre>");