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 * The application selector's own template engine handler
41 private $selectorTplEngine = null;
44 * A list of items we shall ignore while reading from directories
46 private $dirIgnoreList = array(
54 * The protected constructor. No direct instances can be created from this.
58 protected function __construct() {
59 // Call parent constructor
60 parent::__construct(__CLASS__);
63 $this->setObjectDescription("Applikationswechsler");
65 // Create unique ID number
66 $this->generateUniqueId();
68 // Remove system array and thousand seperator
69 $this->removeSystemArray();
70 $this->removeNumberFormaters();
72 // Initialize the array lists
73 $this->initializeAppsList();
74 $this->initializeTemplatesList();
78 * Create a prepared instance of ApplicationSelector
80 * @param $langInstance The language sub-system: LanguageSystem
81 * @param $fileIOInstance The file I/O instance
82 * @return $selInstance An instance of ApplicationSelector
84 public final static function createApplicationSelector (ManageableLanguage $langInstance, FileIoHandler $fileIOInstance) {
86 $selInstance = new ApplicationSelector();
88 // Get all applications
89 $selInstance->readApplicationDirectory();
91 // Set language and file I/O instances
92 $selInstance->setLanguageInstance($langInstance);
93 $selInstance->setFileIoInstance($fileIOInstance);
95 // Return the prepared instance
100 * Initialize the application list
104 private function initializeAppsList () {
105 $this->foundApps = new FrameworkArrayObject("FakedFoundApplications");
109 * Initialize the loaded templates list
113 private function initializeTemplatesList () {
114 $this->loadedTemplates = new FrameworkArrayObject("FakedLoadedTemplates");
118 * Load the init.php script of an application and append the application
119 * instance to $foundApps
121 * @param $initScript The FQFN of init.php
122 * @param $appName The application's Uni* name
125 private function loadInitScript ($initScript, $appName) {
126 // Is it a file and readable?
127 if ((is_file($initScript)) && (is_readable($initScript))) {
129 include ($initScript);
131 // Add the current instance to the list
132 $this->foundApps->append($app);
134 } // END - if ((is_file(...
138 * Setter for the selector's template engine instance
140 * @param $templateInstance An instance of TemplateEngine
143 private final function setSelectorTemplateEngine (CompileableTemplate $templateInstance) {
144 $this->selectorTplEngine = $templateInstance;
148 * Getter for the selector's template engine instance
150 * @return $selectTplEngine The selector's template engine
152 private final function getSelectorTemplateEngine () {
153 return $this->selectorTplEngine;
157 * Getter for the $loadedTemplates array object
159 * @return $loadedTemplates An array object holding all loaded
160 * application templates
162 private final function getLoadedTemplates () {
163 return $this->loadedTemplates;
167 * Method for compatiblity with prepareTemplateInstance()
169 * @return $shortName This selector's short name
171 public function getAppShortName() {
172 $shortName = $this->getConfigInstance()->readConfig('selector_path');
177 * Add a directory/file to the ignore list
179 * @param $ignoreItem The file/directory we shall ignore
182 public function addDirIgnoreList ($ignoreItem) {
184 $ignoreItem = (string) $ignoreItem;
185 $this->dirIgnoreList[] = $ignoreItem;
189 * Read the base path for all applications (application/) and create a
190 * list of all found applications
194 public function readApplicationDirectory () {
195 // Generate the base path for all applications
196 $appBasePath = sprintf("%s%s/",
198 $this->getConfigInstance()->readConfig('application_path')
201 // Add the selector path to the ignore list
202 $this->addDirIgnoreList($this->getConfigInstance()->readConfig('selector_path'));
204 // Get a directory pointer for the application path
205 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
207 // Backup and remove the 'app' from global name space
211 // Read all directories&files except some parts
212 while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
213 // Generate FQFN for the application name (or better directory name)
214 $fqfn = sprintf("%s%s", $appBasePath, $appName);
216 // Is this a readable directory? (files will be ignored silently)
217 if ((is_dir($fqfn)) && (is_readable($fqfn))) {
218 // Then get the init.php script for analyzing
219 $initScript = sprintf("%s/init%s", $fqfn, $this->getConfigInstance()->readConfig('php_extension'));
221 // Load the application's init.php script and append the
222 // application to the ArrayObject
223 $this->loadInitScript($initScript, $appName);
225 } // END - if ((is_dir(...
229 // Close directory pointer
230 $dirInstance->closeDirectory();
232 // Restore old 'app' from backup
237 * Load all templates for found applications in previous scan
241 public function loadApplicationTemplates () {
242 // Iterate through all applications
243 for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
244 // Get current application
245 $appInstance = $idx->current();
247 // Prepare the template engine for the current template
248 $templateInstance = $this->prepareTemplateInstance($appInstance);
250 // Try to load the web template
251 $templateInstance->loadWebTemplate(sprintf("%s_%s",
252 $this->getConfigInstance()->readConfig('tpl_selector_prefix'),
253 strtolower($appInstance->getAppShortName())
256 // Remember this template and the application for later usage
257 $this->loadedTemplates->append(array(
258 'template_class' => $templateInstance,
259 'app_instance' => $appInstance
263 // Re-initialize the application list to avoid double loading
264 $this->initializeAppsList();
268 * Removes $dirIgnoreList from the object to save some memory
272 public final function removeDirIgnoreList () {
273 unset($this->dirIgnoreList);
277 * Loads the selector's own main template. This step is not linking the
278 * application's templates into the main one.
282 public function loadSelectorTemplate () {
283 // Prepare the template engine
284 $templateInstance = $this->prepareTemplateInstance($this);
286 // Load the selector's template
287 $templateInstance->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_main_tpl'));
289 // Now store it in the class
290 $this->setSelectorTemplateEngine($templateInstance);
294 * Inserts all loaded application templates into the selector's template
297 * @throws NullPointerException If $curr is null
298 * @throws NoArrayException If $curr is not an array
299 * @throws InvalidArrayCountException If $curr contains an
300 * unexpected count of elements
301 * @throws MissingArrayElementsException If $curr is missing expected
304 public function insertApplicationTemplates () {
305 // First prepare the instance
306 $templateInstance = $this->prepareTemplateInstance($this);
308 // Load template which shall later hold all application templates
309 $templateInstance->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_apps_tpl'));
311 // Add all loaded application templates together
313 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
314 // Get current item from array object
315 $curr = $idx->current();
317 // Do some sanity checks on the loaded item
318 if (is_null($curr)) {
320 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
321 } elseif (!is_array($curr)) {
323 throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
324 } elseif (count($curr) != 2) {
325 // Not expected count of entries
326 throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
327 } elseif (!isset($curr['template_class']) || (!isset($curr['app_instance']))) {
328 // Expected entries missing
329 throw new MissingArrayElementsException(array($this, "curr", array("template_class", "app_instance")), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
331 die("<pre>".print_r($curr, true)."</pre>");