3 namespace CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Request\Requestable;
10 * A generic command resolver class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class BaseCommandResolver extends BaseResolver {
33 * Protected constructor
35 * @param $className Name of the class
38 protected function __construct ($className) {
39 // Call parent constructor
40 parent::__construct($className);
44 * "Loads" a given command and instances it if not yet cached
46 * @param $commandName A command name we shall look for
47 * @return $commandInstance A loaded command instance
48 * @throws InvalidCommandException Thrown if even the default
49 * command class is missing (bad!)
51 protected function loadCommand ($commandName) {
52 // Init command instance
53 $commandInstance = NULL;
56 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($commandName) . 'Command';
58 // Create command class name
59 $this->setClassName($className);
61 // Is this class loaded?
62 if (!class_exists($this->getClassName())) {
63 // Class not found, so throw an exception
64 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
67 // Initiate the command
68 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
71 return $commandInstance;
75 * Returns an command instance for a given request class or null if
78 * @param $requestInstance An instance of a Requestable class
79 * @return $commandInstance An instance of the resolved command
80 * @throws InvalidCommandException Thrown if $commandName is
82 * @throws InvalidCommandInstanceException Thrown if $commandInstance
83 * is an invalid instance
85 public function resolveCommandByRequest (Requestable $requestInstance) {
88 $commandInstance = NULL;
90 // This goes fine so let's resolve the command
91 $commandName = $requestInstance->getRequestElement('command');
93 // Is the command empty? Then fall back to default command
94 if (empty($commandName)) {
95 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
98 // Check if command is valid
99 if ($this->isCommandValid($commandName) === FALSE) {
100 // This command is invalid!
101 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
105 $commandInstance = $this->loadCommand($commandName);
108 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
109 // This command has an invalid instance!
110 throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
114 $this->setResolvedInstance($commandInstance);
116 // Return the resolved command instance
117 return $commandInstance;
121 * Resolves the command by its direct name and returns an instance of its class
123 * @param $commandName The direct command name we shall resolve
124 * @return $commandInstance An instance of the command class
125 * @throws InvalidCommandException Thrown if $commandName is invalid
127 public function resolveCommand ($commandName) {
128 // Initiate the instance variable
129 $commandInstance = NULL;
131 // Is the command empty? Then fall back to default command
132 if (empty($commandName)) {
133 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
136 // Check if command is valid
137 if ($this->isCommandValid($commandName) === FALSE) {
138 // This command is invalid!
139 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
143 $commandInstance = $this->loadCommand($commandName);
145 // Return the instance
146 return $commandInstance;
150 * Checks whether the given command is valid
152 * @param $commandName The default command we shall execute
153 * @return $isValid Whether the given command is valid
154 * @throws EmptyVariableException Thrown if given command is not set
156 public function isCommandValid ($commandName) {
157 // By default nothing shall be valid
161 if (empty($commandName)) {
162 // Then thrown an exception here
163 throw new EmptyVariableException(array($this, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
166 // Create the full class name
167 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($commandName) . 'Command';
169 // Now, let us create the full name of the command class
170 $this->setClassName($className);
172 // Is this class already loaded?
173 if (class_exists($this->getClassName())) {
174 // This class does exist. :-)
179 $this->setCommandName($commandName);