From 090cdbc7b32bb76588126b2591e4a8d18d855f0c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 13 Mar 2008 19:42:42 +0000 Subject: [PATCH] More exceptions added, init of template engine moved to base class, entryPoint() continued (still unfinished) --- .gitattributes | 3 + .../selector/class_ApplicationSelector.php | 107 +--------------- .../ship-simu/class_ApplicationHelper.php | 12 +- application/ship-simu/init.php | 2 + .../main/companies/class_ShippingCompany.php | 2 +- .../templates/de/code/shipsimu_main.ctp | 11 ++ .../class_MissingFileIoHandlerException.php | 45 +++++++ .../class_MissingLanguageHandlerException.php | 45 +++++++ .../main/class_BaseFrameworkSystem.php | 120 +++++++++++++++++- .../classes/class_LocalFileDatabase.php | 43 +------ .../main/template/class_TemplateEngine.php | 42 +----- 11 files changed, 245 insertions(+), 187 deletions(-) create mode 100644 application/ship-simu/templates/de/code/shipsimu_main.ctp create mode 100644 inc/classes/exceptions/language/class_MissingFileIoHandlerException.php create mode 100644 inc/classes/exceptions/language/class_MissingLanguageHandlerException.php diff --git a/.gitattributes b/.gitattributes index 527b37d..c549cfb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -114,6 +114,7 @@ application/ship-simu/main/structures/extended/upper/class_Bridge.php -text application/ship-simu/starter.php -text application/ship-simu/templates/.htaccess -text application/ship-simu/templates/de/.htaccess -text +application/ship-simu/templates/de/code/shipsimu_main.ctp -text application/ship-simu/templates/de/html/.htaccess -text application/ship-simu/templates/de/html/selector_ship-simu.tpl -text db/.htaccess -text @@ -154,6 +155,8 @@ inc/classes/exceptions/language/class_InvalidLanguagePathStringException.php -te inc/classes/exceptions/language/class_LanguagePathIsEmptyException.php -text inc/classes/exceptions/language/class_LanguagePathIsNoDirectoryException.php -text inc/classes/exceptions/language/class_LanguagePathReadProtectedException.php -text +inc/classes/exceptions/language/class_MissingFileIoHandlerException.php -text +inc/classes/exceptions/language/class_MissingLanguageHandlerException.php -text inc/classes/exceptions/main/.htaccess -text inc/classes/exceptions/main/class_ClassMismatchException.php -text inc/classes/exceptions/main/class_ClassNotFoundException.php -text diff --git a/application/selector/class_ApplicationSelector.php b/application/selector/class_ApplicationSelector.php index f4515e2..c47ef87 100644 --- a/application/selector/class_ApplicationSelector.php +++ b/application/selector/class_ApplicationSelector.php @@ -49,16 +49,6 @@ class ApplicationSelector extends BaseFrameworkSystem { ".htaccess" ); - /** - * The language instance for the template loader - */ - private $langInstance = null; - - /** - * The file I/O instance for the template loader - */ - private $fileIOInstance = null; - /** * The private constructor. No direct instances can be created from this. * @@ -123,80 +113,6 @@ class ApplicationSelector extends BaseFrameworkSystem { $this->loadedTemplates = new FrameworkArrayObject(); } - /** - * Private getter for language instance - * - * @return $langInstance An instance to the language sub-system - */ - private function getLanguageInstance () { - return $this->langInstance; - } - - /** - * Private getter for file IO instance - * - * @return $fileIOInstance An instance to the file I/O sub-system - */ - private function getFileIOInstance () { - return $this->fileIOInstance; - } - - /** - * Prepare the template engine (TemplateEngine by default) for a give - * application helper instance (ApplicationHelper by default). - * - * @param $appInstance An application helper instance - * @return $tplEngine The template engine instance - * @throws NullPointerException If the template engine could not - * be initialized - * @throws UnsupportedTemplateEngineException If $tplEngine is an - * unsupported template engine - */ - private function prepareTemplateEngine (BaseFrameworkSystem $appInstance) { - // Generate FQFN for all application templates - $fqfn = sprintf("%s%s/%s/%s", - PATH, - $this->getConfigInstance()->readConfig("application_path"), - strtolower($appInstance->getAppShortName()), - $this->getConfigInstance()->readConfig("tpl_base_path") - ); - - // Initialize the template engine - $tplEngine = null; - $eval = sprintf("\$tplEngine = %s::create%s( - \"%s\", - \$this->getLanguageInstance(), - \$this->getFileIOInstance() -);", - $this->getConfigInstance()->readConfig("tpl_engine"), - $this->getConfigInstance()->readConfig("tpl_engine"), - $fqfn - ); - - // Debug message - if ((!is_null($this->getDebugInstance())) && (defined('DEBUG_EVAL'))) { - $this->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung:
%s

\n", - $this->__toString(), - htmlentities($eval) - )); - } - - // Run the command - @eval($eval); - - // Is it a valid instance? - if (is_null($tplEngine)) { - // No class returned - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } elseif (!$tplEngine instanceof CompileableTemplate) { - // Not an object! ;-( - throw new UnsupportedTemplateEngineException($tplEngine, self::EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED); - } - - // Return the prepared instance - return $tplEngine; - } - /** * Load the init.php script of an application and append the application * instance to $foundApps @@ -269,7 +185,7 @@ class ApplicationSelector extends BaseFrameworkSystem { * * @return $shortName This selector's short name */ - private function getAppShortName() { + public function getAppShortName() { $shortName = $this->getConfigInstance()->readConfig("selector_path"); return $shortName; } @@ -286,27 +202,6 @@ class ApplicationSelector extends BaseFrameworkSystem { $this->dirIgnoreList[] = $ignoreItem; } - /** - * Setter for language instance - * - * @param $langInstance An instance to the language sub-system - * @return void - * @see LanguageSystem - */ - public final function setLanguageInstance (ManageableLanguage $langInstance) { - $this->langInstance = $langInstance; - } - - /** - * Setter for file I/O instance - * - * @param $fileIOInstance An instance to the file I/O sub-system - * @return void - */ - public final function setFileIOInstance (FileIOHandler $fileIOInstance) { - $this->fileIOInstance = $fileIOInstance; - } - /** * Read the base path for all applications (application/) and create a * list of all found applications diff --git a/application/ship-simu/class_ApplicationHelper.php b/application/ship-simu/class_ApplicationHelper.php index e3209ef..e141d04 100644 --- a/application/ship-simu/class_ApplicationHelper.php +++ b/application/ship-simu/class_ApplicationHelper.php @@ -163,8 +163,16 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica * @return void */ public final function entryPoint () { - // Must still be implemented! - trigger_error(__METHOD__.": Not implemented yet!"); + // Handle the request + + // Prepare the template engine + $tplEngine = $this->prepareTemplateEngine($this); + + // Load the main template + $tplEngine->loadCodeTemplate("shipsimu_main"); + + // Raise an error here + trigger_error(__METHOD__.": Unfinished work!"); } } diff --git a/application/ship-simu/init.php b/application/ship-simu/init.php index 0674104..b8f232b 100644 --- a/application/ship-simu/init.php +++ b/application/ship-simu/init.php @@ -49,9 +49,11 @@ require(PATH . "inc/output.php"); // Initialize file i/o system require(PATH . "inc/file_io.php"); +$app->setFileIOInstance($io); // Include the language sub-system require(PATH . "inc/language.php"); +$app->setLanguageInstance($lang); // This application needs a database connection then we have to simply include // the inc/database.php script diff --git a/application/ship-simu/main/companies/class_ShippingCompany.php b/application/ship-simu/main/companies/class_ShippingCompany.php index 87a4a7c..8bb077c 100644 --- a/application/ship-simu/main/companies/class_ShippingCompany.php +++ b/application/ship-simu/main/companies/class_ShippingCompany.php @@ -291,7 +291,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner // Element holen $employee = $iterator->current(); } else { - // Should�normally not happen... :( + // Should normally not happen... :( throw new StructuresOutOfBoundsException($idx, self::EXCEPTION_INDEX_OUT_OF_BOUNDS); } } diff --git a/application/ship-simu/templates/de/code/shipsimu_main.ctp b/application/ship-simu/templates/de/code/shipsimu_main.ctp new file mode 100644 index 0000000..ed9e20f --- /dev/null +++ b/application/ship-simu/templates/de/code/shipsimu_main.ctp @@ -0,0 +1,11 @@ + + +{?header:title={--WELCOME_SHIP_SIMU--}?} + +{?navigation:nav_row=home;imprint;contact?} + +
+ {--HEADER_SHIP_SIMU--} +
+ +{?footer_msg:footer_msg={--FOOTER_SHIP_SIMU--}?} diff --git a/inc/classes/exceptions/language/class_MissingFileIoHandlerException.php b/inc/classes/exceptions/language/class_MissingFileIoHandlerException.php new file mode 100644 index 0000000..9a56466 --- /dev/null +++ b/inc/classes/exceptions/language/class_MissingFileIoHandlerException.php @@ -0,0 +1,45 @@ + + * @version 0.3.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @license GNU GPL 3.0 or any newer version + * @link http://www.mxchange.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class MissingFileIoHandlerException extends FrameworkException { + /** + * The constructor + * + * @param $message Message from the exception + * @param $code Code number for the exception + * @return void + */ + public function __construct (BaseFrameworkSystem $class, $code) { + // Add a message around the missing class + $message = sprintf("[%s:%d] File i/o sub-system not initialized!", + $class->__toString(), + $this->getLine() + ); + + // Call parent constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/inc/classes/exceptions/language/class_MissingLanguageHandlerException.php b/inc/classes/exceptions/language/class_MissingLanguageHandlerException.php new file mode 100644 index 0000000..4b2ef13 --- /dev/null +++ b/inc/classes/exceptions/language/class_MissingLanguageHandlerException.php @@ -0,0 +1,45 @@ + + * @version 0.3.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @license GNU GPL 3.0 or any newer version + * @link http://www.mxchange.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class MissingLanguageHandlerException extends FrameworkException { + /** + * The constructor + * + * @param $message Message from the exception + * @param $code Code number for the exception + * @return void + */ + public function __construct (BaseFrameworkSystem $class, $code) { + // Add a message around the missing class + $message = sprintf("[%s:%d] Language sub-system not initialized!", + $class->__toString(), + $this->getLine() + ); + + // Call parent constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index 2ffe0b2..8ac8d31 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -78,6 +78,16 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { */ private $decimals = ","; // German + /** + * The language instance for the template loader + */ + private $langInstance = null; + + /** + * The file I/O instance for the template loader + */ + private $fileIOInstance = null; + /*********************** * Exception codes.... * ***********************/ @@ -127,6 +137,8 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { const EXCEPTION_ATTRIBUTES_ARE_MISSING = 0x02b; const EXCEPTION_ARRAY_ELEMENTS_MISSING = 0x02c; const EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED = 0x02d; + const EXCEPTION_MISSING_LANGUAGE_HANDLER = 0x02e; + const EXCEPTION_MISSING_FILE_IO_HANDLER = 0x02f; /** * In the super constructor these system classes shall be ignored or else @@ -751,7 +763,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { /** * Appends a trailing slash to a string * - * @param $str A string (maybe) without trailing slash + * @param $str A string (maybe) without trailing slash * @return $str A string with an auto-appended trailing slash */ public final function addMissingTrailingSlash ($str) { @@ -759,6 +771,112 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { if (substr($str, -1, 1) != "/") $str .= "/"; return $str; } + + /** + * Private getter for language instance + * + * @return $langInstance An instance to the language sub-system + */ + protected final function getLanguageInstance () { + return $this->langInstance; + } + + /** + * Setter for language instance + * + * @param $langInstance An instance to the language sub-system + * @return void + * @see LanguageSystem + */ + public final function setLanguageInstance (ManageableLanguage $langInstance) { + $this->langInstance = $langInstance; + } + + /** + * Private getter for file IO instance + * + * @return $fileIOInstance An instance to the file I/O sub-system + */ + protected final function getFileIOInstance () { + return $this->fileIOInstance; + } + + /** + * Setter for file I/O instance + * + * @param $fileIOInstance An instance to the file I/O sub-system + * @return void + */ + public final function setFileIOInstance (FileIOHandler $fileIOInstance) { + $this->fileIOInstance = $fileIOInstance; + } + + /** + * Prepare the template engine (TemplateEngine by default) for a given + * application helper instance (ApplicationHelper by default). + * + * @param $appInstance An application helper instance + * @return $tplEngine The template engine instance + * @throws NullPointerException If the template engine could not + * be initialized + * @throws UnsupportedTemplateEngineException If $tplEngine is an + * unsupported template engine + * @throws MissingLanguageHandlerException If the language sub-system + * is not yet initialized + */ + protected function prepareTemplateEngine (BaseFrameworkSystem $appInstance) { + // Generate FQFN for all application templates + $fqfn = sprintf("%s%s/%s/%s", + PATH, + $this->getConfigInstance()->readConfig("application_path"), + strtolower($appInstance->getAppShortName()), + $this->getConfigInstance()->readConfig("tpl_base_path") + ); + + // Are both instances set? + if ($appInstance->getLanguageInstance() === null) { + // Invalid language instance + throw new MissingLanguageHandlerException($appInstance, self::EXCEPTION_MISSING_LANGUAGE_HANDLER); + } elseif ($appInstance->getFileIOInstance() === null) { + // Invalid language instance + throw new MissingFileIoHandlerException($appInstance, self::EXCEPTION_MISSING_FILE_IO_HANDLER); + } + + // Initialize the template engine + $tplEngine = null; + $eval = sprintf("\$tplEngine = %s::create%s( + \"%s\", + \$appInstance->getLanguageInstance(), + \$appInstance->getFileIOInstance() +);", + $this->getConfigInstance()->readConfig("tpl_engine"), + $this->getConfigInstance()->readConfig("tpl_engine"), + $fqfn + ); + + // Debug message + if ((!is_null($this->getDebugInstance())) && (defined('DEBUG_EVAL'))) { + $this->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung:
%s

\n", + $this->__toString(), + htmlentities($eval) + )); + } + + // Run the command + eval($eval); + + // Is it a valid instance? + if (is_null($tplEngine)) { + // No class returned + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } elseif (!$tplEngine instanceof CompileableTemplate) { + // Not an object! ;-( + throw new UnsupportedTemplateEngineException($tplEngine, self::EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED); + } + + // Return the prepared instance + return $tplEngine; + } } // [EOF] diff --git a/inc/classes/main/database/classes/class_LocalFileDatabase.php b/inc/classes/main/database/classes/class_LocalFileDatabase.php index 231dfee..0dc66c6 100644 --- a/inc/classes/main/database/classes/class_LocalFileDatabase.php +++ b/inc/classes/main/database/classes/class_LocalFileDatabase.php @@ -34,11 +34,6 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend */ private $fileExtension = "serialized"; - /** - * The IO handler for file handling which should be FileIOHandler. - */ - private $ioInstance = null; - /** * The last read file's name */ @@ -111,7 +106,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend // Set save path and IO instance $dbInstance->setSavePath($savePath); - $dbInstance->setIOInstance($ioInstance); + $dbInstance->setFileIOInstance($ioInstance); // Return database instance return $dbInstance; @@ -146,15 +141,6 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend return $this->savePath; } - /** - * Getter for file extension - * - * @return $fileExtension The file extension for all file names - */ - public final function getFileExtension () { - return $this->fileExtension; - } - /** * Saves a given object to the local file system by serializing and * transparently compressing it @@ -201,7 +187,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend // Save the file to disc we don't care here if the path is there, // this must be done in later methods. - $this->getIOInstance()->saveFile($fqfn, array($this->getCompressorChannel()->getCompressorExtension(), $serialized)); + $this->getFileIOInstance()->saveFile($fqfn, array($this->getCompressorChannel()->getCompressorExtension(), $serialized)); } /** @@ -301,7 +287,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend $this->setLastFile($fqfn); // Get instance for file handler - $inputHandler = $this->getIOInstance(); + $inputHandler = $this->getFileIOInstance(); // Try to read from it. This makes it sure that the file is // readable and a valid database file @@ -324,27 +310,6 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend return $isUsed; } - /** - * Getter for the file IO instance - * - *�@return $ioInstance An instance for IO operations - * @see FileIOHandler The concrete handler for IO operations - */ - public final function getIOInstance () { - return $this->ioInstance; - } - - /** - * Setter for the file IO instance - * - * @param $ioInstance An instance for IO operations (should be - * FileIOHandler) - * @return void - */ - public final function setIOInstance (FileIOHandler $ioInstance) { - $this->ioInstance = $ioInstance; - } - /** * Setter for the last read file * @@ -406,7 +371,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend */ public final function getObjectFromCachedData ($uniqueID) { // Get instance for file handler - $inputHandler = $this->getIOInstance(); + $inputHandler = $this->getFileIOInstance(); // Get last file's name and contents $fqfn = $this->repairFQFN($this->getLastFile(), $uniqueID); diff --git a/inc/classes/main/template/class_TemplateEngine.php b/inc/classes/main/template/class_TemplateEngine.php index 2fff655..cc95918 100644 --- a/inc/classes/main/template/class_TemplateEngine.php +++ b/inc/classes/main/template/class_TemplateEngine.php @@ -71,11 +71,6 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate */ private $configVariables = array(); - /** - * The language instance which should link to an object of LanguageSystem - */ - private $langInstance = null; - /** * Loaded templates for recursive protection and detection */ @@ -152,7 +147,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate * @throws BasePathReadProtectedException If $basePath is * read-protected */ - public final static function createTemplateEngine ($basePath, $langInstance, $ioInstance) { + public final static function createTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIOHandler $ioInstance) { // Get a new instance $tplInstance = new TemplateEngine(); @@ -182,7 +177,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate // Set the language and IO instances $tplInstance->setLanguageInstance($langInstance); - $tplInstance->setIOInstance($ioInstance); + $tplInstance->setFileIOInstance($ioInstance); // Set template extensions $tplInstance->setRawTemplateExtension($cfgInstance->readConfig("raw_template_extension")); @@ -271,35 +266,6 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate $this->varStack = new FrameworkArrayObject(); } - /** - * Setter for language instance which should be LanguageSystem - * - * @param $langInstance The language instance - * @return void - */ - public final function setLanguageInstance (ManageableLanguage $langInstance) { - $this->langInstance = $langInstance; - } - - /** - * Setter for file I/O instance which should be FileIOHandler - * - * @param $ioInstance The file I/O instance - * @return void - */ - public final function setIOInstance (FileIOHandler $ioInstance) { - $this->ioInstance = $ioInstance; - } - - /** - * Getter for file I/O instance which should be FileIOHandler - * - * @return $ioInstance The file I/O instance - */ - public final function getIOInstance () { - return $this->ioInstance; - } - /** * Setter for base path * @@ -533,7 +499,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate // Construct the FQFN for the template by honoring the current language $fqfn = sprintf("%s%s/%s/%s%s", $this->getBasePath(), - $this->langInstance->getLanguageCode(), + $this->getLanguageInstance()->getLanguageCode(), $this->getTemplateType(), $template, $ext @@ -562,7 +528,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate )); // Get a input/output instance from the middleware - $ioInstance = $this->getIOInstance(); + $ioInstance = $this->getFileIOInstance(); // Validate the instance if (is_null($ioInstance)) { -- 2.39.5