X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=inc%2Fclasses%2Fmain%2Fresolver%2Fcommand%2Fclass_BaseCommandResolver.php;h=0994bf168f5385f0899ec765edbcdb447b50945e;hb=00f71f79dde86aa012b897bb9094f01fadf855d3;hp=1c6a94d58cc82f215e9afeb79dca3f286f5658a5;hpb=fdc6a02b5e6c2155cda61fcc345c7583b734ab85;p=core.git diff --git a/inc/classes/main/resolver/command/class_BaseCommandResolver.php b/inc/classes/main/resolver/command/class_BaseCommandResolver.php index 1c6a94d5..0994bf16 100644 --- a/inc/classes/main/resolver/command/class_BaseCommandResolver.php +++ b/inc/classes/main/resolver/command/class_BaseCommandResolver.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @@ -57,6 +57,112 @@ class BaseCommandResolver extends BaseResolver { return $this->commandName; } + /** + * "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!) + */ + protected function loadCommand ($commandName) { + // Init command instance + $commandInstance = NULL; + + // Create class name + $className = $this->getCapitalizedClassPrefix() . $this->convertToClassName($commandName) . 'Command'; + + // Create command class name + $this->setClassName($className); + + // Is this class loaded? + if (!class_exists($this->getClassName())) { + // Class not found, so throw an exception + throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND); + } // END - if + + // Initiate the command + $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this)); + + // Return the result + return $commandInstance; + } + + /** + * 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 resolve the command + $commandName = $requestInstance->getRequestElement('command'); + + // Is the command empty? Then fall back to default command + if (empty($commandName)) { + $commandName = $this->getConfigInstance()->getConfigEntry('default_html_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); + + // 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->setResolvedInstance($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()->getConfigEntry('default_html_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); + + // Return the instance + return $commandInstance; + } + /** * Checks whether the given command is valid * @@ -75,7 +181,7 @@ class BaseCommandResolver extends BaseResolver { } // END - if // Create the full class name - $className = $this->getClassPrefix() . $this->convertToClassName($commandName) . 'Command'; + $className = $this->getCapitalizedClassPrefix() . $this->convertToClassName($commandName) . 'Command'; // Now, let us create the full name of the command class $this->setClassName($className);