* @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 WebControllerResolver extends BaseResolver 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 "Web"
$this->setCommandPrefix("Web");
}
/**
* Creates an instance of a resolver class with a given 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 createWebControllerResolver ($commandName, ManageableApplication $appInstance) {
// Create the new instance
$resolverInstance = new WebControllerResolver();
// 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;
}
/**
* Resolves the default controller of the given command
*
* @return $controllerInstance A controller instance for the default
* command
* @throws InvalidCommandException Thrown if $commandName is
* invalid
* @throws InvalidControllerInstanceException Thrown if $commandInstance
* is invalid
*/
public function resolveCommandController () {
// Init variables
$commandName = "";
$controllerInstance = null;
// Try to resolv the command
try {
// Get the command name
$commandName = $this->getCommandName();
// 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
$controllerInstance = $this->loadController($commandName);
// And validate it
if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
// This command has an invalid instance!
throw new InvalidControllerInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_CONTROLLER);
}
// Set last controller
$this->lastControllerInstance = $controllerInstance;
} catch (MissingArrayElementsException $e) {
// Just catch it here...
}
// 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 $commandName A controller name we shall look for
* @return $controllerInstance A loaded controller instance
* @throws DefaultControllerException Thrown if even the default
* controller class is missing (bad!)
*/
private function loadController ($commandName) {
// Debug message
//print("----- ".__METHOD__." -----
");
//debug_print_backtrace();
//print("
");
//
// Cache default command
$defaultCommand = $this->getConfigInstance()->readConfig('default_command');
// Init controller instance
$controllerInstance = null;
// Default controller
$class = "WebDefaultController";
// Generate the class name
//* DEBUG: */ echo __METHOD__.": Command=".$commandName;
if ($commandName != $defaultCommand) {
// Create controller class name
$class = sprintf("Web%sController",
$this->convertToClassName($commandName)
);
} elseif ($this->getConfigInstance()->readConfig('home_with_news') == "Y") {
// Yes, display news in home then set default controller with news
$class = "WebDefaultNewsController";
} else {
// No nes at "home" page
$class = "WebDefaultController";
}
//* DEBUG: */ echo ", controller=".$class."
\n";
// Is this class loaded?
if (!class_exists($class)) {
// Class not found, so try the default one or throw exception
if ($commandName != $defaultCommand) {
// Try the default controller
return $this->loadController($defaultCommand);
} else {
// Still not found?
throw new DefaultControllerException($this, self::EXCEPTION_DEFAUL_CONTROLLER_GONE);
}
} // END - if
// Initiate the resolver and controller
$resolverInstance = ObjectFactory::createObjectByConfiguredName('web_cmd_resolver_class', array($commandName, $this->getApplicationInstance()));
$controllerInstance = ObjectFactory::createObjectByName($class, array($resolverInstance));
// Remove resolver
unset($resolverInstance);
// Return the result
return $controllerInstance;
}
}
// [EOF]
?>