From d5e40277c2607f2cb130ab9fde84f8fbdb832ce4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 24 Jun 2008 15:21:28 +0000 Subject: [PATCH] Image controller/resolver and fixes: - Controller and resolver added for images - Exceptions fixed in controller and resolver - No more fall-back on default command if requested command is missing - Misc constant fixes --- .gitattributes | 6 + .../ship-simu/class_ApplicationHelper.php | 4 +- application/ship-simu/config.php | 7 +- .../templates/de/code/captch_graphic_code.ctp | 2 +- .../class_DefaultControllerException.php | 3 + .../class_InvalidControllerException.php | 8 +- .../class_ConfigEntryNotFoundException.php | 3 + .../main/class_InvalidCommandException.php | 3 + .../main/class_BaseFrameworkSystem.php | 2 +- inc/classes/main/controller/class_ | 43 +++- inc/classes/main/controller/image/.htaccess | 1 + .../image/class_ImageDefaultController.php | 84 ++++++++ .../web/class_WebDefaultController.php | 4 +- .../main/resolver/command/image/.htaccess | 1 + .../image/class_ImageCommandResolver.php | 184 ++++++++++++++++++ .../command/web/class_WebCommandResolver.php | 20 +- .../class_BaseControllerResolver.php | 11 +- .../main/resolver/controller/image/.htaccess | 1 + .../image/class_ImageControllerResolver.php | 173 ++++++++++++++++ .../web/class_WebControllerResolver.php | 16 +- inc/config.php | 3 + 21 files changed, 536 insertions(+), 43 deletions(-) create mode 100644 inc/classes/main/controller/image/.htaccess create mode 100644 inc/classes/main/controller/image/class_ImageDefaultController.php create mode 100644 inc/classes/main/resolver/command/image/.htaccess create mode 100644 inc/classes/main/resolver/command/image/class_ImageCommandResolver.php create mode 100644 inc/classes/main/resolver/controller/image/.htaccess create mode 100644 inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php diff --git a/.gitattributes b/.gitattributes index 6eb8633..bec7301 100644 --- a/.gitattributes +++ b/.gitattributes @@ -393,6 +393,8 @@ inc/classes/main/controller/class_BaseController.php -text inc/classes/main/controller/form/.htaccess -text inc/classes/main/controller/form/class_WebDoFormController.php -text inc/classes/main/controller/form/class_WebLoginAreaController.php -text +inc/classes/main/controller/image/.htaccess -text +inc/classes/main/controller/image/class_ImageDefaultController.php -text inc/classes/main/controller/login/.htaccess -text inc/classes/main/controller/login/class_WebLoginAreaController.php -text inc/classes/main/controller/web/.htaccess -text @@ -513,10 +515,14 @@ inc/classes/main/resolver/action/web/class_WebActionResolver.php -text inc/classes/main/resolver/class_BaseResolver.php -text inc/classes/main/resolver/command/.htaccess -text inc/classes/main/resolver/command/class_BaseCommandResolver.php -text +inc/classes/main/resolver/command/image/.htaccess -text +inc/classes/main/resolver/command/image/class_ImageCommandResolver.php -text inc/classes/main/resolver/command/web/.htaccess -text inc/classes/main/resolver/command/web/class_WebCommandResolver.php -text inc/classes/main/resolver/controller/.htaccess -text inc/classes/main/resolver/controller/class_BaseControllerResolver.php -text +inc/classes/main/resolver/controller/image/.htaccess -text +inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php -text inc/classes/main/resolver/controller/web/.htaccess -text inc/classes/main/resolver/controller/web/class_WebControllerResolver.php -text inc/classes/main/response/.htaccess -text diff --git a/application/ship-simu/class_ApplicationHelper.php b/application/ship-simu/class_ApplicationHelper.php index 370bd02..89acb85 100644 --- a/application/ship-simu/class_ApplicationHelper.php +++ b/application/ship-simu/class_ApplicationHelper.php @@ -208,7 +208,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica // If it is null then get default command if (is_null($commandName)) { - $commandName = $this->getConfigInstance()->readConfig('default_command'); + $commandName = $responseInstance->getDefaultCommand(); } // END - if // Get a resolver @@ -232,7 +232,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica // Walk through all messages foreach ($messageList as $message) { print("MSG:".$message."
\n"); - } + } // END - if } } diff --git a/application/ship-simu/config.php b/application/ship-simu/config.php index d3d98f7..53dff5c 100644 --- a/application/ship-simu/config.php +++ b/application/ship-simu/config.php @@ -25,8 +25,11 @@ // Get a configuration instance for shorter lines $cfg = FrameworkConfiguration::getInstance(); -// CFG: DEFAULT-COMMAND -$cfg->setConfigEntry('default_command', "home"); +// CFG: DEFAULT-WEB-COMMAND +$cfg->setConfigEntry('default_web_command', "home"); + +// CFG: DEFAULT-IMAGE-COMMAND +$cfg->setConfigEntry('default_image_command', "build"); // CFG: PAGE-WITH-NEWS $cfg->setConfigEntry('page_with_news', "home"); diff --git a/application/ship-simu/templates/de/code/captch_graphic_code.ctp b/application/ship-simu/templates/de/code/captch_graphic_code.ctp index 0e044f0..fbf6f2e 100644 --- a/application/ship-simu/templates/de/code/captch_graphic_code.ctp +++ b/application/ship-simu/templates/de/code/captch_graphic_code.ctp @@ -9,7 +9,7 @@ $captchaHelper->flushContent(); // [EOF] ?>
- CAPTCHA-Bild + CAPTCHA-Bild
{?captcha_code?} diff --git a/inc/classes/exceptions/controller/class_DefaultControllerException.php b/inc/classes/exceptions/controller/class_DefaultControllerException.php index 5a93a29..e6be3a1 100644 --- a/inc/classes/exceptions/controller/class_DefaultControllerException.php +++ b/inc/classes/exceptions/controller/class_DefaultControllerException.php @@ -36,6 +36,9 @@ class DefaultControllerException extends FrameworkException { $this->getLine() ); + // Set extra data + $this->setExtraData($resolverInstance->getClassName()); + // Call parent contructor with message parent::__construct($message, $code); } diff --git a/inc/classes/exceptions/controller/class_InvalidControllerException.php b/inc/classes/exceptions/controller/class_InvalidControllerException.php index 4aa54c8..2546e72 100644 --- a/inc/classes/exceptions/controller/class_InvalidControllerException.php +++ b/inc/classes/exceptions/controller/class_InvalidControllerException.php @@ -31,12 +31,16 @@ class InvalidControllerException extends FrameworkException { */ public function __construct(array $msgArray, $code) { // Prepare the message - $message = sprintf("[%s:%d] Cannot resolve controller %s.", + $message = sprintf("[%s:%d] Cannot resolve controller %s (class name: %s).", $msgArray[0]->__toString(), $this->getLine(), - $msgArray[1] + $msgArray[1], + $msgArray[0]->getClassName() ); + // Set extra data + $this->setExtraData($msgArray[0]->getClassName()); + // Call parent contructor with message parent::__construct($message, $code); } diff --git a/inc/classes/exceptions/main/class_ConfigEntryNotFoundException.php b/inc/classes/exceptions/main/class_ConfigEntryNotFoundException.php index 324e991..d7d2c95 100644 --- a/inc/classes/exceptions/main/class_ConfigEntryNotFoundException.php +++ b/inc/classes/exceptions/main/class_ConfigEntryNotFoundException.php @@ -37,6 +37,9 @@ class ConfigEntryNotFoundException extends FrameworkException { $classArray[1] ); + // Set extra data + $this->setExtraData($classArray[1]); + // Call parent constructor parent::__construct($message, $code); } diff --git a/inc/classes/exceptions/main/class_InvalidCommandException.php b/inc/classes/exceptions/main/class_InvalidCommandException.php index 9a1e9e2..dd36dba 100644 --- a/inc/classes/exceptions/main/class_InvalidCommandException.php +++ b/inc/classes/exceptions/main/class_InvalidCommandException.php @@ -38,6 +38,9 @@ class InvalidCommandException extends FrameworkException { $msgArray[0]->getClassName() ); + // Set extra data + $this->setExtraData($msgArray[0]->getClassName()); + // Call parent constructor parent::__construct($message, $code); } diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index 5c96b95..b49be03 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -146,7 +146,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { const EXCEPTION_MISSING_FILE_IO_HANDLER = 0x02f; const EXCEPTION_MISSING_ELEMENT = 0x030; const EXCEPTION_HEADERS_ALREADY_SENT = 0x031; - const EXCEPTION_DEFAUL_CONTROLLER_GONE = 0x032; + const EXCEPTION_DEFAULT_CONTROLLER_GONE = 0x032; const EXCEPTION_CLASS_NOT_FOUND = 0x033; const EXCEPTION_REQUIRED_INTERFACE_MISSING = 0x034; const EXCEPTION_FATAL_ERROR = 0x035; diff --git a/inc/classes/main/controller/class_ b/inc/classes/main/controller/class_ index 46459d5..d9c2a19 100644 --- a/inc/classes/main/controller/class_ +++ b/inc/classes/main/controller/class_ @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class Controller extends BaseController implements Controller { +class ???Controller extends BaseController implements Controller { /** * Protected constructor * @@ -32,13 +32,48 @@ class Controller extends BaseController implements Controller { parent::__construct(__CLASS__); // Set part description - $this->setObjectDescription("Ein spezieller Controller"); + $this->setObjectDescription(""); // Create unique ID number $this->generateUniqueId(); + } + + /** + * Creates an instance of this class + * + * @param $resolverInstance An instance of a command resolver class + * @return $controllerInstance A prepared instance of this class + */ + public final static function create???Controller (CommandResolver $resolverInstance) { + // Create the instance + $controllerInstance = new ???Controller(); + + // Set the command resolver + $controllerInstance->setResolverInstance($resolverInstance); + + // Return the prepared instance + return $controllerInstance; + } + + /** + * Handles the given request and response + * + * @param $requestInstance An instance of a request class + * @param $responseInstance An instance of a response class + * @return void + */ + public function handleRequest (Requestable $requestInstance, Responseable $responseInstance) { + // Get the command instance + $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance); + + // This request was valid! :-D + $requestInstance->requestIsValid(); + + // Execute the command + $commandInstance->execute($requestInstance, $responseInstance); - // Clean up a little - $this->removeSystemArray(); + // Flush the response out + $responseInstance->flushBuffer(); } } diff --git a/inc/classes/main/controller/image/.htaccess b/inc/classes/main/controller/image/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/main/controller/image/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/controller/image/class_ImageDefaultController.php b/inc/classes/main/controller/image/class_ImageDefaultController.php new file mode 100644 index 0000000..0ff1ec2 --- /dev/null +++ b/inc/classes/main/controller/image/class_ImageDefaultController.php @@ -0,0 +1,84 @@ + + * @version 0.0.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.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 ImageDefaultController extends BaseController implements Controller { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set part description + $this->setObjectDescription("Default image controller"); + + // Create unique ID number + $this->generateUniqueId(); + + // Clean up a little + $this->removeSystemArray(); + } + + /** + * Creates an instance of this class + * + * @param $resolverInstance An instance of a command resolver class + * @return $controllerInstance A prepared instance of this class + */ + public final static function createImageDefaultController (CommandResolver $resolverInstance) { + // Create the instance + $controllerInstance = new ImageDefaultController(); + + // Set the command resolver + $controllerInstance->setResolverInstance($resolverInstance); + + // Return the prepared instance + return $controllerInstance; + } + + /** + * Handles the given request and response + * + * @param $requestInstance An instance of a request class + * @param $responseInstance An instance of a response class + * @return void + */ + public function handleRequest (Requestable $requestInstance, Responseable $responseInstance) { + // Get the command instance + $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance); + + // This request was valid! :-D + $requestInstance->requestIsValid(); + + // Execute the command + $commandInstance->execute($requestInstance, $responseInstance); + + // Flush the response out + $responseInstance->flushBuffer(); + } +} + +// [EOF] +?> diff --git a/inc/classes/main/controller/web/class_WebDefaultController.php b/inc/classes/main/controller/web/class_WebDefaultController.php index 7f7e862..5a10b05 100644 --- a/inc/classes/main/controller/web/class_WebDefaultController.php +++ b/inc/classes/main/controller/web/class_WebDefaultController.php @@ -1,6 +1,6 @@ * @version 0.0.0 @@ -33,7 +33,7 @@ class WebDefaultController extends BaseController implements Controller { parent::__construct(__CLASS__); // Set part description - $this->setObjectDescription("Default controller for all other requests"); + $this->setObjectDescription("Default controller for all other web requests"); // Create unique ID number $this->generateUniqueId(); diff --git a/inc/classes/main/resolver/command/image/.htaccess b/inc/classes/main/resolver/command/image/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/main/resolver/command/image/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/resolver/command/image/class_ImageCommandResolver.php b/inc/classes/main/resolver/command/image/class_ImageCommandResolver.php new file mode 100644 index 0000000..84e6a26 --- /dev/null +++ b/inc/classes/main/resolver/command/image/class_ImageCommandResolver.php @@ -0,0 +1,184 @@ + + * @version 0.0.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.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 ImageCommandResolver extends BaseCommandResolver implements CommandResolver { + /** + * Last successfull resolved command + */ + private $lastCommandInstance = null; + + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set part description + $this->setObjectDescription("Resolver for local image commands"); + + // Create unique ID number + $this->generateUniqueId(); + + // Set prefix to "Image" + $this->setCommandPrefix("Image"); + } + + /** + * Creates an instance of a Image command resolver with a given default command + * + * @param $commandName The default command we shall execute + * @param $appInstance An instance of a manageable application helper class + * @return $resolverInstance The prepared command resolver instance + * @throws EmptyVariableException Thrown if the default command is not set + * @throws InvalidCommandException Thrown if the default command is invalid + */ + public final static function createImageCommandResolver ($commandName, ManageableApplication $appInstance) { + // Create the new instance + $resolverInstance = new ImageCommandResolver(); + + // Is the variable $commandName set and the command is valid? + if (empty($commandName)) { + // Then thrown an exception here + throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); + } elseif (!$resolverInstance->isCommandValid($commandName)) { + // Invalid command found + throw new InvalidCommandException(array($resolverInstance, $commandName), self::EXCEPTION_INVALID_COMMAND); + } + + // Set the application instance + $resolverInstance->setApplicationInstance($appInstance); + + // Return the prepared instance + return $resolverInstance; + } + + /** + * Returns an command instance for a given request class or null if + * it was not found + * + * @param $requestInstance An instance of a request class + * @return $commandInstance An instance of the resolved command + * @throws InvalidCommandException Thrown if $commandName is + * invalid + * @throws InvalidCommandInstanceException Thrown if $commandInstance + * is an invalid instance + */ + public function resolveCommandByRequest (Requestable $requestInstance) { + // Init variables + $commandName = ""; + $commandInstance = null; + + // This goes fine so let's resolv the command + $commandName = $requestInstance->getRequestElement('page'); + + // Is the command empty? Then fall back to default command + if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_image_command'); + + // Check if the command is valid + if (!$this->isCommandValid($commandName)) { + // This command is invalid! + throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND); + } // END - if + + // Get the command + $commandInstance = $this->loadCommand($commandName); + + // And validate it + if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) { + // This command has an invalid instance! + throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND); + } // END - if + + // Set last command + $this->lastCommandInstance = $commandInstance; + + // Return the resolved command instance + return $commandInstance; + } + + /** + * Resolves the command by its direct name and returns an instance of its class + * + * @param $commandName The direct command name we shall resolve + * @return $commandInstance An instance of the command class + * @throws InvalidCommandException Thrown if $commandName is invalid + */ + public function resolveCommand ($commandName) { + // Initiate the instance variable + $commandInstance = null; + + // Is the command empty? Then fall back to default command + if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_image_command'); + + // Check if the command is valid + if (!$this->isCommandValid($commandName)) { + // This command is invalid! + throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND); + } + + // Get the command + $commandInstance = $this->loadCommand($commandName); + + // Return the instance + return $commandInstance; + } + + /** + * "Loads" a given command and instances it if not yet cached + * + * @param $commandName A command name we shall look for + * @return $commandInstance A loaded command instance + * @throws InvalidCommandException Thrown if even the default + * command class is missing (bad!) + */ + private function loadCommand ($commandName) { + // Cache default command + $defaultCommand = $this->getConfigInstance()->readConfig('default_image_command'); + + // Init command instance + $commandInstance = null; + + // Create command class name + $this->setClassName(sprintf("Image%sCommand", + $this->convertToClassName($commandName) + )); + + // Is this class loaded? + if (!class_exists($this->getClassName())) { + // Class not found, so throw an exception + throw new InvalidCommandException(array($this, $defaultCommand), self::EXCEPTION_INVALID_COMMAND); + } // END - if + + // Initiate the command + $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this)); + + // Return the result + return $commandInstance; + } +} + +// [EOF] +?> diff --git a/inc/classes/main/resolver/command/web/class_WebCommandResolver.php b/inc/classes/main/resolver/command/web/class_WebCommandResolver.php index acb6e52..9143b73 100644 --- a/inc/classes/main/resolver/command/web/class_WebCommandResolver.php +++ b/inc/classes/main/resolver/command/web/class_WebCommandResolver.php @@ -1,6 +1,6 @@ * @version 0.0.0 @@ -62,7 +62,7 @@ class WebCommandResolver extends BaseCommandResolver implements CommandResolver // Is the variable $commandName set and the command is valid? if (empty($commandName)) { // Then thrown an exception here - throw new EmptyVariableException(array($resolverInstance, 'defaultCommand'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); + throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); } elseif (!$resolverInstance->isCommandValid($commandName)) { // Invalid command found throw new InvalidCommandException(array($resolverInstance, $commandName), self::EXCEPTION_INVALID_COMMAND); @@ -95,7 +95,7 @@ class WebCommandResolver extends BaseCommandResolver implements CommandResolver $commandName = $requestInstance->getRequestElement('page'); // Is the command empty? Then fall back to default command - if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_command'); + if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_web_command'); // Check if the command is valid if (!$this->isCommandValid($commandName)) { @@ -131,7 +131,7 @@ class WebCommandResolver extends BaseCommandResolver implements CommandResolver $commandInstance = null; // Is the command empty? Then fall back to default command - if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_command'); + if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_web_command'); // Check if the command is valid if (!$this->isCommandValid($commandName)) { @@ -156,7 +156,7 @@ class WebCommandResolver extends BaseCommandResolver implements CommandResolver */ private function loadCommand ($commandName) { // Cache default command - $defaultCommand = $this->getConfigInstance()->readConfig('default_command'); + $defaultCommand = $this->getConfigInstance()->readConfig('default_web_command'); // Init command instance $commandInstance = null; @@ -168,14 +168,8 @@ class WebCommandResolver extends BaseCommandResolver implements CommandResolver // Is this class loaded? if (!class_exists($this->getClassName())) { - // Class not found, so try the default one or throw exception - if ($commandName != $defaultCommand) { - // Try the default command - return $this->loadCommand($defaultCommand); - } else { - // Still not found? - throw new InvalidCommandException(array($this, $defaultCommand), self::EXCEPTION_INVALID_COMMAND); - } + // Class not found, so throw an exception + throw new InvalidCommandException(array($this, $defaultCommand), self::EXCEPTION_INVALID_COMMAND); } // END - if // Initiate the command diff --git a/inc/classes/main/resolver/controller/class_BaseControllerResolver.php b/inc/classes/main/resolver/controller/class_BaseControllerResolver.php index 1b5fa1c..2081745 100644 --- a/inc/classes/main/resolver/controller/class_BaseControllerResolver.php +++ b/inc/classes/main/resolver/controller/class_BaseControllerResolver.php @@ -56,7 +56,7 @@ class BaseControllerResolver extends BaseResolver { /** * Setter for controller name * - * @param $controllerName Last validated controller name + * @param $controllerName Last validated controller name * @return void */ protected final function setControllerName ($controllerName) { @@ -68,16 +68,17 @@ class BaseControllerResolver extends BaseResolver { * * @return $controllerName Last validated controller name */ - protected final function getControllerName () { + public final function getControllerName () { return $this->controllerName; } /** * Checks wether the given controller is valid * - * @param $controllerName The default controller we shall execute - * @return $isValid Wether the given controller is valid + * @param $controllerName The default controller we shall execute + * @return $isValid Wether the given controller is valid * @throws EmptyVariableException Thrown if the given controller is not set + * @throws DefaultControllerException If the default controller was not found */ public function isControllerValid ($controllerName) { // By default nothing shall be valid @@ -112,7 +113,7 @@ class BaseControllerResolver extends BaseResolver { } } else { // All is tried, give it up here - throw new DefaultControllerException($this, self::EXCEPTION_DEFAUL_CONTROLLER_GONE); + throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE); } } // END - while diff --git a/inc/classes/main/resolver/controller/image/.htaccess b/inc/classes/main/resolver/controller/image/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/main/resolver/controller/image/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php b/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php new file mode 100644 index 0000000..9b982f8 --- /dev/null +++ b/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php @@ -0,0 +1,173 @@ + + * @version 0.0.0 + * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.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 ImageControllerResolver extends BaseControllerResolver implements ControllerResolver { + /** + * Last successfull resolved controller (name) + */ + private $lastControllerName = ""; + + /** + * Last successfull resolved controller (instance) + */ + private $lastControllerInstance = null; + + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set part description + $this->setObjectDescription("Resolver for local web controllers"); + + // Create unique ID number + $this->generateUniqueId(); + + // Set prefix to "Image" + $this->setControllerPrefix("Image"); + } + + /** + * Creates an instance of a resolver class with a given command + * + * @param $controllerName The controller we shall resolve + * @param $appInstance An instance of a manageable application helper class + * @return $resolverInstance The prepared controller resolver instance + * @throws EmptyVariableException Thrown if the default command is not set + * @throws InvalidControllerException Thrown if the default controller is invalid + */ + public final static function createImageControllerResolver ($controllerName, ManageableApplication $appInstance) { + // Create the new instance + $resolverInstance = new ImageControllerResolver(); + + // Is the variable $controllerName set and the command is valid? + if (empty($controllerName)) { + // Then thrown an exception here + throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); + } elseif (!$resolverInstance->isControllerValid($controllerName)) { + // Invalid command found + throw new InvalidControllerException(array($resolverInstance, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); + } + + // Set the application instance + $resolverInstance->setApplicationInstance($appInstance); + + // Set command name + $resolverInstance->setControllerName($controllerName); + + // Return the prepared instance + return $resolverInstance; + } + + /** + * Resolves the default controller of the given command + * + * @return $controllerInstance A controller instance for the default + * command + * @throws InvalidControllerInstanceException Thrown if $controllerInstance + * is invalid + */ + public function resolveController () { + // Init variables + $controllerName = ""; + $controllerInstance = null; + + // Get the command name + $controllerName = $this->getControllerName(); + + // Get the command + $controllerInstance = $this->loadController($controllerName); + + // And validate it + if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) { + // This command has an invalid instance! + throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); + } // END - if + + // Set last controller + $this->lastControllerInstance = $controllerInstance; + + // Return the maybe resolved instance + return $controllerInstance; + } + + /** + * "Loads" a given controller and instances it if not yet cached. If the + * controller was not found one of the default controllers will be used + * depending on wether news shall be displayed. + * + * @param $controllerName A controller name we shall look for + * @return $controllerInstance A loaded controller instance + * @throws InvalidControllerException Thrown if even the requested + * controller class is missing (bad!) + */ + private function loadController ($controllerName) { + // Debug message + //* DEBUG: */ $this->debugBackTrace(); + + // Cache default command + $defaultController = $this->getConfigInstance()->readConfig('default_image_command'); + + // Init controller instance + $controllerInstance = null; + + // Default controller + $this->setClassName("ImageDefaultController"); + + // Generate the class name + //* DEBUG: */ echo __METHOD__.": Controller=".$controllerName; + if ($controllerName != $defaultController) { + // Create controller class name + $this->setClassName(sprintf("Image%sController", + $this->convertToClassName($controllerName) + )); + } else { + // Default controller + $this->setClassName("ImageDefaultController"); + } + //* DEBUG: */ echo ", controller=".$this->getClassName()."
\n"; + + // Is this class loaded? + if (!class_exists($this->getClassName())) { + // Class not found, so try the default one or throw exception + throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); + } // END - if + + // Initiate the resolver and controller + $resolverInstance = ObjectFactory::createObjectByConfiguredName('image_cmd_resolver_class', array($controllerName, $this->getApplicationInstance())); + $controllerInstance = ObjectFactory::createObjectByName($this->getClassName(), array($resolverInstance)); + + // Remove resolver + unset($resolverInstance); + + // Return the result + return $controllerInstance; + } +} + +// [EOF] +?> diff --git a/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php b/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php index ca1aa92..777f8ed 100644 --- a/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php +++ b/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php @@ -70,7 +70,7 @@ class WebControllerResolver extends BaseControllerResolver implements Controller throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); } elseif (!$resolverInstance->isControllerValid($controllerName)) { // Invalid command found - throw new InvalidControllerException(array($resolverInstance, $controllerName), self::EXCEPTION_INVALID_COMMAND); + throw new InvalidControllerException(array($resolverInstance, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); } // Set the application instance @@ -122,7 +122,7 @@ class WebControllerResolver extends BaseControllerResolver implements Controller * * @param $controllerName A controller name we shall look for * @return $controllerInstance A loaded controller instance - * @throws DefaultControllerException Thrown if even the default + * @throws InvalidControllerException Thrown if even the requested * controller class is missing (bad!) */ private function loadController ($controllerName) { @@ -130,7 +130,7 @@ class WebControllerResolver extends BaseControllerResolver implements Controller //* DEBUG: */ $this->debugBackTrace(); // Cache default command - $defaultController = $this->getConfigInstance()->readConfig('default_command'); + $defaultController = $this->getConfigInstance()->readConfig('default_web_command'); // Init controller instance $controllerInstance = null; @@ -156,14 +156,8 @@ class WebControllerResolver extends BaseControllerResolver implements Controller // Is this class loaded? if (!class_exists($this->getClassName())) { - // Class not found, so try the default one or throw exception - if ($controllerName != $defaultController) { - // Try the default controller - return $this->loadController($defaultController); - } else { - // Still not found? - throw new DefaultControllerException($this, self::EXCEPTION_DEFAUL_CONTROLLER_GONE); - } + // Throw an exception here + throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); } // END - if // Initiate the resolver and controller diff --git a/inc/config.php b/inc/config.php index a9e567f..a23d685 100644 --- a/inc/config.php +++ b/inc/config.php @@ -227,6 +227,9 @@ $cfg->setConfigEntry('news_db_wrapper_class', "NewsDatabaseWrapper"); // CFG: WEB-CMD-RESOLVER-CLASS $cfg->setConfigEntry('web_cmd_resolver_class', "WebCommandResolver"); +// CFG: IMAGE-CMD-RESOLVER-CLASS +$cfg->setConfigEntry('image_cmd_resolver_class', "ImageCommandResolver"); + // CFG: MAILER-CLASS $cfg->setConfigEntry('mailer_class', "DebugMailer"); -- 2.39.5