class_ApplicationSelector.php

Go to the documentation of this file.
00001 <?php
00024 class ApplicationSelector extends BaseFrameworkSystem {
00028         private $foundApps = null;
00029 
00033         private $loadedTemplates = null;
00034 
00038         private $dirIgnoreList = array(
00039                 ".",
00040                 "..",
00041                 ".htaccess",
00042                 ".svn"
00043         );
00044 
00050         protected function __construct() {
00051                 // Call parent constructor
00052                 parent::__construct(__CLASS__);
00053 
00054                 // Remove system array and thousand seperator
00055                 $this->removeSystemArray();
00056                 $this->removeNumberFormaters();
00057 
00058                 // Initialize the array lists
00059                 $this->initializeAppsList();
00060                 $this->initializeTemplatesList();
00061         }
00062 
00070         public final static function createApplicationSelector (ManageableLanguage $langInstance, FileIoHandler $fileIOInstance) {
00071                 // Get a new instance
00072                 $selInstance = new ApplicationSelector();
00073 
00074                 // Get all applications
00075                 $selInstance->readApplicationDirectory();
00076 
00077                 // Set language and file I/O instances
00078                 $selInstance->setLanguageInstance($langInstance);
00079                 $selInstance->setFileIoInstance($fileIOInstance);
00080 
00081                 // Return the prepared instance
00082                 return $selInstance;
00083         }
00084 
00090         private function initializeAppsList () {
00091                 $this->foundApps = new FrameworkArrayObject("FakedFoundApplications");
00092         }
00093 
00099         private function initializeTemplatesList () {
00100                 $this->loadedTemplates = new FrameworkArrayObject("FakedLoadedTemplates");
00101         }
00102 
00111         private function loadApplicationData ($appData, $appName) {
00112                 // Is it a file and readable?
00113                 if ((is_file($appData)) && (is_readable($appData))) {
00114                         // Then include it
00115                         include ($appData);
00116 
00117                         // Add the current instance to the list
00118                         $this->foundApps->append($app);
00119                 } // END - if ((is_file(...
00120         }
00121 
00128         private final function getLoadedTemplates () {
00129                 return $this->loadedTemplates;
00130         }
00131 
00137         public function getAppShortName() {
00138                 $shortName = $this->getConfigInstance()->readConfig('selector_path');
00139                 return $shortName;
00140         }
00141 
00148         public function addDirIgnoreList ($ignoreItem) {
00149                 // Cast and add it
00150                 $this->dirIgnoreList[] = (string) $ignoreItem;
00151         }
00152 
00159         public function readApplicationDirectory () {
00160                 // Generate the base path for all applications
00161                 $appBasePath = $this->getConfigInstance()->readConfig('application_path');
00162 
00163                 // Add the selector path to the ignore list
00164                 $this->addDirIgnoreList($this->getConfigInstance()->readConfig('selector_path'));
00165 
00166                 // Get a directory pointer for the application path
00167                 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
00168 
00169                 // Read all directories&files except some parts
00170                 while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
00171                         // Generate FQFN for the application name (or better directory name)
00172                         $fqfn = sprintf("%s%s", $appBasePath, $appName);
00173 
00174                         // Is this a readable directory? (files will be ignored silently)
00175                         if ((is_dir($fqfn)) && (is_readable($fqfn))) {
00176                                 // Then get the data.php script for analyzing
00177                                 $appData = sprintf("%s/data.php", $fqfn);
00178 
00179                                 // Load the application's data.php script and append the
00180                                 // application to the ArrayObject
00181                                 $this->loadApplicationData($appData, $appName);
00182                         } // END - if
00183                 } // END - while
00184 
00185                 // Close directory pointer
00186                 $dirInstance->closeDirectory();
00187         }
00188 
00194         public function loadApplicationTemplates () {
00195                 // Iterate through all applications
00196                 for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
00197                         // Get current application
00198                         $appInstance = $idx->current();
00199 
00200                         // Prepare the template engine for the current template
00201                         $templateInstance = $this->prepareTemplateInstance($appInstance);
00202 
00203                         // Try to load the web template
00204                         $templateInstance->loadWebTemplate(sprintf("%s_%s",
00205                                 $this->getConfigInstance()->readConfig('tpl_selector_prefix'),
00206                                 strtolower($appInstance->getAppShortName())
00207                         ));
00208 
00209                         // Remember this template and the application for later usage
00210                         $this->loadedTemplates->append(array(
00211                                 'template_class'   => $templateInstance,
00212                                 'app_instance' => $appInstance
00213                         ));
00214                 }
00215 
00216                 // Re-initialize the application list to avoid double loading
00217                 $this->initializeAppsList();
00218         }
00219 
00225         public final function removeDirIgnoreList () {
00226                 unset($this->dirIgnoreList);
00227         }
00228 
00235         public function loadSelectorTemplate () {
00236                 // Prepare the template engine
00237                 $templateInstance = $this->prepareTemplateInstance($this);
00238 
00239                 // Load the selector's template
00240                 $templateInstance->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_main_tpl'));
00241 
00242                 // Now store it in the class, we need this later on final compilation of available applications
00243                 $this->setTemplateInstance($templateInstance);
00244         }
00245 
00258         public function insertApplicationTemplates () {
00259                 // First prepare the instance
00260                 $templateInstance = $this->prepareTemplateInstance($this);
00261 
00262                 // Load template which shall later hold all application templates
00263                 $templateInstance->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_apps_tpl'));
00264 
00265                 // Add all loaded application templates together
00266                 $dummy = "";
00267                 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
00268                         // Get current item from array object
00269                         $curr = $idx->current();
00270 
00271                         // Do some sanity checks on the loaded item
00272                         if (is_null($curr)) {
00273                                 // $curr is null
00274                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
00275                         } elseif (!is_array($curr)) {
00276                                 // Not an array
00277                                 throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
00278                         } elseif (count($curr) != 2) {
00279                                 // Not expected count of entries
00280                                 throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
00281                         } elseif (!isset($curr['template_class']) || (!isset($curr['app_instance']))) {
00282                                 // Expected entries missing
00283                                 throw new MissingArrayElementsException(array($this, "curr", array("template_class", "app_instance")), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
00284                         }
00285 
00286                         // Debug output
00287                         die(__METHOD__."()<pre>".print_r($curr, true)."</pre>");
00288                 } // END - for
00289         }
00290 }
00291 
00292 // [EOF]
00293 ?>

Generated on Mon Dec 8 01:06:44 2008 for Ship-Simulator by  doxygen 1.5.6