+++ /dev/null
-<?php
-/**
- * A resolver for resolving controllers locally
- *
- * @author Roland Haeder <webmaster@ship-simu.org>
- * @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 <http://www.gnu.org/licenses/>.
- */
-class LocalControllerResolver extends BaseResolver implements ControllerResolver {
- /**
- * Last successfull resolved controller
- */
- private $lastControllerName = "";
-
- /**
- * Private constructor
- *
- * @return void
- */
- protected function __construct () {
- // Call parent constructor
- parent::__construct(__CLASS__);
-
- // Set part description
- $this->setObjectDescription("");
-
- // Create unique ID number
- $this->createUniqueID();
-
- // Clean up a little
- $this->removeNumberFormaters();
- $this->removeSystemArray();
-
- // Set prefix to "Local"
- $this->setCommandPrefix("Local");
- }
-
- /**
- * 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 createLocalControllerResolver ($commandName, ManageableApplication $appInstance) {
- // Create the new instance
- $resolverInstance = new LocalControllerResolver();
-
- // 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 resolveDefaultController () {
- // 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 command
- $this->lastCommandInstance = $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
- *
- * @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) {
- // Cache default command
- $defaultCommand = $this->getConfigInstance()->readConfig("default_command");
-
- // Init controller instance
- $controllerInstance = null;
-
- // Default controller
- $class = "LocalDefaultController";
-
- // Generate the class name
- if ($commandName != $defaultCommand) {
- // Create controller class name
- $class = sprintf("Local%sController",
- ucfirst(strtolower($commandName))
- );
- } elseif ($this->getConfigInstance()->readConfig("home_with_news") == "Y") {
- // Yes, display news in home then set default controller with news
- $class = "LocalDefaultNewsController";
- }
-
- // 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);
- }
- }
-
- // Initiate the controller
- $eval = sprintf("\$controllerInstance = %s::create%s(LocalCommandResolver::createLocalCommandResolver(\$commandName, \$this->getApplicationInstance()));",
- $class,
- $class
- );
-
- // Run the command
- eval($eval);
-
- // Is the instance valid?
- if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
- // Something is wrong
- $controllerInstance = null;
- }
-
- // Return the result
- return $controllerInstance;
- }
-}
-
-// [EOF]
-?>