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 NoObjectException If $tplEngine is not an object
154 * @throws MissingMethodException If $tplEngine misses a required
155 * method 'loadWebTemplate()'
157 private function prepareTemplateEngine (BaseFrameworkSystem $appInstance) {
158 // Generate FQFN for all application templates
159 $fqfn = sprintf("%s%s/%s/%s",
161 $this->getConfigInstance()->readConfig("application_path"),
162 strtolower($appInstance->getAppShortName()),
163 $this->getConfigInstance()->readConfig("tpl_base_path")
166 // Initialize the template engine
168 $eval = sprintf("\$tplEngine = %s::create%s(
170 \$this->getLanguageInstance(),
171 \$this->getFileIOInstance()
173 $this->getConfigInstance()->readConfig("tpl_engine"),
174 $this->getConfigInstance()->readConfig("tpl_engine"),
179 if ((!is_null($this->getDebugInstance())) && (defined('DEBUG_EVAL'))) {
180 $this->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
189 // Is it a valid instance?
190 if (is_null($tplEngine)) {
192 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
193 } elseif (!is_object($tplEngine)) {
194 // Not an object! ;-(
195 throw new NoObjectException($tplEngine, self::EXCEPTION_IS_NO_OBJECT);
196 } elseif (!method_exists($tplEngine, 'loadWebTemplate')) {
197 // Nope, so throw exception
198 throw new MissingMethodException(array($tplEngine, 'loadWebTemplate'), self::EXCEPTION_MISSING_METHOD);
201 // Return the prepared instance
206 * Load the init.php script of an application and append the application
207 * instance to $foundApps
209 * @param $initScript The FQFN of init.php
210 * @param $appName The application's Uni* name
212 * @throws AppVarIsNotSetException If 'app' is not set
213 * @throws NullPointerException If 'app' is null
214 * @throws NoObjectException If 'app' is not an object
215 * @throws MissingMethodException If a required method is missing
217 private function loadInitScript ($initScript, $appName) {
218 // Is it a file and readable?
219 if ((is_file($initScript)) && (is_readable($initScript))) {
221 include ($initScript);
223 // We now should have $app re-defined!
225 // This application shall not be loaded
227 } elseif (is_null($app)) {
228 // The class instance is null
229 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
230 } elseif (!is_object($app)) {
232 throw new NoObjectException($app, self::EXCEPTION_IS_NO_OBJECT);
233 } elseif (!method_exists($app, $this->getConfigInstance()->readConfig("entry_method"))) {
235 throw new MissingMethodException(array($app, $this->getConfigInstance()->readConfig("entry_method")), self::EXCEPTION_MISSING_METHOD);
238 // Add the current instance to the list
239 $this->foundApps->append($app);
241 } // END - if ((is_file(...
245 * Setter for the selector's template engine instance
247 * @param $tplEngine An instance of TemplateEngine
249 private function setSelectorTemplateEngine (CompileableTemplate $tplEngine) {
250 $this->selectorTplEngine = $tplEngine;
254 * Getter for the selector's template engine instance
256 * @return $selectTplEngine The selector's template engine
258 private function getSelectorTemplateEngine () {
259 return $this->selectorTplEngine;
263 * Getter for the $loadedTemplates array object
265 * @return $loadedTemplates An array object holding all loaded
266 * application templates
268 private function getLoadedTemplates () {
269 return $this->loadedTemplates;
273 * Method for compatiblity with prepareTemplateEngine()
275 * @return $shortName This selector's short name
277 private function getAppShortName() {
278 $shortName = $this->getConfigInstance()->readConfig("selector_path");
283 * Add a directory/file to the ignore list
285 * @param $ignoreItem The file/directory we shall ignore
288 public function addDirIgnoreList ($ignoreItem) {
290 $ignoreItem = (string) $ignoreItem;
291 $this->dirIgnoreList[] = $ignoreItem;
295 * Setter for language instance
297 * @param $langInstance An instance to the language sub-system
299 * @see LanguageSystem
301 public final function setLanguageInstance (ManageableLanguage $langInstance) {
302 $this->langInstance = $langInstance;
306 * Setter for file I/O instance
308 * @param $fileIOInstance An instance to the file I/O sub-system
311 public final function setFileIOInstance (FileIOHandler $fileIOInstance) {
312 $this->fileIOInstance = $fileIOInstance;
316 * Read the base path for all applications (application/) and create a
317 * list of all found applications
321 public function readApplicationDirectory () {
322 // Generate the base path for all applications
323 $appBasePath = sprintf("%s%s/",
325 $this->getConfigInstance()->readConfig("application_path")
328 // Add the selector path to the ignore list
329 $this->addDirIgnoreList($this->getConfigInstance()->readConfig("selector_path"));
331 // Get a directory pointer for the application path
332 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
334 // Backup and remove the 'app' from global name space
338 // Read all directories&files except some parts
339 while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
340 // Generate FQFN for the application name (or better directory name)
341 $fqfn = sprintf("%s%s", $appBasePath, $appName);
343 // Is this a readable directory? (files will be ignored silently)
344 if ((is_dir($fqfn)) && (is_readable($fqfn))) {
345 // Then get the init.php script for analyzing
346 $initScript = sprintf("%s/init%s", $fqfn, $this->getConfigInstance()->readConfig("php_extension"));
348 // Load the application's init.php script and append the
349 // application to the ArrayObject
350 $this->loadInitScript($initScript, $appName);
352 } // END - if ((is_dir(...
356 // Close directory pointer
357 $dirInstance->closeDirectory();
359 // Restore old 'app' from backup
364 * Load all templates for found applications in previous scan
368 public function loadApplicationTemplates () {
369 // Iterate through all applications
370 for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
371 // Get current application
372 $appInstance = $idx->current();
374 // Prepare the template engine for the current template
375 $tplEngine = $this->prepareTemplateEngine($appInstance);
377 // Try to load the web template
378 $tplEngine->loadWebTemplate(sprintf("%s_%s",
379 $this->getConfigInstance()->readConfig("tpl_selector_prefix"),
380 strtolower($appInstance->getAppShortName())
383 // Remember this template and the application for later usage
384 $this->loadedTemplates->append(array(
385 'tpl_engine' => $tplEngine,
386 'app_instance' => $appInstance
390 // Re-initialize the application list to avoid double loading
391 $this->initializeAppsList();
395 * Removes $dirIgnoreList from the object to save some memory
399 public function removeDirIgnoreList () {
400 unset($this->dirIgnoreList);
404 * Loads the selector's own main template. This step is not linking the
405 * application's templates into the main one.
409 public function loadSelectorTemplate () {
410 // Prepare the template engine
411 $tplEngine = $this->prepareTemplateEngine($this);
413 // Load the selector's template
414 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig("selector_main_tpl"));
416 // Now store it in the class
417 $this->setSelectorTemplateEngine($tplEngine);
421 * Inserts all loaded application templates into the selector's template
424 * @throws NullPointerException If $curr is null
425 * @throws NoArrayException If $curr is not an array
426 * @throws InvalidArrayCountException If $curr contains an
427 * unexpected count of elements
428 * @throws MissingArrayElementsException If $curr is missing expected
431 public function insertApplicationTemplates () {
432 // First prepare the instance
433 $tplEngine = $this->prepareTemplateEngine($this);
435 // Load template which shall later hold all application templates
436 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig("selector_apps_tpl"));
438 // Add all loaded application templates together
440 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
441 // Get current item from array object
442 $curr = $idx->current();
444 // Do some sanity checks on the loaded item
445 if (is_null($curr)) {
447 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
448 } elseif (!is_array($curr)) {
450 throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
451 } elseif (count($curr) != 2) {
452 // Not expected count of entries
453 throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
454 } elseif (!isset($curr['tpl_engine']) || (!isset($curr['app_instance']))) {
455 // Expected entries missing
456 throw new MissingArrayElementsException(array($this, "curr", array('tpl_engine', 'app_instance')), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
458 die("<pre>".print_r($curr, true)."</pre>");
466 public function saveObjectToDatabase () {
467 $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",