3 namespace CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
9 * A generic command resolver class
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class BaseCommandResolver extends BaseResolver {
32 * Protected constructor
34 * @param $className Name of the class
37 protected function __construct ($className) {
38 // Call parent constructor
39 parent::__construct($className);
43 * "Loads" a given command and instances it if not yet cached
45 * @param $commandName A command name we shall look for
46 * @return $commandInstance A loaded command instance
47 * @throws InvalidCommandException Thrown if even the default
48 * command class is missing (bad!)
50 protected function loadCommand ($commandName) {
51 // Init command instance
52 $commandInstance = NULL;
55 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($commandName) . 'Command';
57 // Create command class name
58 $this->setClassName($className);
60 // Is this class loaded?
61 if (!class_exists($this->getClassName())) {
62 // Class not found, so throw an exception
63 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
66 // Initiate the command
67 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
70 return $commandInstance;
74 * Returns an command instance for a given request class or null if
77 * @param $requestInstance An instance of a request class
78 * @return $commandInstance An instance of the resolved command
79 * @throws InvalidCommandException Thrown if $commandName is
81 * @throws InvalidCommandInstanceException Thrown if $commandInstance
82 * is an invalid instance
84 public function resolveCommandByRequest (Requestable $requestInstance) {
87 $commandInstance = NULL;
89 // This goes fine so let's resolve the command
90 $commandName = $requestInstance->getRequestElement('command');
92 // Is the command empty? Then fall back to default command
93 if (empty($commandName)) {
94 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
97 // Check if command is valid
98 if ($this->isCommandValid($commandName) === FALSE) {
99 // This command is invalid!
100 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
104 $commandInstance = $this->loadCommand($commandName);
107 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
108 // This command has an invalid instance!
109 throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
113 $this->setResolvedInstance($commandInstance);
115 // Return the resolved command instance
116 return $commandInstance;
120 * Resolves the command by its direct name and returns an instance of its class
122 * @param $commandName The direct command name we shall resolve
123 * @return $commandInstance An instance of the command class
124 * @throws InvalidCommandException Thrown if $commandName is invalid
126 public function resolveCommand ($commandName) {
127 // Initiate the instance variable
128 $commandInstance = NULL;
130 // Is the command empty? Then fall back to default command
131 if (empty($commandName)) {
132 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
135 // Check if command is valid
136 if ($this->isCommandValid($commandName) === FALSE) {
137 // This command is invalid!
138 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
142 $commandInstance = $this->loadCommand($commandName);
144 // Return the instance
145 return $commandInstance;
149 * Checks whether the given command is valid
151 * @param $commandName The default command we shall execute
152 * @return $isValid Whether the given command is valid
153 * @throws EmptyVariableException Thrown if given command is not set
155 public function isCommandValid ($commandName) {
156 // By default nothing shall be valid
160 if (empty($commandName)) {
161 // Then thrown an exception here
162 throw new EmptyVariableException(array($this, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
165 // Create the full class name
166 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($commandName) . 'Command';
168 // Now, let us create the full name of the command class
169 $this->setClassName($className);
171 // Is this class already loaded?
172 if (class_exists($this->getClassName())) {
173 // This class does exist. :-)
178 $this->setCommandName($commandName);