3 namespace CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Request\Requestable;
8 use CoreFramework\Resolver\BaseResolver;
11 * A generic command resolver class
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class BaseCommandResolver extends BaseResolver {
34 * Protected constructor
36 * @param $className Name of the class
39 protected function __construct ($className) {
40 // Call parent constructor
41 parent::__construct($className);
45 * "Loads" a given command and instances it if not yet cached
47 * @param $commandName A command name we shall look for
48 * @return $commandInstance A loaded command instance
49 * @throws InvalidCommandException Thrown if even the default
50 * command class is missing (bad!)
52 protected function loadCommand ($commandName) {
53 // Init command instance
54 $commandInstance = NULL;
57 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($commandName) . 'Command';
59 // Create command class name
60 $this->setClassName($className);
62 // Is this class loaded?
63 if (!class_exists($this->getClassName())) {
64 // Class not found, so throw an exception
65 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
68 // Initiate the command
69 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
72 return $commandInstance;
76 * Returns an command instance for a given request class or null if
79 * @param $requestInstance An instance of a Requestable class
80 * @return $commandInstance An instance of the resolved command
81 * @throws InvalidCommandException Thrown if $commandName is
83 * @throws InvalidCommandInstanceException Thrown if $commandInstance
84 * is an invalid instance
86 public function resolveCommandByRequest (Requestable $requestInstance) {
89 $commandInstance = NULL;
91 // This goes fine so let's resolve the command
92 $commandName = $requestInstance->getRequestElement('command');
94 // Is the command empty? Then fall back to default command
95 if (empty($commandName)) {
96 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
99 // Check if command is valid
100 if ($this->isCommandValid($commandName) === FALSE) {
101 // This command is invalid!
102 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
106 $commandInstance = $this->loadCommand($commandName);
109 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
110 // This command has an invalid instance!
111 throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
115 $this->setResolvedInstance($commandInstance);
117 // Return the resolved command instance
118 return $commandInstance;
122 * Resolves the command by its direct name and returns an instance of its class
124 * @param $commandName The direct command name we shall resolve
125 * @return $commandInstance An instance of the command class
126 * @throws InvalidCommandException Thrown if $commandName is invalid
128 public function resolveCommand ($commandName) {
129 // Initiate the instance variable
130 $commandInstance = NULL;
132 // Is the command empty? Then fall back to default command
133 if (empty($commandName)) {
134 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
137 // Check if command is valid
138 if ($this->isCommandValid($commandName) === FALSE) {
139 // This command is invalid!
140 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
144 $commandInstance = $this->loadCommand($commandName);
146 // Return the instance
147 return $commandInstance;
151 * Checks whether the given command is valid
153 * @param $commandName The default command we shall execute
154 * @return $isValid Whether the given command is valid
155 * @throws EmptyVariableException Thrown if given command is not set
157 public function isCommandValid ($commandName) {
158 // By default nothing shall be valid
162 if (empty($commandName)) {
163 // Then thrown an exception here
164 throw new EmptyVariableException(array($this, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
167 // Create the full class name
168 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($commandName) . 'Command';
170 // Now, let us create the full name of the command class
171 $this->setClassName($className);
173 // Is this class already loaded?
174 if (class_exists($this->getClassName())) {
175 // This class does exist. :-)
180 $this->setCommandName($commandName);