3 namespace CoreFramework\Resolver\Command;
5 // Import framework stuff
6 use CoreFramework\Command\Commandable;
7 use CoreFramework\Command\InvalidCommandException;
8 use CoreFramework\Factory\ObjectFactory;
9 use CoreFramework\Generic\EmptyVariableException;
10 use CoreFramework\Request\Requestable;
11 use CoreFramework\Resolver\BaseResolver;
14 * A generic command resolver class
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class BaseCommandResolver extends BaseResolver {
37 * Protected constructor
39 * @param $className Name of the class
42 protected function __construct ($className) {
43 // Call parent constructor
44 parent::__construct($className);
48 * "Loads" a given command and instances it if not yet cached
50 * @param $commandName A command name we shall look for
51 * @return $commandInstance A loaded command instance
52 * @throws InvalidCommandException Thrown if even the default
53 * command class is missing (bad!)
55 protected function loadCommand ($commandName) {
56 // Init command instance
57 $commandInstance = NULL;
62 $this->getNamespace(),
63 $this->getCapitalizedClassPrefix(),
64 self::convertToClassName($commandName)
67 // Create command class name
68 $this->setClassName($className);
70 // Is this class loaded?
71 if (!class_exists($this->getClassName())) {
72 // Class not found, so throw an exception
73 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
76 // Initiate the command
77 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
80 return $commandInstance;
84 * Returns an command instance for a given request class or null if
87 * @param $requestInstance An instance of a Requestable class
88 * @return $commandInstance An instance of the resolved command
89 * @throws InvalidCommandException Thrown if $commandName is
91 * @throws InvalidCommandInstanceException Thrown if $commandInstance
92 * is an invalid instance
94 public function resolveCommandByRequest (Requestable $requestInstance) {
97 $commandInstance = NULL;
99 // This goes fine so let's resolve the command
100 $commandName = $requestInstance->getRequestElement('command');
102 // Is the command empty? Then fall back to default command
103 if (empty($commandName)) {
104 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
107 // Check if command is valid
108 if ($this->isCommandValid($this->getNamespace(), $commandName) === FALSE) {
109 // This command is invalid!
110 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
114 $commandInstance = $this->loadCommand($commandName);
117 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
118 // This command has an invalid instance!
119 throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
123 $this->setResolvedInstance($commandInstance);
125 // Return the resolved command instance
126 return $commandInstance;
130 * Resolves the command by its direct name and returns an instance of its class
132 * @param $commandName The direct command name we shall resolve
133 * @return $commandInstance An instance of the command class
134 * @throws InvalidCommandException Thrown if $commandName is invalid
136 public function resolveCommand ($commandName) {
137 // Initiate the instance variable
138 $commandInstance = NULL;
140 // Is the command empty? Then fall back to default command
141 if (empty($commandName)) {
142 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
145 // Check if command is valid
146 if ($this->isCommandValid($commandName) === FALSE) {
147 // This command is invalid!
148 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
152 $commandInstance = $this->loadCommand($commandName);
154 // Return the instance
155 return $commandInstance;
159 * Checks whether the given command is valid
161 * @param $namespace Namespace to look in
162 * @param $commandName The default command we shall execute
163 * @return $isValid Whether the given command is valid
164 * @throws EmptyVariableException Thrown if given command is not set
166 protected function isCommandValid ($namespace, $commandName) {
167 // By default nothing shall be valid
170 // Is namespace and command name set?
171 if (empty($namespace)) {
172 // Then thrown an exception here
173 throw new EmptyVariableException(array($this, 'namespace'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
174 } elseif (empty($commandName)) {
175 // Then thrown an exception here
176 throw new EmptyVariableException(array($this, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
179 // Create the full class name
180 $className = sprintf(
183 $this->getCapitalizedClassPrefix(),
184 self::convertToClassName($commandName)
187 // Now, let us create the full name of the command class
188 $this->setClassName($className);
190 // Is this class already loaded?
191 if (class_exists($this->getClassName())) {
192 // This class does exist. :-)
197 $this->setCommandName($commandName);