From c6d8f7eb4020504335a62e07a8ead8d40868cde7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 5 Mar 2011 11:09:11 +0000 Subject: [PATCH] Host name fixed (a redirect causes an error), duplicate code rewrriten to be more generic --- .../main/console/class_ConsoleTools.php | 3 +- .../action/class_BaseActionResolver.php | 19 +----- .../action/web/class_WebActionResolver.php | 16 +++-- .../main/resolver/class_BaseResolver.php | 58 +++++++++++++++++-- .../command/class_BaseCommandResolver.php | 26 +-------- .../console/class_ConsoleCommandResolver.php | 21 ++++--- .../image/class_ImageCommandResolver.php | 16 +++-- .../command/web/class_WebCommandResolver.php | 12 ++-- .../class_BaseControllerResolver.php | 41 +++---------- .../class_ConsoleControllerResolver.php | 4 +- .../image/class_ImageControllerResolver.php | 4 +- .../web/class_WebControllerResolver.php | 4 +- 12 files changed, 110 insertions(+), 114 deletions(-) diff --git a/inc/classes/main/console/class_ConsoleTools.php b/inc/classes/main/console/class_ConsoleTools.php index dca20929..33d4d714 100644 --- a/inc/classes/main/console/class_ConsoleTools.php +++ b/inc/classes/main/console/class_ConsoleTools.php @@ -151,6 +151,7 @@ class ConsoleTools extends BaseFrameworkSystem { $helperInstance = new ConsoleTools(); // First get a socket + // @TODO Add some DNS caching here $socket = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 5); // Check if there was an error else @@ -161,7 +162,7 @@ class ConsoleTools extends BaseFrameworkSystem { // Prepare the GET request $request = 'GET /ip.php HTTP/1.0' . self::HTTP_EOL; - $request .= 'Host: www.ship-simu.org' . self::HTTP_EOL; + $request .= 'Host: ship-simu.org' . self::HTTP_EOL; $request .= 'User-Agent: ' . self::HTTP_USER_AGENT . self::HTTP_EOL; $request .= 'Connection: close' . self::HTTP_EOL; $request .= self::HTTP_EOL; diff --git a/inc/classes/main/resolver/action/class_BaseActionResolver.php b/inc/classes/main/resolver/action/class_BaseActionResolver.php index 61b60331..601b00e6 100644 --- a/inc/classes/main/resolver/action/class_BaseActionResolver.php +++ b/inc/classes/main/resolver/action/class_BaseActionResolver.php @@ -22,11 +22,6 @@ * along with this program. If not, see . */ class BaseActionResolver extends BaseResolver { - /** - * Prefix for local, remote or other resolver - */ - private $actionPrefix = ''; - /** * Validated action name */ @@ -43,16 +38,6 @@ class BaseActionResolver extends BaseResolver { parent::__construct($className); } - /** - * Setter for action prefix - * - * @param $actionPrefix Last validated actionPrefix - * @return void - */ - protected final function setActionPrefix ($actionPrefix) { - $this->actionPrefix = $actionPrefix; - } - /** * Setter for action name * @@ -90,7 +75,7 @@ class BaseActionResolver extends BaseResolver { } // END - if // Create class name - $className = $this->actionPrefix . $this->convertToClassName($actionName) . 'Action'; + $className = $this->getClassPrefix() . $this->convertToClassName($actionName) . 'Action'; // Now, let us create the full name of the action class $this->setClassName($className); @@ -118,7 +103,7 @@ class BaseActionResolver extends BaseResolver { $actionInstance = null; // Create action class name - $className = $this->actionPrefix . $this->convertToClassName($this->getActionName()) . 'Action'; + $className = $this->getClassPrefix() . $this->convertToClassName($this->getActionName()) . 'Action'; // ... and set it $this->setClassName($className); diff --git a/inc/classes/main/resolver/action/web/class_WebActionResolver.php b/inc/classes/main/resolver/action/web/class_WebActionResolver.php index de659a3e..b88d7c44 100644 --- a/inc/classes/main/resolver/action/web/class_WebActionResolver.php +++ b/inc/classes/main/resolver/action/web/class_WebActionResolver.php @@ -25,7 +25,7 @@ class WebActionResolver extends BaseActionResolver implements ActionResolver { /** * Last successfull resolved action */ - private $lastActionInstance = ''; + private $lastActionInstance = null; /** * Protected constructor @@ -37,7 +37,7 @@ class WebActionResolver extends BaseActionResolver implements ActionResolver { parent::__construct(__CLASS__); // Set prefix to "Web" - $this->setActionPrefix("Web"); + $this->setClassPrefix('Web'); } /** @@ -89,7 +89,9 @@ class WebActionResolver extends BaseActionResolver implements ActionResolver { $actionName = $requestInstance->getRequestElement('action'); // Is the action empty? Then fall back to default action - if (empty($actionName)) $actionName = $this->getConfigInstance()->getConfigEntry('default_action'); + if (empty($actionName)) { + $actionName = $this->getConfigInstance()->getConfigEntry('default_action'); + } // END - if // Check if action is valid if ($this->isActionValid($actionName) === false) { @@ -107,7 +109,7 @@ class WebActionResolver extends BaseActionResolver implements ActionResolver { } // END - if // Set last action - $this->lastActionInstance = $actionInstance; + $this->setResolvedInstance($actionInstance); // Return the resolved action instance return $actionInstance; @@ -127,13 +129,15 @@ class WebActionResolver extends BaseActionResolver implements ActionResolver { $actionName = $this->getActionName(); // Is the action empty? Then fall back to default action - if (empty($actionName)) $actionName = $this->getConfigInstance()->getConfigEntry('default_action'); + if (empty($actionName)) { + $actionName = $this->getConfigInstance()->getConfigEntry('default_action'); + } // END - if // Check if action is valid if ($this->isActionValid($actionName) === false) { // This action is invalid! throw new InvalidActionException(array($this, $actionName), self::EXCEPTION_INVALID_ACTION); - } + } // END - if // Get the action $actionInstance = $this->loadAction(); diff --git a/inc/classes/main/resolver/class_BaseResolver.php b/inc/classes/main/resolver/class_BaseResolver.php index c7cb14a2..58cc571f 100644 --- a/inc/classes/main/resolver/class_BaseResolver.php +++ b/inc/classes/main/resolver/class_BaseResolver.php @@ -27,6 +27,16 @@ class BaseResolver extends BaseFrameworkSystem { */ private $className = ''; + /** + * Prefix for class + */ + private $classPrefix = ''; + + /** + * (Last) resolved instance + */ + private $resolvedInstance = null; + // Exception constants const EXCEPTION_INVALID_COMMAND = 0x1d0; const EXCEPTION_INVALID_CONTROLLER = 0x1d1; @@ -44,6 +54,15 @@ class BaseResolver extends BaseFrameworkSystem { parent::__construct($className); } + /** + * Getter for class name + * + * @return $className Name of the class + */ + public final function getClassName () { + return $this->className; + } + /** * Setter for class name * @@ -51,16 +70,45 @@ class BaseResolver extends BaseFrameworkSystem { * @return void */ protected final function setClassName ($className) { - $this->className = $className; + $this->className = (string) $className; } /** - * Getter for class name + * Getter for class prefix * - * @return $className Name of the class + * @return $classPrefix Last validated classPrefix */ - public final function getClassName () { - return $this->className; + protected final function getClassPrefix () { + return $this->classPrefix; + } + + /** + * Setter for class prefix + * + * @param $classPrefix Last validated classPrefix + * @return void + */ + protected final function setClassPrefix ($classPrefix) { + $this->classPrefix = (string) $classPrefix; + } + + /** + * Getter for (last) resolved instance + * + * @return $resolvedInstance Last validated resolvedInstance + */ + protected final function getResolvedInstance () { + return $this->resolvedInstance; + } + + /** + * Setter for (last) resolved instance + * + * @param $resolvedInstance (Last) validated resolved instance + * @return void + */ + protected final function setResolvedInstance (FrameworkInterface $resolvedInstance) { + $this->resolvedInstance = $resolvedInstance; } } diff --git a/inc/classes/main/resolver/command/class_BaseCommandResolver.php b/inc/classes/main/resolver/command/class_BaseCommandResolver.php index 6bef7f05..02d09ccd 100644 --- a/inc/classes/main/resolver/command/class_BaseCommandResolver.php +++ b/inc/classes/main/resolver/command/class_BaseCommandResolver.php @@ -22,11 +22,6 @@ * along with this program. If not, see . */ class BaseCommandResolver extends BaseResolver { - /** - * Prefix for local, remote or other resolver - */ - private $commandPrefix = ''; - /** * Validated command name */ @@ -43,25 +38,6 @@ class BaseCommandResolver extends BaseResolver { parent::__construct($className); } - /** - * Getter for command prefix - * - * @return $commandPrefix Last validated commandPrefix - */ - protected final function getCommandPrefix () { - return $this->commandPrefix; - } - - /** - * Setter for command prefix - * - * @param $commandPrefix Last validated commandPrefix - * @return void - */ - protected final function setCommandPrefix ($commandPrefix) { - $this->commandPrefix = $commandPrefix; - } - /** * Setter for command name * @@ -99,7 +75,7 @@ class BaseCommandResolver extends BaseResolver { } // END - if // Create the full class name - $className = $this->getCommandPrefix() . $this->convertToClassName($commandName) . 'Command'; + $className = $this->getClassPrefix() . $this->convertToClassName($commandName) . 'Command'; // Now, let us create the full name of the command class $this->setClassName($className); diff --git a/inc/classes/main/resolver/command/console/class_ConsoleCommandResolver.php b/inc/classes/main/resolver/command/console/class_ConsoleCommandResolver.php index 6611129a..8aefd8cf 100644 --- a/inc/classes/main/resolver/command/console/class_ConsoleCommandResolver.php +++ b/inc/classes/main/resolver/command/console/class_ConsoleCommandResolver.php @@ -22,11 +22,6 @@ * along with this program. If not, see . */ class ConsoleCommandResolver extends BaseCommandResolver implements CommandResolver { - /** - * Last successfull resolved command - */ - private $lastCommandInstance = null; - /** * Protected constructor * @@ -37,7 +32,7 @@ class ConsoleCommandResolver extends BaseCommandResolver implements CommandResol parent::__construct(__CLASS__); // Set prefix to "Console" - $this->setCommandPrefix('Console'); + $this->setClassPrefix('Console'); } /** @@ -89,7 +84,9 @@ class ConsoleCommandResolver extends BaseCommandResolver implements CommandResol $commandName = $requestInstance->getRequestElement('command'); // Is the command empty? Then fall back to default command - if (empty($commandName)) $commandName = $this->getConfigInstance()->getConfigEntry('default_console_command'); + if (empty($commandName)) { + $commandName = $this->getConfigInstance()->getConfigEntry('default_console_command'); + } // END - if // Check if command is valid if ($this->isCommandValid($commandName) === false) { @@ -107,7 +104,7 @@ class ConsoleCommandResolver extends BaseCommandResolver implements CommandResol } // END - if // Set last command - $this->lastCommandInstance = $commandInstance; + $this->setResolvedInstance($commandInstance); // Return the resolved command instance return $commandInstance; @@ -125,13 +122,15 @@ class ConsoleCommandResolver extends BaseCommandResolver implements CommandResol $commandInstance = null; // Is the command empty? Then fall back to default command - if (empty($commandName)) $commandName = $this->getConfigInstance()->getConfigEntry('default_console_command'); + if (empty($commandName)) { + $commandName = $this->getConfigInstance()->getConfigEntry('default_console_command'); + } // END - if // Check if command is valid if ($this->isCommandValid($commandName) === false) { // This command is invalid! throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND); - } + } // END - if // Get the command $commandInstance = $this->loadCommand($commandName); @@ -153,7 +152,7 @@ class ConsoleCommandResolver extends BaseCommandResolver implements CommandResol $commandInstance = null; // Create class name - $className = $this->getCommandPrefix() . $this->convertToClassName($commandName) . 'Command'; + $className = $this->getClassPrefix() . $this->convertToClassName($commandName) . 'Command'; // Create command class name $this->setClassName($className); diff --git a/inc/classes/main/resolver/command/image/class_ImageCommandResolver.php b/inc/classes/main/resolver/command/image/class_ImageCommandResolver.php index fee6f4fe..cc571227 100644 --- a/inc/classes/main/resolver/command/image/class_ImageCommandResolver.php +++ b/inc/classes/main/resolver/command/image/class_ImageCommandResolver.php @@ -37,7 +37,7 @@ class ImageCommandResolver extends BaseCommandResolver implements CommandResolve parent::__construct(__CLASS__); // Set prefix to "Image" - $this->setCommandPrefix('Image'); + $this->setClassPrefix('Image'); } /** @@ -89,7 +89,9 @@ class ImageCommandResolver extends BaseCommandResolver implements CommandResolve $commandName = $requestInstance->getRequestElement('page'); // Is the command empty? Then fall back to default command - if (empty($commandName)) $commandName = $this->getConfigInstance()->getConfigEntry('default_image_command'); + if (empty($commandName)) { + $commandName = $this->getConfigInstance()->getConfigEntry('default_image_command'); + } // END - if // Check if command is valid if ($this->isCommandValid($commandName) === false) { @@ -107,7 +109,7 @@ class ImageCommandResolver extends BaseCommandResolver implements CommandResolve } // END - if // Set last command - $this->lastCommandInstance = $commandInstance; + $this->setResolvedInstance($commandInstance); // Return the resolved command instance return $commandInstance; @@ -125,13 +127,15 @@ class ImageCommandResolver extends BaseCommandResolver implements CommandResolve $commandInstance = null; // Is the command empty? Then fall back to default command - if (empty($commandName)) $commandName = $this->getConfigInstance()->getConfigEntry('default_image_command'); + if (empty($commandName)) { + $commandName = $this->getConfigInstance()->getConfigEntry('default_image_command'); + } // END - if // Check if command is valid if ($this->isCommandValid($commandName) === false) { // This command is invalid! throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND); - } + } // END - if // Get the command $commandInstance = $this->loadCommand($commandName); @@ -153,7 +157,7 @@ class ImageCommandResolver extends BaseCommandResolver implements CommandResolve $commandInstance = null; // Create class name - $className = $this->getCommandPrefix() . $this->convertToClassName($commandName) . 'Command'; + $className = $this->getClassPrefix() . $this->convertToClassName($commandName) . 'Command'; // Is this class loaded? if (!class_exists($this->getClassName())) { diff --git a/inc/classes/main/resolver/command/web/class_WebCommandResolver.php b/inc/classes/main/resolver/command/web/class_WebCommandResolver.php index fd51fc38..a19ff10c 100644 --- a/inc/classes/main/resolver/command/web/class_WebCommandResolver.php +++ b/inc/classes/main/resolver/command/web/class_WebCommandResolver.php @@ -37,7 +37,7 @@ class WebCommandResolver extends BaseCommandResolver implements CommandResolver parent::__construct(__CLASS__); // Set prefix to "Web" - $this->setCommandPrefix('Web'); + $this->setClassPrefix('Web'); } /** @@ -89,7 +89,9 @@ 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()->getConfigEntry('default_web_command'); + if (empty($commandName)) { + $commandName = $this->getConfigInstance()->getConfigEntry('default_web_command'); + } // END - if // Check if command is valid if ($this->isCommandValid($commandName) === false) { @@ -107,7 +109,7 @@ class WebCommandResolver extends BaseCommandResolver implements CommandResolver } // END - if // Set last command - $this->lastCommandInstance = $commandInstance; + $this->setResolvedInstance($commandInstance); // Return the resolved command instance return $commandInstance; @@ -125,7 +127,9 @@ class WebCommandResolver extends BaseCommandResolver implements CommandResolver $commandInstance = null; // Is the command empty? Then fall back to default command - if (empty($commandName)) $commandName = $this->getConfigInstance()->getConfigEntry('default_web_command'); + if (empty($commandName)) { + $commandName = $this->getConfigInstance()->getConfigEntry('default_web_command'); + } // END - if // Check if command is valid if ($this->isCommandValid($commandName) === false) { diff --git a/inc/classes/main/resolver/controller/class_BaseControllerResolver.php b/inc/classes/main/resolver/controller/class_BaseControllerResolver.php index 0c39b385..3cef50bb 100644 --- a/inc/classes/main/resolver/controller/class_BaseControllerResolver.php +++ b/inc/classes/main/resolver/controller/class_BaseControllerResolver.php @@ -22,11 +22,6 @@ * along with this program. If not, see . */ class BaseControllerResolver extends BaseResolver { - /** - * Prefix for local, remote or other resolver - */ - private $controllerPrefix = ''; - /** * Validated controller name */ @@ -43,26 +38,6 @@ class BaseControllerResolver extends BaseResolver { parent::__construct($className); } - /** - * Setter for controller prefix - * - * @param $controllerPrefix Last validated controllerPrefix - * @return void - */ - protected final function setControllerPrefix ($controllerPrefix) { - $this->controllerPrefix = $controllerPrefix; - } - - /** - * Getter for controller prefix - * - * @param $controllerPrefix Last validated controllerPrefix - * @return void - */ - protected final function getControllerPrefix () { - return $this->controllerPrefix; - } - /** * Setter for controller name * @@ -94,25 +69,25 @@ class BaseControllerResolver extends BaseResolver { */ protected function loadController ($controllerName) { // Cache default command - $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getControllerPrefix()) . '_command'); + $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_command'); // Init controller instance $controllerInstance = null; // Default controller - $this->setClassName($this->getControllerPrefix() . 'DefaultNewsController'); + $this->setClassName($this->getClassPrefix() . 'DefaultNewsController'); // Generate the class name //* DEBUG: */ echo __METHOD__.": Controller=".$controllerName; if ($controllerName != $defaultController) { // Create controller class name - $className = $this->getControllerPrefix() . '' . $this->convertToClassName($controllerName) . 'Controller'; + $className = $this->getClassPrefix() . $this->convertToClassName($controllerName) . 'Controller'; // ... and set it $this->setClassName($className); } else { // No news at main command or non-news command - $this->setClassName($this->getControllerPrefix() . 'DefaultNewsController'); + $this->setClassName($this->getClassPrefix() . 'DefaultNewsController'); } //* DEBUG: */ echo ", controller=".$this->getClassName()."
\n"; @@ -126,7 +101,7 @@ class BaseControllerResolver extends BaseResolver { $resolverConfigEntry = ''; // Try to read a config entry for our resolver including controller name... ;-) - $resolverConfigEntry = sprintf("%s_cmd_%s_resolver_class", strtolower($this->getControllerPrefix()), strtolower($controllerName)); + $resolverConfigEntry = sprintf("%s_cmd_%s_resolver_class", strtolower($this->getClassPrefix()), strtolower($controllerName)); // Get the config, this will throw an exception if there is no special command resolver $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry); @@ -167,7 +142,7 @@ class BaseControllerResolver extends BaseResolver { } // END - if // Create class name - $className = $this->controllerPrefix . $this->convertToClassName($controllerName) . 'Controller'; + $className = $this->getClassPrefix() . $this->convertToClassName($controllerName) . 'Controller'; // Now, let us create the full name of the controller class $this->setClassName($className); @@ -178,9 +153,9 @@ class BaseControllerResolver extends BaseResolver { if (class_exists($this->getClassName())) { // This class does exist. :-) $isValid = true; - } elseif ($this->getClassName() != $this->controllerPrefix.'DefaultNewsController') { + } elseif ($this->getClassName() != $this->getClassPrefix() . 'DefaultNewsController') { // Set default controller - $this->setClassName($this->controllerPrefix . 'DefaultNewsController'); + $this->setClassName($this->getClassPrefix() . 'DefaultNewsController'); } else { // All is tried, give it up here throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE); diff --git a/inc/classes/main/resolver/controller/console/class_ConsoleControllerResolver.php b/inc/classes/main/resolver/controller/console/class_ConsoleControllerResolver.php index 9df8266a..6803513c 100644 --- a/inc/classes/main/resolver/controller/console/class_ConsoleControllerResolver.php +++ b/inc/classes/main/resolver/controller/console/class_ConsoleControllerResolver.php @@ -42,7 +42,7 @@ class ConsoleControllerResolver extends BaseControllerResolver implements Contro parent::__construct(__CLASS__); // Set prefix to "Console" - $this->setControllerPrefix('Console'); + $this->setClassPrefix('Console'); } /** @@ -103,7 +103,7 @@ class ConsoleControllerResolver extends BaseControllerResolver implements Contro } // END - if // Set last controller - $this->lastControllerInstance = $controllerInstance; + $this->setResolvedInstance($controllerInstance); // Return the maybe resolved instance return $controllerInstance; diff --git a/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php b/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php index 0c3737e2..41d66fac 100644 --- a/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php +++ b/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php @@ -42,7 +42,7 @@ class ImageControllerResolver extends BaseControllerResolver implements Controll parent::__construct(__CLASS__); // Set prefix to 'Image' - $this->setControllerPrefix('Image'); + $this->setClassPrefix('Image'); } /** @@ -103,7 +103,7 @@ class ImageControllerResolver extends BaseControllerResolver implements Controll } // END - if // Set last controller - $this->lastControllerInstance = $controllerInstance; + $this->setResolvedInstance($controllerInstance); // Return the maybe resolved instance return $controllerInstance; diff --git a/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php b/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php index 157b1b56..8cf46cc4 100644 --- a/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php +++ b/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php @@ -42,7 +42,7 @@ class WebControllerResolver extends BaseControllerResolver implements Controller parent::__construct(__CLASS__); // Set prefix to 'Web' - $this->setControllerPrefix('Web'); + $this->setClassPrefix('Web'); } /** @@ -103,7 +103,7 @@ class WebControllerResolver extends BaseControllerResolver implements Controller } // END - if // Set last controller - $this->lastControllerInstance = $controllerInstance; + $this->setResolvedInstance($controllerInstance); // Return the maybe resolved instance return $controllerInstance; -- 2.39.5