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 * @todo Extend a later BaseFrameworkApplication class
9 * @author Roland Haeder <webmaster@ship-simu.org>
11 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
12 * @license GNU GPL 3.0 or any newer version
13 * @link http://www.ship-simu.org
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 class ApplicationSelector extends BaseFrameworkSystem {
30 * An ArrayObject for all found applications
32 private $foundApps = null;
35 * An array object for all loaded application templates (selector_app-name.tpl)
37 private $loadedTemplates = null;
40 * The application selector's own template engine handler
42 private $selectorTplEngine = null;
45 * A list of items we shall ignore while reading from directories
47 private $dirIgnoreList = array(
54 * The language instance for the template loader
56 private $langInstance = null;
59 * The file I/O instance for the template loader
61 private $fileIOInstance = null;
64 * The private constructor. No direct instances can be created from this.
68 private function __construct() {
69 // Call parent constructor
70 parent::constructor(__CLASS__);
73 $this->setPartDescr("Applikationswechsler");
75 // Create unique ID number
76 $this->createUniqueID();
78 // Remove system array and thousand seperator
79 $this->removeSystemArray();
80 $this->removeNumberFormaters();
82 // Initialize the array lists
83 $this->initializeAppsList();
84 $this->initializeTemplatesList();
88 * Create a prepared instance of ApplicationSelector
90 * @param $langInstance The language sub-system: LanguageSystem
91 * @param $fileIOInstance The file I/O instance
92 * @return $selInstance An instance of ApplicationSelector
94 public static function createApplicationSelector (ManageableLanguage $langInstance, FileIOHandler $fileIOInstance) {
96 $selInstance = new ApplicationSelector();
98 // Get all applications
99 $selInstance->readApplicationDirectory();
101 // Set language and file I/O instances
102 $selInstance->setLanguageInstance($langInstance);
103 $selInstance->setFileIOInstance($fileIOInstance);
105 // Return the prepared instance
110 * Initialize the application list
114 private function initializeAppsList () {
115 $this->foundApps = new FrameworkArrayObject();
119 * Initialize the loaded templates list
123 private function initializeTemplatesList () {
124 $this->loadedTemplates = new FrameworkArrayObject();
128 * Private getter for language instance
130 * @return $langInstance An instance to the language sub-system
132 private function getLanguageInstance () {
133 return $this->langInstance;
137 * Private getter for file IO instance
139 * @return $fileIOInstance An instance to the file I/O sub-system
141 private function getFileIOInstance () {
142 return $this->fileIOInstance;
146 * Prepare the template engine (TemplateEngine by default) for a give
147 * application helper instance (ApplicationHelper by default).
149 * @param $appInstance An application helper instance
150 * @return $tplEngine The template engine instance
151 * @throws NullPointerException If the template engine could not
153 * @throws UnsupportedTemplateEngineException If $tplEngine is an
154 * unsupported template engine
156 private function prepareTemplateEngine (BaseFrameworkSystem $appInstance) {
157 // Generate FQFN for all application templates
158 $fqfn = sprintf("%s%s/%s/%s",
160 $this->getConfigInstance()->readConfig("application_path"),
161 strtolower($appInstance->getAppShortName()),
162 $this->getConfigInstance()->readConfig("tpl_base_path")
165 // Initialize the template engine
167 $eval = sprintf("\$tplEngine = %s::create%s(
169 \$this->getLanguageInstance(),
170 \$this->getFileIOInstance()
172 $this->getConfigInstance()->readConfig("tpl_engine"),
173 $this->getConfigInstance()->readConfig("tpl_engine"),
178 if ((!is_null($this->getDebugInstance())) && (defined('DEBUG_EVAL'))) {
179 $this->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
188 // Is it a valid instance?
189 if (is_null($tplEngine)) {
191 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
192 } elseif (!$tplEngine instanceof CompileableTemplate) {
193 // Not an object! ;-(
194 throw new UnsupportedTemplateEngineException($tplEngine, self::EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED);
197 // Return the prepared instance
202 * Load the init.php script of an application and append the application
203 * instance to $foundApps
205 * @param $initScript The FQFN of init.php
206 * @param $appName The application's Uni* name
208 * @throws AppVarIsNotSetException If 'app' is not set
209 * @throws NullPointerException If 'app' is null
210 * @throws NoObjectException If 'app' is not an object
211 * @throws MissingMethodException If a required method is missing
213 private function loadInitScript ($initScript, $appName) {
214 // Is it a file and readable?
215 if ((is_file($initScript)) && (is_readable($initScript))) {
217 include ($initScript);
219 // We now should have $app re-defined!
221 // This application shall not be loaded
223 } elseif (is_null($app)) {
224 // The class instance is null
225 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
226 } elseif (!is_object($app)) {
228 throw new NoObjectException($app, self::EXCEPTION_IS_NO_OBJECT);
229 } elseif (!method_exists($app, $this->getConfigInstance()->readConfig("entry_method"))) {
231 throw new MissingMethodException(array($app, $this->getConfigInstance()->readConfig("entry_method")), self::EXCEPTION_MISSING_METHOD);
234 // Add the current instance to the list
235 $this->foundApps->append($app);
237 } // END - if ((is_file(...
241 * Setter for the selector's template engine instance
243 * @param $tplEngine An instance of TemplateEngine
245 private function setSelectorTemplateEngine (CompileableTemplate $tplEngine) {
246 $this->selectorTplEngine = $tplEngine;
250 * Getter for the selector's template engine instance
252 * @return $selectTplEngine The selector's template engine
254 private function getSelectorTemplateEngine () {
255 return $this->selectorTplEngine;
259 * Getter for the $loadedTemplates array object
261 * @return $loadedTemplates An array object holding all loaded
262 * application templates
264 private function getLoadedTemplates () {
265 return $this->loadedTemplates;
269 * Method for compatiblity with prepareTemplateEngine()
271 * @return $shortName This selector's short name
273 private function getAppShortName() {
274 $shortName = $this->getConfigInstance()->readConfig("selector_path");
279 * Add a directory/file to the ignore list
281 * @param $ignoreItem The file/directory we shall ignore
284 public function addDirIgnoreList ($ignoreItem) {
286 $ignoreItem = (string) $ignoreItem;
287 $this->dirIgnoreList[] = $ignoreItem;
291 * Setter for language instance
293 * @param $langInstance An instance to the language sub-system
295 * @see LanguageSystem
297 public final function setLanguageInstance (ManageableLanguage $langInstance) {
298 $this->langInstance = $langInstance;
302 * Setter for file I/O instance
304 * @param $fileIOInstance An instance to the file I/O sub-system
307 public final function setFileIOInstance (FileIOHandler $fileIOInstance) {
308 $this->fileIOInstance = $fileIOInstance;
312 * Read the base path for all applications (application/) and create a
313 * list of all found applications
317 public function readApplicationDirectory () {
318 // Generate the base path for all applications
319 $appBasePath = sprintf("%s%s/",
321 $this->getConfigInstance()->readConfig("application_path")
324 // Add the selector path to the ignore list
325 $this->addDirIgnoreList($this->getConfigInstance()->readConfig("selector_path"));
327 // Get a directory pointer for the application path
328 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
330 // Backup and remove the 'app' from global name space
334 // Read all directories&files except some parts
335 while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
336 // Generate FQFN for the application name (or better directory name)
337 $fqfn = sprintf("%s%s", $appBasePath, $appName);
339 // Is this a readable directory? (files will be ignored silently)
340 if ((is_dir($fqfn)) && (is_readable($fqfn))) {
341 // Then get the init.php script for analyzing
342 $initScript = sprintf("%s/init%s", $fqfn, $this->getConfigInstance()->readConfig("php_extension"));
344 // Load the application's init.php script and append the
345 // application to the ArrayObject
346 $this->loadInitScript($initScript, $appName);
348 } // END - if ((is_dir(...
352 // Close directory pointer
353 $dirInstance->closeDirectory();
355 // Restore old 'app' from backup
360 * Load all templates for found applications in previous scan
364 public function loadApplicationTemplates () {
365 // Iterate through all applications
366 for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
367 // Get current application
368 $appInstance = $idx->current();
370 // Prepare the template engine for the current template
371 $tplEngine = $this->prepareTemplateEngine($appInstance);
373 // Try to load the web template
374 $tplEngine->loadWebTemplate(sprintf("%s_%s",
375 $this->getConfigInstance()->readConfig("tpl_selector_prefix"),
376 strtolower($appInstance->getAppShortName())
379 // Remember this template and the application for later usage
380 $this->loadedTemplates->append(array(
381 'tpl_engine' => $tplEngine,
382 'app_instance' => $appInstance
386 // Re-initialize the application list to avoid double loading
387 $this->initializeAppsList();
391 * Removes $dirIgnoreList from the object to save some memory
395 public function removeDirIgnoreList () {
396 unset($this->dirIgnoreList);
400 * Loads the selector's own main template. This step is not linking the
401 * application's templates into the main one.
405 public function loadSelectorTemplate () {
406 // Prepare the template engine
407 $tplEngine = $this->prepareTemplateEngine($this);
409 // Load the selector's template
410 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig("selector_main_tpl"));
412 // Now store it in the class
413 $this->setSelectorTemplateEngine($tplEngine);
417 * Inserts all loaded application templates into the selector's template
420 * @throws NullPointerException If $curr is null
421 * @throws NoArrayException If $curr is not an array
422 * @throws InvalidArrayCountException If $curr contains an
423 * unexpected count of elements
424 * @throws MissingArrayElementsException If $curr is missing expected
427 public function insertApplicationTemplates () {
428 // First prepare the instance
429 $tplEngine = $this->prepareTemplateEngine($this);
431 // Load template which shall later hold all application templates
432 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig("selector_apps_tpl"));
434 // Add all loaded application templates together
436 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
437 // Get current item from array object
438 $curr = $idx->current();
440 // Do some sanity checks on the loaded item
441 if (is_null($curr)) {
443 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
444 } elseif (!is_array($curr)) {
446 throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
447 } elseif (count($curr) != 2) {
448 // Not expected count of entries
449 throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
450 } elseif (!isset($curr['tpl_engine']) || (!isset($curr['app_instance']))) {
451 // Expected entries missing
452 throw new MissingArrayElementsException(array($this, "curr", array('tpl_engine', 'app_instance')), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
454 die("<pre>".print_r($curr, true)."</pre>");
462 public function saveObjectToDatabase () {
463 $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",