3 * The application selector class.
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class ApplicationSelector extends BaseFrameworkSystem {
26 * An ArrayObject for all found applications
28 private $foundApps = null;
31 * An array object for all loaded application templates (selector_app-name.tpl)
33 private $loadedTemplates = null;
36 * A list of items we shall ignore while reading from directories
38 private $dirIgnoreList = array(
46 * The protected constructor. No direct instances can be created from this.
50 protected function __construct() {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
54 // Remove system array and thousand seperator
55 $this->removeSystemArray();
56 $this->removeNumberFormaters();
58 // Initialize the array lists
59 $this->initializeAppsList();
60 $this->initializeTemplatesList();
64 * Create a prepared instance of ApplicationSelector
66 * @param $langInstance The language sub-system: LanguageSystem
67 * @param $fileIOInstance The file I/O instance
68 * @return $selInstance An instance of ApplicationSelector
70 public final static function createApplicationSelector (ManageableLanguage $langInstance, FileIoHandler $fileIOInstance) {
72 $selInstance = new ApplicationSelector();
74 // Get all applications
75 $selInstance->readApplicationDirectory();
77 // Set language and file I/O instances
78 $selInstance->setLanguageInstance($langInstance);
79 $selInstance->setFileIoInstance($fileIOInstance);
81 // Return the prepared instance
86 * Initialize the application list
90 private function initializeAppsList () {
91 $this->foundApps = new FrameworkArrayObject("FakedFoundApplications");
95 * Initialize the loaded templates list
99 private function initializeTemplatesList () {
100 $this->loadedTemplates = new FrameworkArrayObject("FakedLoadedTemplates");
104 * Load the data.php script of an application and append the application
105 * instance to $foundApps
107 * @param $appData The FQFN of data.php
108 * @param $appName The application's Uni* name
111 private function loadApplicationData ($appData, $appName) {
112 // Is it a file and readable?
113 if ((is_file($appData)) && (is_readable($appData))) {
117 // Add the current instance to the list
118 $this->foundApps->append($app);
119 } // END - if ((is_file(...
123 * Getter for the $loadedTemplates array object
125 * @return $loadedTemplates An array object holding all loaded
126 * application templates
128 private final function getLoadedTemplates () {
129 return $this->loadedTemplates;
133 * Method for compatiblity with prepareTemplateInstance()
135 * @return $shortName This selector's short name
137 public function getAppShortName() {
138 $shortName = $this->getConfigInstance()->readConfig('selector_path');
143 * Add a directory/file to the ignore list
145 * @param $ignoreItem The file/directory we shall ignore
148 public function addDirIgnoreList ($ignoreItem) {
150 $this->dirIgnoreList[] = (string) $ignoreItem;
154 * Read the base path for all applications (application/) and create a
155 * list of all found applications
159 public function readApplicationDirectory () {
160 // Generate the base path for all applications
161 $appBasePath = $this->getConfigInstance()->readConfig('application_path');
163 // Add the selector path to the ignore list
164 $this->addDirIgnoreList($this->getConfigInstance()->readConfig('selector_path'));
166 // Get a directory pointer for the application path
167 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
169 // Read all directories&files except some parts
170 while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
171 // Generate FQFN for the application name (or better directory name)
172 $fqfn = sprintf("%s%s", $appBasePath, $appName);
174 // Is this a readable directory? (files will be ignored silently)
175 if ((is_dir($fqfn)) && (is_readable($fqfn))) {
176 // Then get the data.php script for analyzing
177 $appData = sprintf("%s/data.php", $fqfn);
179 // Load the application's data.php script and append the
180 // application to the ArrayObject
181 $this->loadApplicationData($appData, $appName);
185 // Close directory pointer
186 $dirInstance->closeDirectory();
190 * Load all templates for found applications in previous scan
194 public function loadApplicationTemplates () {
195 // Iterate through all applications
196 for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
197 // Get current application
198 $appInstance = $idx->current();
200 // Prepare the template engine for the current template
201 $templateInstance = $this->prepareTemplateInstance($appInstance);
203 // Try to load the web template
204 $templateInstance->loadWebTemplate(sprintf("%s_%s",
205 $this->getConfigInstance()->readConfig('tpl_selector_prefix'),
206 strtolower($appInstance->getAppShortName())
209 // Remember this template and the application for later usage
210 $this->loadedTemplates->append(array(
211 'template_class' => $templateInstance,
212 'app_instance' => $appInstance
216 // Re-initialize the application list to avoid double loading
217 $this->initializeAppsList();
221 * Removes $dirIgnoreList from the object to save some memory
225 public final function removeDirIgnoreList () {
226 unset($this->dirIgnoreList);
230 * Loads the selector's own main template. This step is not linking the
231 * application's templates into the main one.
235 public function loadSelectorTemplate () {
236 // Prepare the template engine
237 $templateInstance = $this->prepareTemplateInstance($this);
239 // Load the selector's template
240 $templateInstance->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_main_tpl'));
242 // Now store it in the class, we need this later on final compilation of available applications
243 $this->setTemplateInstance($templateInstance);
247 * Inserts all loaded application templates into the selector's template
250 * @throws NullPointerException If $curr is null
251 * @throws NoArrayException If $curr is not an array
252 * @throws InvalidArrayCountException If $curr contains an
253 * unexpected count of elements
254 * @throws MissingArrayElementsException If $curr is missing expected
256 * @todo Finish handling all applications here
258 public function insertApplicationTemplates () {
259 // First prepare the instance
260 $templateInstance = $this->prepareTemplateInstance($this);
262 // Load template which shall later hold all application templates
263 $templateInstance->loadCodeTemplate($this->getConfigInstance()->readConfig('selector_apps_tpl'));
265 // Add all loaded application templates together
267 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
268 // Get current item from array object
269 $curr = $idx->current();
271 // Do some sanity checks on the loaded item
272 if (is_null($curr)) {
274 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
275 } elseif (!is_array($curr)) {
277 throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
278 } elseif (count($curr) != 2) {
279 // Not expected count of entries
280 throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
281 } elseif (!isset($curr['template_class']) || (!isset($curr['app_instance']))) {
282 // Expected entries missing
283 throw new MissingArrayElementsException(array($this, "curr", array("template_class", "app_instance")), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
287 die(__METHOD__."()<pre>".print_r($curr, true)."</pre>");