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("Resolver for local web commands");
42 // Create unique ID number
43 $this->createUniqueID();
45 // Set prefix to "Web"
46 $this->setCommandPrefix("Web");
50 * Creates an instance of a Web command resolver with a given default command
52 * @param $commandName The default command we shall execute
53 * @param $appInstance An instance of a manageable application helper class
54 * @return $resolverInstance The prepared command resolver instance
55 * @throws EmptyVariableException Thrown if the default command is not set
56 * @throws InvalidCommandException Thrown if the default command is invalid
58 public final static function createWebCommandResolver ($commandName, ManageableApplication $appInstance) {
59 // Create the new instance
60 $resolverInstance = new WebCommandResolver();
62 // Is the variable $commandName set and the command is valid?
63 if (empty($commandName)) {
64 // Then thrown an exception here
65 throw new EmptyVariableException(array($resolverInstance, 'defaultCommand'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
66 } elseif (!$resolverInstance->isCommandValid($commandName)) {
67 // Invalid command found
68 throw new InvalidCommandException(array($resolverInstance, $commandName), self::EXCEPTION_INVALID_COMMAND);
71 // Set the application instance
72 $resolverInstance->setApplicationInstance($appInstance);
74 // Return the prepared instance
75 return $resolverInstance;
79 * Returns an command instance for a given request class or null if
80 * MissingArrayElementsException was thrown
82 * @param $requestInstance An instance of a request class
83 * @return $commandInstance An instance of the resolved command
84 * @throws InvalidCommandException Thrown if $commandName is
86 * @throws InvalidCommandInstanceException Thrown if $commandInstance
87 * is an invalid instance
89 public function resolvCommandByRequest (Requestable $requestInstance) {
92 $commandInstance = null;
94 // Test if the required parameter is set
96 // This goes fine so let's resolv the command
97 $commandName = $requestInstance->getRequestElement($this->getConfigInstance()->readConfig('command_parameter'));
99 // Is the command empty? Then fall back to default command
100 if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_command');
102 // Check if the command is valid
103 if (!$this->isCommandValid($commandName)) {
104 // This command is invalid!
105 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
109 $commandInstance = $this->loadCommand($commandName);
112 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
113 // This command has an invalid instance!
114 throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
118 $this->lastCommandInstance = $commandInstance;
119 } catch (MissingArrayElementsException $e) {
120 // Just catch it here...
123 // Return the resolved command instance
124 return $commandInstance;
128 * Resolves the command by its direct name and returns an instance of its class
130 * @param $commandName The direct command name we shall resolve
131 * @return $commandInstance An instance of the command class
132 * @throws InvalidCommandException Thrown if $commandName is invalid
134 public function resolveCommand ($commandName) {
135 // Initiate the instance variable
136 $commandInstance = null;
138 // Is the command empty? Then fall back to default command
139 if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_command');
141 // Check if the command is valid
142 if (!$this->isCommandValid($commandName)) {
143 // This command is invalid!
144 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
148 $commandInstance = $this->loadCommand($commandName);
150 // Return the instance
151 return $commandInstance;
155 * "Loads" a given command and instances it if not yet cached
157 * @param $commandName A command name we shall look for
158 * @return $commandInstance A loaded command instance
159 * @throws InvalidCommandException Thrown if even the default
160 * command class is missing (bad!)
162 private function loadCommand ($commandName) {
163 // Cache default command
164 $defaultCommand = $this->getConfigInstance()->readConfig('default_command');
166 // Init command instance
167 $commandInstance = null;
169 // Create command class name
170 $className = sprintf("Web%sCommand",
171 $this->convertToClassName($commandName)
174 // Is this class loaded?
175 if (!class_exists($className)) {
176 // Class not found, so try the default one or throw exception
177 if ($commandName != $defaultCommand) {
178 // Try the default command
179 return $this->loadCommand($defaultCommand);
182 throw new InvalidCommandException(array($this, $defaultCommand), self::EXCEPTION_INVALID_COMMAND);
186 // Initiate the command
187 $eval = sprintf("\$commandInstance = %s::create%s(\$this);",
195 // Is the instance valid?
196 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
197 // Something is wrong
198 $commandInstance = null;
202 return $commandInstance;