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\Request\Requestable;
10 use CoreFramework\Resolver\BaseResolver;
13 * A generic command resolver class
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class BaseCommandResolver extends BaseResolver {
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
47 * "Loads" a given command and instances it if not yet cached
49 * @param $commandName A command name we shall look for
50 * @return $commandInstance A loaded command instance
51 * @throws InvalidCommandException Thrown if even the default
52 * command class is missing (bad!)
54 protected function loadCommand ($commandName) {
55 // Init command instance
56 $commandInstance = NULL;
61 $this->getNamespace(),
62 $this->getCapitalizedClassPrefix(),
63 self::convertToClassName($commandName)
66 // Create command class name
67 $this->setClassName($className);
69 // Is this class loaded?
70 if (!class_exists($this->getClassName())) {
71 // Class not found, so throw an exception
72 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
75 // Initiate the command
76 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
79 return $commandInstance;
83 * Returns an command instance for a given request class or null if
86 * @param $requestInstance An instance of a Requestable 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 resolveCommandByRequest (Requestable $requestInstance) {
96 $commandInstance = NULL;
98 // This goes fine so let's resolve the command
99 $commandName = $requestInstance->getRequestElement('command');
101 // Is the command empty? Then fall back to default command
102 if (empty($commandName)) {
103 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
106 // Check if command is valid
107 if ($this->isCommandValid($commandName) === FALSE) {
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->setResolvedInstance($commandInstance);
124 // Return the resolved command instance
125 return $commandInstance;
129 * Resolves the command by its direct name and returns an instance of its class
131 * @param $commandName The direct command name we shall resolve
132 * @return $commandInstance An instance of the command class
133 * @throws InvalidCommandException Thrown if $commandName is invalid
135 public function resolveCommand ($commandName) {
136 // Initiate the instance variable
137 $commandInstance = NULL;
139 // Is the command empty? Then fall back to default command
140 if (empty($commandName)) {
141 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
144 // Check if command is valid
145 if ($this->isCommandValid($commandName) === FALSE) {
146 // This command is invalid!
147 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
151 $commandInstance = $this->loadCommand($commandName);
153 // Return the instance
154 return $commandInstance;
158 * Checks whether the given command is valid
160 * @param $namespace Namespace to look in
161 * @param $commandName The default command we shall execute
162 * @return $isValid Whether the given command is valid
163 * @throws EmptyVariableException Thrown if given command is not set
165 protected function isCommandValid ($namespace, $commandName) {
166 // By default nothing shall be valid
169 // Is namespace and command name set?
170 if (empty($namespace)) {
171 // Then thrown an exception here
172 throw new EmptyVariableException(array($this, 'namespace'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
173 } elseif (empty($commandName)) {
174 // Then thrown an exception here
175 throw new EmptyVariableException(array($this, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
178 // Create the full class name
179 $className = sprintf(
182 $this->getCapitalizedClassPrefix(),
183 self::convertToClassName($commandName)
186 // Now, let us create the full name of the command class
187 $this->setClassName($className);
189 // Is this class already loaded?
190 if (class_exists($this->getClassName())) {
191 // This class does exist. :-)
196 $this->setCommandName($commandName);