3 * A command resolver for local (non-hubbed) commands
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class WebCommandResolver extends BaseResolver implements CommandResolver {
26 * Last successfull resolved command
28 private $lastCommandInstance = "";
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
39 // Set part description
40 $this->setObjectDescription("Lokaler Kommandoauflöser");
42 // Create unique ID number
43 $this->createUniqueID();
46 $this->removeNumberFormaters();
47 $this->removeSystemArray();
49 // Set prefix to "Web"
50 $this->setCommandPrefix("Web");
54 * Creates an instance of a Web command resolver with a given default command
56 * @param $defaultCommand The default command we shall execute
57 * @param $appInstance An instance of a manageable application helper class
58 * @return $resolverInstance The prepared command resolver instance
59 * @throws EmptyVariableException Thrown if the default command is not set
60 * @throws InvalidCommandException Thrown if the default command is invalid
62 public final static function createWebCommandResolver ($defaultCommand, ManageableApplication $appInstance) {
63 // Create the new instance
64 $resolverInstance = new WebCommandResolver();
66 // Is the variable $defaultCommand set and the command is valid?
67 if (empty($defaultCommand)) {
68 // Then thrown an exception here
69 throw new EmptyVariableException(array($resolverInstance, 'defaultCommand'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
70 } elseif (!$resolverInstance->isCommandValid($defaultCommand)) {
71 // Invalid command found
72 throw new InvalidCommandException(array($resolverInstance, $defaultCommand), self::EXCEPTION_INVALID_COMMAND);
75 // Set the application instance
76 $resolverInstance->setApplicationInstance($appInstance);
78 // Return the prepared instance
79 return $resolverInstance;
83 * Returns an command instance for a given request class or null if
84 * MissingArrayElementsException was thrown
86 * @param $requestInstance An instance of a request class
87 * @return $commandInstance An instance of the resolved command
88 * @throws InvalidCommandException Thrown if $commandName is
90 * @throws InvalidCommandInstanceException Thrown if $commandInstance
91 * is an invalid instance
93 public function resolvCommandByRequest (Requestable $requestInstance) {
96 $commandInstance = null;
98 // Test if the required parameter is set
100 // This goes fine so let's resolv the command
101 $commandName = $requestInstance->getRequestElement($this->getConfigInstance()->readConfig("command_parameter"));
103 // Is the command empty? Then fall back to default command
104 if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig("default_command");
106 // Check if the command is valid
107 if (!$this->isCommandValid($commandName)) {
108 // This command is invalid!
109 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
113 $commandInstance = $this->loadCommand($commandName);
116 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
117 // This command has an invalid instance!
118 throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
122 $this->lastCommandInstance = $commandInstance;
123 } catch (MissingArrayElementsException $e) {
124 // Just catch it here...
127 // Return the resolved command instance
128 return $commandInstance;
132 * "Loads" a given command and instances it if not yet cached
134 * @param $commandName A command name we shall look for
135 * @return $commandInstance A loaded command instance
136 * @throws InvalidCommandException Thrown if even the default
137 * command class is missing (bad!)
139 private function loadCommand ($commandName) {
140 // Cache default command
141 $defaultCommand = $this->getConfigInstance()->readConfig("default_command");
143 // Init command instance
144 $commandInstance = null;
146 // Create command class name
147 $class = sprintf("Web%sCommand",
148 ucfirst(strtolower($commandName))
151 // Is this class loaded?
152 if (!class_exists($class)) {
153 // Class not found, so try the default one or throw exception
154 if ($commandName != $defaultCommand) {
155 // Try the default command
156 return $this->loadCommand($defaultCommand);
159 throw new InvalidCommandException(array($this, $defaultCommand), self::EXCEPTION_INVALID_COMMAND);
163 // Initiate the command
164 $eval = sprintf("\$commandInstance = %s::create%s(\$this);",
172 // Is the instance valid?
173 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
174 // Something is wrong
175 $commandInstance = null;
179 return $commandInstance;