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(
53 * The language instance for the template loader
55 private $langInstance = null;
58 * The file I/O instance for the template loader
60 private $fileIOInstance = null;
63 * The private constructor. No direct instances can be created from this.
67 private function __construct() {
68 // Call parent constructor
69 parent::constructor(__CLASS__);
72 $this->setPartDescr("Applikationswechsler");
74 // Create unique ID number
75 $this->createUniqueID();
77 // Remove system array and thousand seperator
78 $this->removeSystemArray();
79 $this->removeNumberFormaters();
81 // Initialize the array lists
82 $this->initializeAppsList();
83 $this->initializeTemplatesList();
87 * Create a prepared instance of ApplicationSelector
89 * @param $langInstance The language sub-system: LanguageSystem
90 * @param $fileIOInstance The file I/O instance
91 * @return $selInstance An instance of ApplicationSelector
93 public static function createApplicationSelector (ManageableLanguage $langInstance, FileIOHandler $fileIOInstance) {
95 $selInstance = new ApplicationSelector();
97 // Get all applications
98 $selInstance->readApplicationDirectory();
100 // Set language and file I/O instances
101 $selInstance->setLanguageInstance($langInstance);
102 $selInstance->setFileIOInstance($fileIOInstance);
104 // Return the prepared instance
109 * Initialize the application list
113 private function initializeAppsList () {
114 $this->foundApps = new FrameworkArrayObject();
118 * Initialize the loaded templates list
122 private function initializeTemplatesList () {
123 $this->loadedTemplates = new FrameworkArrayObject();
127 * Private getter for language instance
129 * @return $langInstance An instance to the language sub-system
131 private function getLanguageInstance () {
132 return $this->langInstance;
136 * Private getter for file IO instance
138 * @return $fileIOInstance An instance to the file I/O sub-system
140 private function getFileIOInstance () {
141 return $this->fileIOInstance;
145 * Prepare the template engine (TemplateEngine by default) for a give
146 * application helper instance (ApplicationHelper by default).
148 * @param $appInstance An application helper instance
149 * @return $tplEngine The template engine instance
150 * @throws NullPointerException If the template engine could not
152 * @throws UnsupportedTemplateEngineException If $tplEngine is an
153 * unsupported template engine
155 private function prepareTemplateEngine (BaseFrameworkSystem $appInstance) {
156 // Generate FQFN for all application templates
157 $fqfn = sprintf("%s%s/%s/%s",
159 $this->getConfigInstance()->readConfig("application_path"),
160 strtolower($appInstance->getAppShortName()),
161 $this->getConfigInstance()->readConfig("tpl_base_path")
164 // Initialize the template engine
166 $eval = sprintf("\$tplEngine = %s::create%s(
168 \$this->getLanguageInstance(),
169 \$this->getFileIOInstance()
171 $this->getConfigInstance()->readConfig("tpl_engine"),
172 $this->getConfigInstance()->readConfig("tpl_engine"),
177 if ((!is_null($this->getDebugInstance())) && (defined('DEBUG_EVAL'))) {
178 $this->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
187 // Is it a valid instance?
188 if (is_null($tplEngine)) {
190 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
191 } elseif (!$tplEngine instanceof CompileableTemplate) {
192 // Not an object! ;-(
193 throw new UnsupportedTemplateEngineException($tplEngine, self::EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED);
196 // Return the prepared instance
201 * Load the init.php script of an application and append the application
202 * instance to $foundApps
204 * @param $initScript The FQFN of init.php
205 * @param $appName The application's Uni* name
207 * @throws AppVarIsNotSetException If 'app' is not set
208 * @throws NullPointerException If 'app' is null
209 * @throws NoObjectException If 'app' is not an object
210 * @throws MissingMethodException If a required method is missing
212 private function loadInitScript ($initScript, $appName) {
213 // Is it a file and readable?
214 if ((is_file($initScript)) && (is_readable($initScript))) {
216 include ($initScript);
218 // We now should have $app re-defined!
220 // This application shall not be loaded
222 } elseif (is_null($app)) {
223 // The class instance is null
224 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
225 } elseif (!is_object($app)) {
227 throw new NoObjectException($app, self::EXCEPTION_IS_NO_OBJECT);
228 } elseif (!method_exists($app, $this->getConfigInstance()->readConfig("entry_method"))) {
230 throw new MissingMethodException(array($app, $this->getConfigInstance()->readConfig("entry_method")), self::EXCEPTION_MISSING_METHOD);
233 // Add the current instance to the list
234 $this->foundApps->append($app);
236 } // END - if ((is_file(...
240 * Setter for the selector's template engine instance
242 * @param $tplEngine An instance of TemplateEngine
244 private function setSelectorTemplateEngine (CompileableTemplate $tplEngine) {
245 $this->selectorTplEngine = $tplEngine;
249 * Getter for the selector's template engine instance
251 * @return $selectTplEngine The selector's template engine
253 private function getSelectorTemplateEngine () {
254 return $this->selectorTplEngine;
258 * Getter for the $loadedTemplates array object
260 * @return $loadedTemplates An array object holding all loaded
261 * application templates
263 private function getLoadedTemplates () {
264 return $this->loadedTemplates;
268 * Method for compatiblity with prepareTemplateEngine()
270 * @return $shortName This selector's short name
272 private function getAppShortName() {
273 $shortName = $this->getConfigInstance()->readConfig("selector_path");
278 * Add a directory/file to the ignore list
280 * @param $ignoreItem The file/directory we shall ignore
283 public function addDirIgnoreList ($ignoreItem) {
285 $ignoreItem = (string) $ignoreItem;
286 $this->dirIgnoreList[] = $ignoreItem;
290 * Setter for language instance
292 * @param $langInstance An instance to the language sub-system
294 * @see LanguageSystem
296 public final function setLanguageInstance (ManageableLanguage $langInstance) {
297 $this->langInstance = $langInstance;
301 * Setter for file I/O instance
303 * @param $fileIOInstance An instance to the file I/O sub-system
306 public final function setFileIOInstance (FileIOHandler $fileIOInstance) {
307 $this->fileIOInstance = $fileIOInstance;
311 * Read the base path for all applications (application/) and create a
312 * list of all found applications
316 public function readApplicationDirectory () {
317 // Generate the base path for all applications
318 $appBasePath = sprintf("%s%s/",
320 $this->getConfigInstance()->readConfig("application_path")
323 // Add the selector path to the ignore list
324 $this->addDirIgnoreList($this->getConfigInstance()->readConfig("selector_path"));
326 // Get a directory pointer for the application path
327 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
329 // Backup and remove the 'app' from global name space
333 // Read all directories&files except some parts
334 while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
335 // Generate FQFN for the application name (or better directory name)
336 $fqfn = sprintf("%s%s", $appBasePath, $appName);
338 // Is this a readable directory? (files will be ignored silently)
339 if ((is_dir($fqfn)) && (is_readable($fqfn))) {
340 // Then get the init.php script for analyzing
341 $initScript = sprintf("%s/init%s", $fqfn, $this->getConfigInstance()->readConfig("php_extension"));
343 // Load the application's init.php script and append the
344 // application to the ArrayObject
345 $this->loadInitScript($initScript, $appName);
347 } // END - if ((is_dir(...
351 // Close directory pointer
352 $dirInstance->closeDirectory();
354 // Restore old 'app' from backup
359 * Load all templates for found applications in previous scan
363 public function loadApplicationTemplates () {
364 // Iterate through all applications
365 for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
366 // Get current application
367 $appInstance = $idx->current();
369 // Prepare the template engine for the current template
370 $tplEngine = $this->prepareTemplateEngine($appInstance);
372 // Try to load the web template
373 $tplEngine->loadWebTemplate(sprintf("%s_%s",
374 $this->getConfigInstance()->readConfig("tpl_selector_prefix"),
375 strtolower($appInstance->getAppShortName())
378 // Remember this template and the application for later usage
379 $this->loadedTemplates->append(array(
380 'tpl_engine' => $tplEngine,
381 'app_instance' => $appInstance
385 // Re-initialize the application list to avoid double loading
386 $this->initializeAppsList();
390 * Removes $dirIgnoreList from the object to save some memory
394 public function removeDirIgnoreList () {
395 unset($this->dirIgnoreList);
399 * Loads the selector's own main template. This step is not linking the
400 * application's templates into the main one.
404 public function loadSelectorTemplate () {
405 // Prepare the template engine
406 $tplEngine = $this->prepareTemplateEngine($this);
408 // Load the selector's template
409 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig("selector_main_tpl"));
411 // Now store it in the class
412 $this->setSelectorTemplateEngine($tplEngine);
416 * Inserts all loaded application templates into the selector's template
419 * @throws NullPointerException If $curr is null
420 * @throws NoArrayException If $curr is not an array
421 * @throws InvalidArrayCountException If $curr contains an
422 * unexpected count of elements
423 * @throws MissingArrayElementsException If $curr is missing expected
426 public function insertApplicationTemplates () {
427 // First prepare the instance
428 $tplEngine = $this->prepareTemplateEngine($this);
430 // Load template which shall later hold all application templates
431 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig("selector_apps_tpl"));
433 // Add all loaded application templates together
435 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
436 // Get current item from array object
437 $curr = $idx->current();
439 // Do some sanity checks on the loaded item
440 if (is_null($curr)) {
442 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
443 } elseif (!is_array($curr)) {
445 throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
446 } elseif (count($curr) != 2) {
447 // Not expected count of entries
448 throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
449 } elseif (!isset($curr['tpl_engine']) || (!isset($curr['app_instance']))) {
450 // Expected entries missing
451 throw new MissingArrayElementsException(array($this, "curr", array('tpl_engine', 'app_instance')), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
453 die("<pre>".print_r($curr, true)."</pre>");
461 public function saveObjectToDatabase () {
462 $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",