3 namespace CoreFramework\Resolver\Command;
5 // Import framework stuff
6 use CoreFramework\Command\InvalidCommandException;
7 use CoreFramework\Factory\ObjectFactory;
8 use CoreFramework\Request\Requestable;
9 use CoreFramework\Resolver\BaseResolver;
12 * A generic command resolver class
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class BaseCommandResolver extends BaseResolver {
35 * Protected constructor
37 * @param $className Name of the class
40 protected function __construct ($className) {
41 // Call parent constructor
42 parent::__construct($className);
46 * "Loads" a given command and instances it if not yet cached
48 * @param $commandName A command name we shall look for
49 * @return $commandInstance A loaded command instance
50 * @throws InvalidCommandException Thrown if even the default
51 * command class is missing (bad!)
53 protected function loadCommand ($commandName) {
54 // Init command instance
55 $commandInstance = NULL;
60 $this->getNamespace(),
61 $this->getCapitalizedClassPrefix(),
62 self::convertToClassName($commandName)
65 // Create command class name
66 $this->setClassName($className);
68 // Is this class loaded?
69 if (!class_exists($this->getClassName())) {
70 // Class not found, so throw an exception
71 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
74 // Initiate the command
75 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
78 return $commandInstance;
82 * Returns an command instance for a given request class or null if
85 * @param $requestInstance An instance of a Requestable class
86 * @return $commandInstance An instance of the resolved command
87 * @throws InvalidCommandException Thrown if $commandName is
89 * @throws InvalidCommandInstanceException Thrown if $commandInstance
90 * is an invalid instance
92 public function resolveCommandByRequest (Requestable $requestInstance) {
95 $commandInstance = NULL;
97 // This goes fine so let's resolve the command
98 $commandName = $requestInstance->getRequestElement('command');
100 // Is the command empty? Then fall back to default command
101 if (empty($commandName)) {
102 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
105 // Check if command is valid
106 if ($this->isCommandValid($commandName) === FALSE) {
107 // This command is invalid!
108 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
112 $commandInstance = $this->loadCommand($commandName);
115 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
116 // This command has an invalid instance!
117 throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
121 $this->setResolvedInstance($commandInstance);
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)) {
140 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
143 // Check if command is valid
144 if ($this->isCommandValid($commandName) === FALSE) {
145 // This command is invalid!
146 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
150 $commandInstance = $this->loadCommand($commandName);
152 // Return the instance
153 return $commandInstance;
157 * Checks whether the given command is valid
159 * @param $namespace Namespace to look in
160 * @param $commandName The default command we shall execute
161 * @return $isValid Whether the given command is valid
162 * @throws EmptyVariableException Thrown if given command is not set
164 protected function isCommandValid ($namespace, $commandName) {
165 // By default nothing shall be valid
168 // Is namespace and command name set?
169 if (empty($namespace)) {
170 // Then thrown an exception here
171 throw new EmptyVariableException(array($this, 'namespace'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
172 } elseif (empty($commandName)) {
173 // Then thrown an exception here
174 throw new EmptyVariableException(array($this, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
177 // Create the full class name
178 $className = sprintf(
181 $this->getCapitalizedClassPrefix(),
182 self::convertToClassName($commandName)
185 // Now, let us create the full name of the command class
186 $this->setClassName($className);
188 // Is this class already loaded?
189 if (class_exists($this->getClassName())) {
190 // This class does exist. :-)
195 $this->setCommandName($commandName);