3 namespace CoreFramework\Resolver\Controller;
6 * A generic command resolver class
8 * @author Roland Haeder <webmaster@shipsimu.org>
10 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.shipsimu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class BaseCommandResolver extends BaseResolver {
29 * Protected constructor
31 * @param $className Name of the class
34 protected function __construct ($className) {
35 // Call parent constructor
36 parent::__construct($className);
40 * "Loads" a given command and instances it if not yet cached
42 * @param $commandName A command name we shall look for
43 * @return $commandInstance A loaded command instance
44 * @throws InvalidCommandException Thrown if even the default
45 * command class is missing (bad!)
47 protected function loadCommand ($commandName) {
48 // Init command instance
49 $commandInstance = NULL;
52 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($commandName) . 'Command';
54 // Create command class name
55 $this->setClassName($className);
57 // Is this class loaded?
58 if (!class_exists($this->getClassName())) {
59 // Class not found, so throw an exception
60 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
63 // Initiate the command
64 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
67 return $commandInstance;
71 * Returns an command instance for a given request class or null if
74 * @param $requestInstance An instance of a request class
75 * @return $commandInstance An instance of the resolved command
76 * @throws InvalidCommandException Thrown if $commandName is
78 * @throws InvalidCommandInstanceException Thrown if $commandInstance
79 * is an invalid instance
81 public function resolveCommandByRequest (Requestable $requestInstance) {
84 $commandInstance = NULL;
86 // This goes fine so let's resolve the command
87 $commandName = $requestInstance->getRequestElement('command');
89 // Is the command empty? Then fall back to default command
90 if (empty($commandName)) {
91 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
94 // Check if command is valid
95 if ($this->isCommandValid($commandName) === FALSE) {
96 // This command is invalid!
97 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
101 $commandInstance = $this->loadCommand($commandName);
104 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
105 // This command has an invalid instance!
106 throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
110 $this->setResolvedInstance($commandInstance);
112 // Return the resolved command instance
113 return $commandInstance;
117 * Resolves the command by its direct name and returns an instance of its class
119 * @param $commandName The direct command name we shall resolve
120 * @return $commandInstance An instance of the command class
121 * @throws InvalidCommandException Thrown if $commandName is invalid
123 public function resolveCommand ($commandName) {
124 // Initiate the instance variable
125 $commandInstance = NULL;
127 // Is the command empty? Then fall back to default command
128 if (empty($commandName)) {
129 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
132 // Check if command is valid
133 if ($this->isCommandValid($commandName) === FALSE) {
134 // This command is invalid!
135 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
139 $commandInstance = $this->loadCommand($commandName);
141 // Return the instance
142 return $commandInstance;
146 * Checks whether the given command is valid
148 * @param $commandName The default command we shall execute
149 * @return $isValid Whether the given command is valid
150 * @throws EmptyVariableException Thrown if given command is not set
152 public function isCommandValid ($commandName) {
153 // By default nothing shall be valid
157 if (empty($commandName)) {
158 // Then thrown an exception here
159 throw new EmptyVariableException(array($this, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
162 // Create the full class name
163 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($commandName) . 'Command';
165 // Now, let us create the full name of the command class
166 $this->setClassName($className);
168 // Is this class already loaded?
169 if (class_exists($this->getClassName())) {
170 // This class does exist. :-)
175 $this->setCommandName($commandName);