* @version 0.0.0
* @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
class ApplicationSelector extends BaseFrameworkSystem {
/**
* An ArrayObject for all found applications
*/
private $foundApps = null;
/**
* An array object for all loaded application templates (selector_app-name.tpl)
*/
private $loadedTemplates = null;
/**
* The application selector's own template engine handler
*/
private $selectorTplEngine = null;
/**
* A list of items we shall ignore while reading from directories
*/
private $dirIgnoreList = array(
".",
"..",
".htaccess",
".svn"
);
/**
* The protected constructor. No direct instances can be created from this.
*
* @return void
*/
protected function __construct() {
// Call parent constructor
parent::__construct(__CLASS__);
// Set description
$this->setObjectDescription("Applikationswechsler");
// Create unique ID number
$this->generateUniqueId();
// Remove system array and thousand seperator
$this->removeSystemArray();
$this->removeNumberFormaters();
// Initialize the array lists
$this->initializeAppsList();
$this->initializeTemplatesList();
}
/**
* Create a prepared instance of ApplicationSelector
*
* @param $langInstance The language sub-system: LanguageSystem
* @param $fileIOInstance The file I/O instance
* @return $selInstance An instance of ApplicationSelector
*/
public final static function createApplicationSelector (ManageableLanguage $langInstance, FileIoHandler $fileIOInstance) {
// Get a new instance
$selInstance = new ApplicationSelector();
// Get all applications
$selInstance->readApplicationDirectory();
// Set language and file I/O instances
$selInstance->setLanguageInstance($langInstance);
$selInstance->setFileIoInstance($fileIOInstance);
// Return the prepared instance
return $selInstance;
}
/**
* Initialize the application list
*
* @return void
*/
private function initializeAppsList () {
$this->foundApps = new FrameworkArrayObject("FakedFoundApplications");
}
/**
* Initialize the loaded templates list
*
* @return void
*/
private function initializeTemplatesList () {
$this->loadedTemplates = new FrameworkArrayObject("FakedLoadedTemplates");
}
/**
* Load the init.php script of an application and append the application
* instance to $foundApps
*
* @param $initScript The FQFN of init.php
* @param $appName The application's Uni* name
* @return void
*/
private function loadInitScript ($initScript, $appName) {
// Is it a file and readable?
if ((is_file($initScript)) && (is_readable($initScript))) {
// Then include it
include ($initScript);
// Add the current instance to the list
$this->foundApps->append($app);
} // END - if ((is_file(...
}
/**
* Setter for the selector's template engine instance
*
* @param $tplEngine An instance of TemplateEngine
*/
private final function setSelectorTemplateEngine (CompileableTemplate $tplEngine) {
$this->selectorTplEngine = $tplEngine;
}
/**
* Getter for the selector's template engine instance
*
* @return $selectTplEngine The selector's template engine
*/
private final function getSelectorTemplateEngine () {
return $this->selectorTplEngine;
}
/**
* Getter for the $loadedTemplates array object
*
* @return $loadedTemplates An array object holding all loaded
* application templates
*/
private final function getLoadedTemplates () {
return $this->loadedTemplates;
}
/**
* Method for compatiblity with prepareTemplateEngine()
*
* @return $shortName This selector's short name
*/
public function getAppShortName() {
$shortName = $this->getConfigInstance()->readConfig('selector_path');
return $shortName;
}
/**
* Add a directory/file to the ignore list
*
* @param $ignoreItem The file/directory we shall ignore
* @return void
*/
public function addDirIgnoreList ($ignoreItem) {
// Cast and add it
$ignoreItem = (string) $ignoreItem;
$this->dirIgnoreList[] = $ignoreItem;
}
/**
* Read the base path for all applications (application/) and create a
* list of all found applications
*
* @return void
*/
public function readApplicationDirectory () {
// Generate the base path for all applications
$appBasePath = sprintf("%s%s/",
PATH,
$this->getConfigInstance()->readConfig('application_path')
);
// Add the selector path to the ignore list
$this->addDirIgnoreList($this->getConfigInstance()->readConfig('selector_path'));
// Get a directory pointer for the application path
$dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
// Backup and remove the 'app' from global name space
/*$appBackup = $app;
unset($app);*/
// Read all directories&files except some parts
while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
// Generate FQFN for the application name (or better directory name)
$fqfn = sprintf("%s%s", $appBasePath, $appName);
// Is this a readable directory? (files will be ignored silently)
if ((is_dir($fqfn)) && (is_readable($fqfn))) {
// Then get the init.php script for analyzing
$initScript = sprintf("%s/init%s", $fqfn, $this->getConfigInstance()->readConfig('php_extension'));
// Load the application's init.php script and append the
// application to the ArrayObject
$this->loadInitScript($initScript, $appName);
} // END - if ((is_dir(...
} // END - while
// Close directory pointer
$dirInstance->closeDirectory();
// Restore old 'app' from backup
//$app = $appBackup;
}
/**
* Load all templates for found applications in previous scan
*
* @return void
*/
public function loadApplicationTemplates () {
// Iterate through all applications
for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
// Get current application
$appInstance = $idx->current();
// Prepare the template engine for the current template
$tplEngine = $this->prepareTemplateEngine($appInstance);
// Try to load the web template
$tplEngine->loadWebTemplate(sprintf("%s_%s",
$this->getConfigInstance()->readConfig('tpl_selector_prefix'),
strtolower($appInstance->getAppShortName())
));
// Remember this template and the application for later usage
$this->loadedTemplates->append(array(
'template_class' => $tplEngine,
'app_instance' => $appInstance
));
}
// Re-initialize the application list to avoid double loading
$this->initializeAppsList();
}
/**
* Removes $dirIgnoreList from the object to save some memory
*
* @return void
*/
public final function removeDirIgnoreList () {
unset($this->dirIgnoreList);
}
/**
* Loads the selector's own main template. This step is not linking the
* application's templates into the main one.
*
* @return void
*/
public function loadSelectorTemplate () {
// Prepare the template engine
$tplEngine = $this->prepareTemplateEngine($this);
// Load the selector's template
$tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_main_tpl'));
// Now store it in the class
$this->setSelectorTemplateEngine($tplEngine);
}
/**
* Inserts all loaded application templates into the selector's template
*
* @return void
* @throws NullPointerException If $curr is null
* @throws NoArrayException If $curr is not an array
* @throws InvalidArrayCountException If $curr contains an
* unexpected count of elements
* @throws MissingArrayElementsException If $curr is missing expected
* array elements
*/
public function insertApplicationTemplates () {
// First prepare the instance
$tplEngine = $this->prepareTemplateEngine($this);
// Load template which shall later hold all application templates
$tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_apps_tpl'));
// Add all loaded application templates together
$dummy = "";
for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
// Get current item from array object
$curr = $idx->current();
// Do some sanity checks on the loaded item
if (is_null($curr)) {
// $curr is null
throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
} elseif (!is_array($curr)) {
// Not an array
throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
} elseif (count($curr) != 2) {
// Not expected count of entries
throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
} elseif (!isset($curr['template_class']) || (!isset($curr['app_instance']))) {
// Expected entries missing
throw new MissingArrayElementsException(array($this, "curr", array("template_class", "app_instance")), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
}
die("
".print_r($curr, true)."
");
} // END - for
}
}
// [EOF]
?>