X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fresolver%2Fweb%2Fclass_WebCommandResolver.php;h=fa52125d68a944b13b6a37e3fac32c6b9fabea51;hb=65d72098d3ec6a0ef7782668264fc6f33f9ebeb6;hp=30b31b5240dd13284fd9235720fb0b5ae4cecaaf;hpb=6de36357d5ad2ef5ff14094f180894492420c9bf;p=shipsimu.git diff --git a/inc/classes/main/resolver/web/class_WebCommandResolver.php b/inc/classes/main/resolver/web/class_WebCommandResolver.php index 30b31b5..fa52125 100644 --- a/inc/classes/main/resolver/web/class_WebCommandResolver.php +++ b/inc/classes/main/resolver/web/class_WebCommandResolver.php @@ -28,7 +28,7 @@ class WebCommandResolver extends BaseResolver implements CommandResolver { private $lastCommandInstance = ""; /** - * Private constructor + * Protected constructor * * @return void */ @@ -37,14 +37,10 @@ class WebCommandResolver extends BaseResolver implements CommandResolver { parent::__construct(__CLASS__); // Set part description - $this->setObjectDescription("Lokaler Kommandoauflöser"); + $this->setObjectDescription("Resolver for local web commands"); // Create unique ID number - $this->createUniqueID(); - - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); + $this->generateUniqueId(); // Set prefix to "Web" $this->setCommandPrefix("Web"); @@ -53,23 +49,23 @@ class WebCommandResolver extends BaseResolver implements CommandResolver { /** * Creates an instance of a Web command resolver with a given default command * - * @param $defaultCommand The default command we shall execute + * @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 createWebCommandResolver ($defaultCommand, ManageableApplication $appInstance) { + public final static function createWebCommandResolver ($commandName, ManageableApplication $appInstance) { // Create the new instance $resolverInstance = new WebCommandResolver(); - // Is the variable $defaultCommand set and the command is valid? - if (empty($defaultCommand)) { + // 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); - } elseif (!$resolverInstance->isCommandValid($defaultCommand)) { + } elseif (!$resolverInstance->isCommandValid($commandName)) { // Invalid command found - throw new InvalidCommandException(array($resolverInstance, $defaultCommand), self::EXCEPTION_INVALID_COMMAND); + throw new InvalidCommandException(array($resolverInstance, $commandName), self::EXCEPTION_INVALID_COMMAND); } // Set the application instance @@ -98,10 +94,10 @@ class WebCommandResolver extends BaseResolver implements CommandResolver { // Test if the required parameter is set try { // This goes fine so let's resolv the command - $commandName = $requestInstance->getRequestElement($this->getConfigInstance()->readConfig("command_parameter")); + $commandName = $requestInstance->getRequestElement($this->getConfigInstance()->readConfig('command_parameter')); // 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_command'); // Check if the command is valid if (!$this->isCommandValid($commandName)) { @@ -128,6 +124,33 @@ class WebCommandResolver extends BaseResolver implements CommandResolver { 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_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 * @@ -138,18 +161,18 @@ class WebCommandResolver extends BaseResolver implements CommandResolver { */ private function loadCommand ($commandName) { // Cache default command - $defaultCommand = $this->getConfigInstance()->readConfig("default_command"); + $defaultCommand = $this->getConfigInstance()->readConfig('default_command'); // Init command instance $commandInstance = null; // Create command class name - $class = sprintf("Web%sCommand", - ucfirst(strtolower($commandName)) + $className = sprintf("Web%sCommand", + $this->convertToClassName($commandName) ); // Is this class loaded? - if (!class_exists($class)) { + if (!class_exists($className)) { // Class not found, so try the default one or throw exception if ($commandName != $defaultCommand) { // Try the default command @@ -161,19 +184,7 @@ class WebCommandResolver extends BaseResolver implements CommandResolver { } // Initiate the command - $eval = sprintf("\$commandInstance = %s::create%s(\$this);", - $class, - $class - ); - - // Run the command - eval($eval); - - // Is the instance valid? - if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) { - // Something is wrong - $commandInstance = null; - } + $commandInstance = ObjectFactory::createObjectByName($className, array($this)); // Return the result return $commandInstance;