3 * The application selector class.
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
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 // Initialize the array lists
55 $this->initializeAppsList();
56 $this->initializeTemplatesList();
60 * Create a prepared instance of ApplicationSelector
62 * @param $langInstance The language sub-system: LanguageSystem
63 * @param $fileIOInstance The file I/O instance
64 * @return $selInstance An instance of ApplicationSelector
66 public final static function createApplicationSelector (ManageableLanguage $langInstance, FileIoHandler $fileIOInstance) {
68 $selInstance = new ApplicationSelector();
70 // Get all applications
71 $selInstance->readApplicationDirectory();
73 // Set language and file I/O instances
74 $selInstance->setLanguageInstance($langInstance);
75 $selInstance->setFileIoInstance($fileIOInstance);
77 // Return the prepared instance
82 * Initialize the application list
86 private function initializeAppsList () {
87 $this->foundApps = new FrameworkArrayObject("FakedFoundApplications");
91 * Initialize the loaded templates list
95 private function initializeTemplatesList () {
96 $this->loadedTemplates = new FrameworkArrayObject("FakedLoadedTemplates");
100 * Load the data.php script of an application and append the application
101 * instance to $foundApps
103 * @param $appData The FQFN of data.php
104 * @param $appName The application's Uni* name
107 private function loadApplicationData ($appData, $appName) {
108 // Is it a file and readable?
109 if ((is_file($appData)) && (is_readable($appData))) {
113 // Add the current instance to the list
114 $this->foundApps->append($app);
115 } // END - if ((is_file(...
119 * Getter for the $loadedTemplates array object
121 * @return $loadedTemplates An array object holding all loaded
122 * application templates
124 private final function getLoadedTemplates () {
125 return $this->loadedTemplates;
129 * Method for compatiblity with prepareTemplateInstance()
131 * @return $shortName This selector's short name
133 public function getAppShortName() {
134 $shortName = $this->getConfigInstance()->getConfigEntry('selector_path');
139 * Add a directory/file to the ignore list
141 * @param $ignoreItem The file/directory we shall ignore
144 public function addDirIgnoreList ($ignoreItem) {
146 $this->dirIgnoreList[] = (string) $ignoreItem;
150 * Read the base path for all applications (application/) and create a
151 * list of all found applications
155 public function readApplicationDirectory () {
156 // Generate the base path for all applications
157 $appBasePath = $this->getConfigInstance()->getConfigEntry('application_path');
159 // Add the selector path to the ignore list
160 $this->addDirIgnoreList($this->getConfigInstance()->getConfigEntry('selector_path'));
162 // Get a directory pointer for the application path
163 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($appBasePath);
165 // Read all directories&files except some parts
166 while ($appName = $dirInstance->readDirectoryExcept($this->dirIgnoreList)) {
167 // Generate FQFN for the application name (or better directory name)
168 $fqfn = sprintf("%s%s", $appBasePath, $appName);
170 // Is this a readable directory? (files will be ignored silently)
171 if ((is_dir($fqfn)) && (is_readable($fqfn))) {
172 // Then get the data.php script for analyzing
173 $appData = sprintf("%s/data.php", $fqfn);
175 // Load the application's data.php script and append the
176 // application to the ArrayObject
177 $this->loadApplicationData($appData, $appName);
181 // Close directory pointer
182 $dirInstance->closeDirectory();
186 * Load all templates for found applications in previous scan
190 public function loadApplicationTemplates () {
191 // Iterate through all applications
192 for ($idx = $this->foundApps->getIterator(); $idx->valid(); $idx->next()) {
193 // Get current application
194 $appInstance = $idx->current();
196 // Prepare the template engine for the current template
197 $templateInstance = $this->prepareTemplateInstance($appInstance);
199 // Try to load the web template
200 $templateInstance->loadWebTemplate(sprintf("%s_%s",
201 $this->getConfigInstance()->getConfigEntry('tpl_selector_prefix'),
202 strtolower($appInstance->getAppShortName())
205 // Remember this template and the application for later usage
206 $this->loadedTemplates->append(array(
207 'template_class' => $templateInstance,
208 'app_instance' => $appInstance
212 // Re-initialize the application list to avoid double loading
213 $this->initializeAppsList();
217 * Removes $dirIgnoreList from the object to save some memory
221 public final function removeDirIgnoreList () {
222 unset($this->dirIgnoreList);
226 * Loads the selector's own main template. This step is not linking the
227 * application's templates into the main one.
231 public function loadSelectorTemplate () {
232 // Prepare the template engine
233 $templateInstance = $this->prepareTemplateInstance($this);
235 // Load the selector's template
236 $templateInstance->loadCodeTemplate($this->getConfigInstance()->getConfigEntry('selector_main_tpl'));
238 // Now store it in the class, we need this later on final compilation of available applications
239 $this->setTemplateInstance($templateInstance);
243 * Inserts all loaded application templates into the selector's template
246 * @throws NoArrayException If $curr is not an array
247 * @throws InvalidArrayCountException If $curr contains an
248 * unexpected count of elements
249 * @throws MissingArrayElementsException If $curr is missing expected
251 * @todo Finish handling all applications here
253 public function insertApplicationTemplates () {
254 // First prepare the instance
255 $templateInstance = $this->prepareTemplateInstance($this);
257 // Load template which shall later hold all application templates
258 $templateInstance->loadCodeTemplate($this->getConfigInstance()->getConfigEntry('selector_apps_tpl'));
260 // Add all loaded application templates together
262 for ($idx = $this->getLoadedTemplates()->getIterator(); $idx->valid(); $idx->next()) {
263 // Get current item from array object
264 $curr = $idx->current();
266 // Do some sanity checks on the loaded item
267 if (!is_array($curr)) {
269 throw new NoArrayException($curr, self::EXCEPTION_IS_NO_ARRAY);
270 } elseif (count($curr) != 2) {
271 // Not expected count of entries
272 throw new InvalidArrayCountException(array($this, "curr", count($curr), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
273 } elseif (!isset($curr['template_class']) || (!isset($curr['app_instance']))) {
274 // Expected entries missing
275 throw new MissingArrayElementsException(array($this, "curr", array("template_class", "app_instance")), self::EXCEPTION_ARRAY_ELEMENTS_MISSING);
279 die(__METHOD__."()<pre>".print_r($curr, true)."</pre>");