]> git.mxchange.org Git - shipsimu.git/blob - application/selector/class_ApplicationSelector.php
todo completed
[shipsimu.git] / application / selector / class_ApplicationSelector.php
1 <?php
2 /**
3  * The application selector class.
4  *
5  * Please remember that this include file is being loaded *before* the class
6  * loader is loading classes from "exceptions", "interfaces" and "main"!
7  *
8  * @author              Roland Haeder <webmaster@ship-simu.org>
9  * @version             0.0.0
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
13  *
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.
18  *
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.
23  *
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/>.
26  */
27 class ApplicationSelector extends BaseFrameworkSystem {
28         /**
29          * An ArrayObject for all found applications
30          */
31         private $foundApps = null;
32
33         /**
34          * An array object for all loaded application templates (selector_app-name.tpl)
35          */
36         private $loadedTemplates = null;
37
38         /**
39          * The application selector's own template engine handler
40          */
41         private $selectorTplEngine = null;
42
43         /**
44          * A list of items we shall ignore while reading from directories
45          */
46         private $dirIgnoreList = array(
47                 ".",
48                 "..",
49                 ".htaccess"
50         );
51
52         /**
53          * The language instance for the template loader
54          */
55         private $langInstance = null;
56
57         /**
58          * The file I/O instance for the template loader
59          */
60         private $fileIOInstance = null;
61
62         /**
63          * The private constructor. No direct instances can be created from this.
64          *
65          * @return      void
66          */
67         private function __construct() {
68                 // Call parent constructor
69                 parent::constructor(__CLASS__);
70
71                 // Set description
72                 $this->setPartDescr("Applikationswechsler");
73
74                 // Create unique ID number
75                 $this->createUniqueID();
76
77                 // Remove system array and thousand seperator
78                 $this->removeSystemArray();
79                 $this->removeNumberFormaters();
80
81                 // Initialize the array lists
82                 $this->initializeAppsList();
83                 $this->initializeTemplatesList();
84         }
85
86         /**
87          * Create a prepared instance of ApplicationSelector
88          *
89          * @param               $langInstance           The language sub-system: LanguageSystem
90          * @param               $fileIOInstance The file I/O instance
91          * @return              $selInstance            An instance of ApplicationSelector
92          */
93         public static function createApplicationSelector (ManageableLanguage $langInstance, FileIOHandler $fileIOInstance) {
94                 // Get a new instance
95                 $selInstance = new ApplicationSelector();
96
97                 // Get all applications
98                 $selInstance->readApplicationDirectory();
99
100                 // Set language and file I/O instances
101                 $selInstance->setLanguageInstance($langInstance);
102                 $selInstance->setFileIOInstance($fileIOInstance);
103
104                 // Return the prepared instance
105                 return $selInstance;
106         }
107
108         /**
109          * Initialize the application list
110          *
111          * @return      void
112          */
113         private function initializeAppsList () {
114                 $this->foundApps = new FrameworkArrayObject();
115         }
116
117         /**
118          * Initialize the loaded templates list
119          *
120          * @return      void
121          */
122         private function initializeTemplatesList () {
123                 $this->loadedTemplates = new FrameworkArrayObject();
124         }
125
126         /**
127          * Private getter for language instance
128          *
129          * @return      $langInstance   An instance to the language sub-system
130          */
131         private function getLanguageInstance () {
132                 return $this->langInstance;
133         }
134
135         /**
136          * Private getter for file IO instance
137          *
138          * @return      $fileIOInstance An instance to the file I/O sub-system
139          */
140         private function getFileIOInstance () {
141                 return $this->fileIOInstance;
142         }
143
144         /**
145          * Prepare the template engine (TemplateEngine by default) for a give
146          * application helper instance (ApplicationHelper by default).
147          *
148          * @param               $appInstance    An application helper instance
149          * @return              $tplEngine      The template engine instance
150          * @throws              NullPointerException    If the template engine could not
151          *                                                                              be initialized
152          * @throws              UnsupportedTemplateEngineException      If $tplEngine is an
153          *                                                                              unsupported template engine
154          */
155         private function prepareTemplateEngine (BaseFrameworkSystem $appInstance) {
156                 // Generate FQFN for all application templates
157                 $fqfn = sprintf("%s%s/%s/%s",
158                         PATH,
159                         $this->getConfigInstance()->readConfig("application_path"),
160                         strtolower($appInstance->getAppShortName()),
161                         $this->getConfigInstance()->readConfig("tpl_base_path")
162                 );
163
164                 // Initialize the template engine
165                 $tplEngine = null;
166                 $eval = sprintf("\$tplEngine = %s::create%s(
167         \"%s\",
168         \$this->getLanguageInstance(),
169         \$this->getFileIOInstance()
170 );",
171                         $this->getConfigInstance()->readConfig("tpl_engine"),
172                         $this->getConfigInstance()->readConfig("tpl_engine"),
173                         $fqfn
174                 );
175
176                 // Debug message
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",
179                                 $this->__toString(),
180                                 htmlentities($eval)
181                         ));
182                 }
183
184                 // Run the command
185                 @eval($eval);
186
187                 // Is it a valid instance?
188                 if (is_null($tplEngine)) {
189                         // No class returned
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);
194                 }
195
196                 // Return the prepared instance
197                 return $tplEngine;
198         }
199
200         /**
201          * Load the init.php script of an application and append the application
202          * instance to $foundApps
203          *
204          * @param               $initScript     The FQFN of init.php
205          * @param               $appName                The application's Uni* name
206          * @return      void
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
211          */
212         private function loadInitScript ($initScript, $appName) {
213                 // Is it a file and readable?
214                 if ((is_file($initScript)) && (is_readable($initScript))) {
215                         // Then include it
216                         include ($initScript);
217
218                         // We now should have $app re-defined!
219                         if (!isset($app)) {
220                                 // This application shall not be loaded
221                                 return;
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)) {
226                                 // Not an object
227                                 throw new NoObjectException($app, self::EXCEPTION_IS_NO_OBJECT);
228                         } elseif (!method_exists($app, $this->getConfigInstance()->readConfig("entry_method"))) {
229                                 // Method not found!
230                                 throw new MissingMethodException(array($app, $this->getConfigInstance()->readConfig("entry_method")), self::EXCEPTION_MISSING_METHOD);
231                         }
232
233                         // Add the current instance to the list
234                         $this->foundApps->append($app);
235
236                 } // END - if ((is_file(...
237         }
238
239         /**
240          * Setter for the selector's template engine instance
241          *
242          * @param               $tplEngine      An instance of TemplateEngine
243          */
244         private function setSelectorTemplateEngine (CompileableTemplate $tplEngine) {
245                 $this->selectorTplEngine = $tplEngine;
246         }
247
248         /**
249          * Getter for the selector's template engine instance
250          *
251          * @return      $selectTplEngine        The selector's template engine
252          */
253         private function getSelectorTemplateEngine () {
254                 return $this->selectorTplEngine;
255         }
256
257         /**
258          * Getter for the $loadedTemplates array object
259          *
260          * @return      $loadedTemplates        An array object holding all loaded
261          *                                                      application templates
262          */
263         private function getLoadedTemplates () {
264                 return $this->loadedTemplates;
265         }
266
267         /**
268          * Method for compatiblity with prepareTemplateEngine()
269          *
270          * @return      $shortName      This selector's short name
271          */
272         private function getAppShortName() {
273                 $shortName = $this->getConfigInstance()->readConfig("selector_path");
274                 return $shortName;
275         }
276
277         /**
278          * Add a directory/file to the ignore list
279          *
280          * @param               $ignoreItem     The file/directory we shall ignore
281          * @return      void
282          */
283         public function addDirIgnoreList ($ignoreItem) {
284                 // Cast and add it
285                 $ignoreItem = (string) $ignoreItem;
286                 $this->dirIgnoreList[] = $ignoreItem;
287         }
288
289         /**
290          * Setter for language instance
291          *
292          * @param               $langInstance   An instance to the language sub-system
293          * @return      void
294          * @see         LanguageSystem
295          */
296         public final function setLanguageInstance (ManageableLanguage $langInstance) {
297                 $this->langInstance = $langInstance;
298         }
299
300         /**
301          * Setter for file I/O instance
302          *
303          * @param               $fileIOInstance An instance to the file I/O sub-system
304          * @return      void
305          */
306         public final function setFileIOInstance (FileIOHandler $fileIOInstance) {
307                 $this->fileIOInstance = $fileIOInstance;
308         }
309
310         /**
311          * Read the base path for all applications (application/) and create a
312          * list of all found applications
313          *
314          * @return      void
315          */
316         public function readApplicationDirectory () {
317                 // Generate the base path for all applications
318                 $appBasePath = sprintf("%s%s/",
319                         PATH,
320                         $this->getConfigInstance()->readConfig("application_path")
321                 );
322
323                 // Add the selector path to the ignore list
324                 $this->addDirIgnoreList($this->getConfigInstance()->readConfig("selector_path"));
325
326                 // Get a directory pointer for the application path
327                 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
328
329                 // Backup and remove the 'app' from global name space
330                 /*$appBackup = $app;
331                 unset($app);*/
332
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);
337
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"));
342
343                                 // Load the application's init.php script and append the
344                                 // application to the ArrayObject
345                                 $this->loadInitScript($initScript, $appName);
346
347                         } // END - if ((is_dir(...
348
349                 } // END - while
350
351                 // Close directory pointer
352                 $dirInstance->closeDirectory();
353
354                 // Restore old 'app' from backup
355                 //$app = $appBackup;
356         }
357
358         /**
359          * Load all templates for found applications in previous scan
360          *
361          * @return      void
362          */
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();
368
369                         // Prepare the template engine for the current template
370                         $tplEngine = $this->prepareTemplateEngine($appInstance);
371
372                         // Try to load the web template
373                         $tplEngine->loadWebTemplate(sprintf("%s_%s",
374                                 $this->getConfigInstance()->readConfig("tpl_selector_prefix"),
375                                 strtolower($appInstance->getAppShortName())
376                         ));
377
378                         // Remember this template and the application for later usage
379                         $this->loadedTemplates->append(array(
380                                 'tpl_engine'   => $tplEngine,
381                                 'app_instance' => $appInstance
382                         ));
383                 }
384
385                 // Re-initialize the application list to avoid double loading
386                 $this->initializeAppsList();
387         }
388
389         /**
390          * Removes $dirIgnoreList from the object to save some memory
391          *
392          * @return      void
393          */
394         public function removeDirIgnoreList () {
395                 unset($this->dirIgnoreList);
396         }
397
398         /**
399          * Loads the selector's own main template. This step is not linking the
400          * application's templates into the main one.
401          *
402          * @return      void
403          */
404         public function loadSelectorTemplate () {
405                 // Prepare the template engine
406                 $tplEngine = $this->prepareTemplateEngine($this);
407
408                 // Load the selector's template
409                 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig("selector_main_tpl"));
410
411                 // Now store it in the class
412                 $this->setSelectorTemplateEngine($tplEngine);
413         }
414
415         /**
416          * Inserts all loaded application templates into the selector's template
417          *
418          * @return      void
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
424          *                                                                      array elements
425          */
426         public function insertApplicationTemplates () {
427                 // First prepare the instance
428                 $tplEngine = $this->prepareTemplateEngine($this);
429
430                 // Load template which shall later hold all application templates
431                 $tplEngine->loadCodeTemplate($this->getConfigInstance()->readConfig("selector_apps_tpl"));
432
433                 // Add all loaded application templates together
434                 $dummy = "";
435                 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
436                         // Get current item from array object
437                         $curr = $idx->current();
438
439                         // Do some sanity checks on the loaded item
440                         if (is_null($curr)) {
441                                 // $curr is null
442                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
443                         } elseif (!is_array($curr)) {
444                                 // Not an array
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);
452                         }
453                         die("<pre>".print_r($curr, true)."</pre>");
454
455                 } // END - for
456         }
457
458         /**
459          * Stub!
460          */
461         public function saveObjectToDatabase () {
462                 $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",
463                         $this->__toString(),
464                         __FUNCTION__
465                 ));
466         }
467 }
468
469 // [EOF]
470 ?>