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
10 * @author Roland Haeder <roland __NOSPAM__ [at] __REMOVE_ME__ mxchange [dot] org>
13 class ApplicationSelector extends BaseFrameworkSystem {
15 * An ArrayObject for all found applications
17 private $foundApps = null;
20 * An array object for all loaded application templates (selector_app-name.tpl)
22 private $loadedTemplates = null;
25 * The application selector's own template engine handler
27 private $selectorTplEngine = null;
30 * A list of items we shall ignore while reading from directories
32 private $dirIgnoreList = array(
39 * The language instance for the template loader
41 private $langInstance = null;
44 * The file I/O instance for the template loader
46 private $fileIOInstance = null;
49 * The private constructor. No direct instances can be created from this.
53 private function __construct() {
54 // Call parent constructor
55 parent::constructor(__CLASS__);
58 $this->setPartDescr("Applikationswechsler");
60 // Create unique ID number
61 $this->createUniqueID();
63 // Remove system array and thousand seperator
64 $this->removeSystemArray();
65 $this->removeNumberFormaters();
67 // Initialize the array lists
68 $this->initializeAppsList();
69 $this->initializeTemplatesList();
73 * Create a prepared instance of ApplicationSelector
75 * @param $langInstance The language sub-system: LanguageSystem
76 * @param $fileIOInstance The file I/O instance
77 * @return $selInstance An instance of ApplicationSelector
79 public static function createApplicationSelector (ManageableLanguage $langInstance, FileIOHandler $fileIOInstance) {
81 $selInstance = new ApplicationSelector();
83 // Get all applications
84 $selInstance->readApplicationDirectory();
86 // Set language and file I/O instances
87 $selInstance->setLanguageInstance($langInstance);
88 $selInstance->setFileIOInstance($fileIOInstance);
90 // Return the prepared instance
95 * Initialize the application list
99 private function initializeAppsList () {
100 $this->foundApps = new FrameworkArrayObject();
104 * Initialize the loaded templates list
108 private function initializeTemplatesList () {
109 $this->loadedTemplates = new FrameworkArrayObject();
113 * Private getter for language instance
115 * @return $langInstance An instance to the language sub-system
117 private function getLanguageInstance () {
118 return $this->langInstance;
122 * Private getter for file IO instance
124 * @return $fileIOInstance An instance to the file I/O sub-system
126 private function getFileIOInstance () {
127 return $this->fileIOInstance;
131 * Prepare the template engine (TemplateEngine by default) for a give
132 * application helper instance (ApplicationHelper by default).
134 * @param $appInstance An application helper instance
135 * @return $tplEngine The template engine instance
136 * @throws NullPointerException If the template engine could not
138 * @throws NoObjectException If $tplEngine is not an object
139 * @throws MissingMethodException If $tplEngine misses a required
140 * method 'loadWebTemplate()'
142 private function prepareTemplateEngine (ManageableApplication $appInstance) {
143 // Generate FQFN for all application templates
144 $fqfn = sprintf("%s%s/%s/%s",
146 $this->getConfigInstance()->readConfig("application_path"),
147 strtolower($appInstance->getAppShortName()),
148 $this->getConfigInstance()->readConfig("tpl_base_path")
151 // Initialize the template engine
153 $eval = sprintf("\$tplEngine = %s::create%s(
155 \$this->getLanguageInstance(),
156 \$this->getFileIOInstance()
158 $this->getConfigInstance()->readConfig("tpl_engine"),
159 $this->getConfigInstance()->readConfig("tpl_engine"),
164 if ((!is_null($this->getDebugInstance())) && (defined('DEBUG_EVAL'))) {
165 $this->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
174 // Is it a valid instance?
175 if (is_null($tplEngine)) {
177 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
178 } elseif (!is_object($tplEngine)) {
179 // Not an object! ;-(
180 throw new NoObjectException($tplEngine, self::EXCEPTION_IS_NO_OBJECT);
181 } elseif (!method_exists($tplEngine, 'loadWebTemplate')) {
182 // Nope, so throw exception
183 throw new MissingMethodException(array($tplEngine, 'loadWebTemplate'), self::EXCEPTION_MISSING_METHOD);
186 // Return the prepared instance
191 * Load the init.php script of an application and append the application
192 * instance to $foundApps
194 * @param $initScript The FQFN of init.php
195 * @param $appName The application's Uni* name
197 * @throws AppVarIsNotSetException If 'app' is not set
198 * @throws NullPointerException If 'app' is null
199 * @throws NoObjectException If 'app' is not an object
200 * @throws MissingMethodException If a required method is missing
202 private function loadInitScript ($initScript, $appName) {
203 // Is it a file and readable?
204 if ((is_file($initScript)) && (is_readable($initScript))) {
206 include ($initScript);
208 // We now should have $app re-defined!
210 // This application shall not be loaded
212 } elseif (is_null($app)) {
213 // The class instance is null
214 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
215 } elseif (!is_object($app)) {
217 throw new NoObjectException($app, self::EXCEPTION_IS_NO_OBJECT);
218 } elseif (!method_exists($app, $this->getConfigInstance()->readConfig("entry_method"))) {
220 throw new MissingMethodException(array($app, $this->getConfigInstance()->readConfig("entry_method")), self::EXCEPTION_MISSING_METHOD);
223 // Add the current instance to the list
224 $this->foundApps->append($app);
226 } // END - if ((is_file(...
230 * Setter for the selector's template engine instance
232 * @param $tplEngine An instance of TemplateEngine
234 private function setSelectorTemplateEngine (CompileableTemplate $tplEngine) {
235 $this->selectorTplEngine = $tplEngine;
239 * Getter for the selector's template engine instance
241 * @return $selectTplEngine The selector's template engine
243 private function getSelectorTemplateEngine () {
244 return $this->selectorTplEngine;
248 * Getter for the $loadedTemplates array object
250 * @return $loadedTemplates An array object holding all loaded
251 * application templates
253 private function getLoadedTemplates () {
254 return $this->loadedTemplates;
258 * Method for compatiblity with prepareTemplateEngine()
260 * @return $shortName This selector's short name
262 private function getAppShortName() {
263 $shortName = $this->getConfigInstance()->readConfig("selector_path");
268 * Add a directory/file to the ignore list
270 * @param $ignoreItem The file/directory we shall ignore
273 public function addDirIgnoreList ($ignoreItem) {
275 $ignoreItem = (string) $ignoreItem;
276 $this->dirIgnoreList[] = $ignoreItem;
280 * Setter for language instance
282 * @param $langInstance An instance to the language sub-system
284 * @see LanguageSystem
286 public function setLanguageInstance (ManageableLanguage $langInstance) {
287 $this->langInstance = $langInstance;
291 * Setter for file I/O instance
293 * @param $fileIOInstance An instance to the file I/O sub-system
296 public function setFileIOInstance (FileIOHandler $fileIOInstance) {
297 $this->fileIOInstance = $fileIOInstance;
301 * Read the base path for all applications (application/) and create a
302 * list of all found applications
306 public function readApplicationDirectory () {
307 // Generate the base path for all applications
308 $appBasePath = sprintf("%s%s/",
310 $this->getConfigInstance()->readConfig("application_path")
313 // Add the selector path to the ignore list
314 $this->addDirIgnoreList($this->getConfigInstance()->readConfig("selector_path"));
316 // Get a directory pointer for the application path
317 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
319 // Backup and remove the 'app' from global name space
323 // Read all directories&files except some parts
324 while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
325 // Generate FQFN for the application name (or better directory name)
326 $fqfn = sprintf("%s%s", $appBasePath, $appName);
328 // Is this a readable directory? (files will be ignored silently)
329 if ((is_dir($fqfn)) && (is_readable($fqfn))) {
330 // Then get the init.php script for analyzing
331 $initScript = sprintf("%s/init%s", $fqfn, $this->getConfigInstance()->readConfig("php_extension"));
333 // Load the application's init.php script and append the
334 // application to the ArrayObject
335 $this->loadInitScript($initScript, $appName);
337 } // END - if ((is_dir(...
341 // Close directory pointer
342 $dirInstance->closeDirectory();
344 // Restore old 'app' from backup
349 * Load all templates for found applications in previous scan
353 public function loadApplicationTemplates () {
354 // Iterate through all applications
355 for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
356 // Get current application
357 $appInstance = $idx->current();
359 // Prepare the template engine for the current template
360 $tplEngine = $this->prepareTemplateEngine($appInstance);
362 // Try to load the web template
363 $tplEngine->loadWebTemplate(sprintf("%s_%s",
364 $this->getConfigInstance()->readConfig("tpl_selector_prefix"),
365 strtolower($appInstance->getAppShortName())
368 // Remember this template and the application for later usage
369 $this->loadedTemplates->append(array(
370 'tpl_engine' => $tplEngine,
371 'app_instance' => $appInstance
375 // Re-initialize the application list to avoid double loading
376 $this->initializeAppsList();
380 * Removes $dirIgnoreList from the object to save some memory
384 public function removeDirIgnoreList () {
385 unset($this->dirIgnoreList);
389 * Loads the selector's own main template. This step is not linking the
390 * application's templates into the main one.
394 public function loadSelectorTemplate () {
395 // Prepare the template engine
396 $tplEngine = $this->prepareTemplateEngine($this);
398 // Load the selector's template
399 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig("selector_main_tpl"));
401 // Now store it in the class
402 $this->setSelectorTemplateEngine($tplEngine);
406 * Inserts all loaded application templates into the selector's template
409 * @throws NullPointerException If $curr is null
410 * @throws NoArrayException If $curr is not an array
411 * @throws InvalidArrayCountException If $curr contains an
412 * unexpected count of elements
413 * @throws MissingArrayElementsException If $curr is missing expected
416 public function insertApplicationTemplates () {
417 // First prepare the instance
418 $tplEngine = $this->prepareTemplateEngine($this);
420 // Load template which shall later hold all application templates
421 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig("selector_apps_tpl"));
423 // Add all loaded application templates together
425 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
426 // Get current item from array object
427 $curr = $idx->current();
429 // Do some sanity checks on the loaded item
430 if (is_null($curr)) {
432 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
433 } elseif (!is_array($curr)) {
435 throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
436 } elseif (count($curr) != 2) {
437 // Not expected count of entries
438 throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
439 } elseif (!isset($curr['tpl_engine']) || (!isset($curr['app_instance']))) {
440 // Expected entries missing
441 throw new MissingArrayElementsException(array($this, "curr", array('tpl_engine', 'app_instance')), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
443 die("<pre>".print_r($curr, true)."</pre>");
451 public function saveObjectToDatabase () {
452 $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",